Skip to content

Commit 64870c0

Browse files
committed
update backoff test
1 parent 7d24978 commit 64870c0

File tree

1 file changed

+35
-83
lines changed

1 file changed

+35
-83
lines changed

backoff_test.go

Lines changed: 35 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pgsql_test
22

33
import (
4-
"fmt"
54
"testing"
65
"time"
76

@@ -22,7 +21,7 @@ func TestExponentialBackoff(t *testing.T) {
2221

2322
// Test exponential growth
2423
delays := []time.Duration{}
25-
for i := 0; i < 5; i++ {
24+
for i := 0; i < 10; i++ {
2625
delay := backoff(i)
2726
delays = append(delays, delay)
2827
}
@@ -33,6 +32,14 @@ func TestExponentialBackoff(t *testing.T) {
3332
t.Errorf("Expected delay[%d] >= delay[%d], got %v < %v", i, i-1, delays[i], delays[i-1])
3433
}
3534
}
35+
36+
// Verify max delay
37+
for i := 0; i < 10; i++ {
38+
delay := backoff(i)
39+
if delay > config.MaxDelay {
40+
t.Errorf("Expected delay[%d] <= MaxDelay (%v), got %v", i, config.MaxDelay, delay)
41+
}
42+
}
3643
}
3744

3845
func TestExponentialBackoffWithFullJitter(t *testing.T) {
@@ -68,6 +75,14 @@ func TestExponentialBackoffWithFullJitter(t *testing.T) {
6875
if allSame {
6976
t.Error("Expected jitter to produce different delays, but all delays were the same")
7077
}
78+
79+
// Verify max delay
80+
for i := 0; i < 15; i++ {
81+
delay := backoff(i)
82+
if delay > config.MaxDelay {
83+
t.Errorf("Expected delay[%d] <= MaxDelay (%v), got %v", i, config.MaxDelay, delay)
84+
}
85+
}
7186
}
7287

7388
func TestExponentialBackoffWithEqualJitter(t *testing.T) {
@@ -92,17 +107,25 @@ func TestExponentialBackoffWithEqualJitter(t *testing.T) {
92107
if delay < expectedMin {
93108
t.Errorf("Expected delay >= %v with equal jitter, got %v", expectedMin, delay)
94109
}
110+
111+
// Verify max delay
112+
for i := 0; i < 15; i++ {
113+
delay := backoff(i)
114+
if delay > config.MaxDelay {
115+
t.Errorf("Expected delay[%d] <= MaxDelay (%v), got %v", i, config.MaxDelay, delay)
116+
}
117+
}
95118
}
96119

97120
func TestLinearBackoff(t *testing.T) {
98121
t.Parallel()
99122

100123
config := pgsql.LinearBackoffConfig{
101124
BackoffConfig: pgsql.BackoffConfig{
102-
BaseDelay: 50 * time.Millisecond,
103-
MaxDelay: 500 * time.Millisecond,
125+
BaseDelay: 100 * time.Millisecond,
126+
MaxDelay: 1 * time.Second,
104127
},
105-
Increment: 50 * time.Millisecond,
128+
Increment: 100 * time.Millisecond,
106129
}
107130
backoff := pgsql.NewLinearBackoff(config)
108131

@@ -115,90 +138,19 @@ func TestLinearBackoff(t *testing.T) {
115138

116139
// Verify linear growth
117140
for i := 1; i < len(delays); i++ {
118-
expectedIncrease := 50 * time.Millisecond
141+
expectedIncrease := 100 * time.Millisecond
119142
actualIncrease := delays[i] - delays[i-1]
120143

121144
if actualIncrease != expectedIncrease {
122145
t.Errorf("Expected linear increase of %v, got %v", expectedIncrease, actualIncrease)
123146
}
124147
}
125-
}
126148

127-
func TestDefaultBackoffFunctions(t *testing.T) {
128-
t.Parallel()
129-
130-
testCases := []struct {
131-
name string
132-
backoff pgsql.BackoffDelayFunc
133-
}{
134-
{"DefaultExponentialBackoff", pgsql.DefaultExponentialBackoff()},
135-
{"DefaultExponentialBackoffWithFullJitter", pgsql.DefaultExponentialBackoffWithFullJitter()},
136-
{"DefaultExponentialBackoffWithEqualJitter", pgsql.DefaultExponentialBackoffWithEqualJitter()},
137-
{"DefaultLinearBackoff", pgsql.DefaultLinearBackoff()},
138-
}
139-
140-
for _, tc := range testCases {
141-
t.Run(tc.name, func(t *testing.T) {
142-
delay := tc.backoff(0)
143-
144-
// Should have some delay (base delay should be at least 50ms for all defaults)
145-
if delay < 50*time.Millisecond {
146-
t.Errorf("Expected some delay, got %v", delay)
147-
}
148-
})
149-
}
150-
}
151-
152-
func TestMaxDelayIsRespected(t *testing.T) {
153-
t.Parallel()
154-
155-
config := pgsql.ExponentialBackoffConfig{
156-
BackoffConfig: pgsql.BackoffConfig{
157-
BaseDelay: 100 * time.Millisecond,
158-
MaxDelay: 200 * time.Millisecond, // Very low max delay
159-
},
160-
Multiplier: 2.0,
161-
}
162-
backoff := pgsql.NewExponentialBackoff(config)
163-
164-
// Test that max delay is respected even with high attempt numbers
165-
delay := backoff(10) // This would normally result in a very long delay
166-
167-
maxExpected := 200 * time.Millisecond
168-
169-
if delay > maxExpected {
170-
t.Errorf("Expected delay capped at %v, got %v", maxExpected, delay)
171-
}
172-
}
173-
174-
// Example demonstrating usage with transaction retry
175-
func ExampleNewExponentialBackoff() {
176-
// Create a custom exponential backoff
177-
backoff := pgsql.NewExponentialBackoff(pgsql.ExponentialBackoffConfig{
178-
BackoffConfig: pgsql.BackoffConfig{
179-
BaseDelay: 100 * time.Millisecond,
180-
MaxDelay: 5 * time.Second,
181-
},
182-
Multiplier: 2.0,
183-
})
184-
185-
// Use with transaction options
186-
opts := &pgsql.TxOptions{
187-
MaxAttempts: 5,
188-
BackoffDelayFunc: backoff,
189-
}
190-
191-
fmt.Printf("Transaction options configured with custom backoff (MaxAttempts: %d)\n", opts.MaxAttempts)
192-
// Output: Transaction options configured with custom backoff (MaxAttempts: 5)
193-
}
194-
195-
func ExampleDefaultExponentialBackoffWithFullJitter() {
196-
// Use a pre-configured exponential backoff with full jitter
197-
opts := &pgsql.TxOptions{
198-
MaxAttempts: 3,
199-
BackoffDelayFunc: pgsql.DefaultExponentialBackoffWithFullJitter(),
149+
// Verify max delay
150+
for i := 0; i < 15; i++ {
151+
delay := backoff(i)
152+
if delay > config.MaxDelay {
153+
t.Errorf("Expected delay[%d] <= MaxDelay (%v), got %v", i, config.MaxDelay, delay)
154+
}
200155
}
201-
202-
fmt.Printf("Transaction options with full jitter backoff (MaxAttempts: %d)\n", opts.MaxAttempts)
203-
// Output: Transaction options with full jitter backoff (MaxAttempts: 3)
204156
}

0 commit comments

Comments
 (0)