1
1
package pgsql_test
2
2
3
3
import (
4
- "fmt"
5
4
"testing"
6
5
"time"
7
6
@@ -22,7 +21,7 @@ func TestExponentialBackoff(t *testing.T) {
22
21
23
22
// Test exponential growth
24
23
delays := []time.Duration {}
25
- for i := 0 ; i < 5 ; i ++ {
24
+ for i := 0 ; i < 10 ; i ++ {
26
25
delay := backoff (i )
27
26
delays = append (delays , delay )
28
27
}
@@ -33,6 +32,14 @@ func TestExponentialBackoff(t *testing.T) {
33
32
t .Errorf ("Expected delay[%d] >= delay[%d], got %v < %v" , i , i - 1 , delays [i ], delays [i - 1 ])
34
33
}
35
34
}
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
+ }
36
43
}
37
44
38
45
func TestExponentialBackoffWithFullJitter (t * testing.T ) {
@@ -68,6 +75,14 @@ func TestExponentialBackoffWithFullJitter(t *testing.T) {
68
75
if allSame {
69
76
t .Error ("Expected jitter to produce different delays, but all delays were the same" )
70
77
}
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
+ }
71
86
}
72
87
73
88
func TestExponentialBackoffWithEqualJitter (t * testing.T ) {
@@ -92,17 +107,25 @@ func TestExponentialBackoffWithEqualJitter(t *testing.T) {
92
107
if delay < expectedMin {
93
108
t .Errorf ("Expected delay >= %v with equal jitter, got %v" , expectedMin , delay )
94
109
}
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
+ }
95
118
}
96
119
97
120
func TestLinearBackoff (t * testing.T ) {
98
121
t .Parallel ()
99
122
100
123
config := pgsql.LinearBackoffConfig {
101
124
BackoffConfig : pgsql.BackoffConfig {
102
- BaseDelay : 50 * time .Millisecond ,
103
- MaxDelay : 500 * time .Millisecond ,
125
+ BaseDelay : 100 * time .Millisecond ,
126
+ MaxDelay : 1 * time .Second ,
104
127
},
105
- Increment : 50 * time .Millisecond ,
128
+ Increment : 100 * time .Millisecond ,
106
129
}
107
130
backoff := pgsql .NewLinearBackoff (config )
108
131
@@ -115,90 +138,19 @@ func TestLinearBackoff(t *testing.T) {
115
138
116
139
// Verify linear growth
117
140
for i := 1 ; i < len (delays ); i ++ {
118
- expectedIncrease := 50 * time .Millisecond
141
+ expectedIncrease := 100 * time .Millisecond
119
142
actualIncrease := delays [i ] - delays [i - 1 ]
120
143
121
144
if actualIncrease != expectedIncrease {
122
145
t .Errorf ("Expected linear increase of %v, got %v" , expectedIncrease , actualIncrease )
123
146
}
124
147
}
125
- }
126
148
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
+ }
200
155
}
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)
204
156
}
0 commit comments