-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry_test.go
359 lines (332 loc) · 9.34 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package retry
import (
"errors"
"sync"
"testing"
"time"
)
var errTest = errors.New("test error")
func failTimes(i int) func() error {
return func() error {
for i > 0 {
i--
return errTest
}
return nil
}
}
func panicTimes(i int) func() error {
return func() error {
for i > 0 {
i--
panic(errTest)
}
return nil
}
}
func TestRetries(t *testing.T) {
e := NewExecutor().WithRetries(0).WithDelay(0)
if err := e.Execute(failTimes(0)); err != nil {
t.Error("error found and not expected")
}
if err := e.Execute(failTimes(1)); err != ErrMaxRetries {
t.Error("ErrMaxRetries expected and not found")
}
e.WithRetries(1)
if err := e.Execute(failTimes(0)); err != nil {
t.Error("error found and not expected")
}
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error expected and not found")
}
if err := e.Execute(failTimes(2)); err != ErrMaxRetries {
t.Error("ErrMaxRetries expected and not found")
}
e.WithRetries(2)
if err := e.Execute(failTimes(0)); err != nil {
t.Error("error found and not expected")
}
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error expected and not found")
}
if err := e.Execute(failTimes(2)); err != nil {
t.Error("error expected and not found")
}
if err := e.Execute(failTimes(3)); err != ErrMaxRetries {
t.Error("ErrMaxRetries expected and not found")
}
}
func TestWithError(t *testing.T) {
sameErr := errors.New("test error")
diffErr := errors.New("different error")
var testCases = []struct {
times int
delay time.Duration
withErr error
expErr error
}{
{0, 0, errTest, nil},
{1, 100 * time.Millisecond, errTest, nil},
{2, 100 * time.Millisecond, errTest, ErrMaxRetries},
{2, 100 * time.Millisecond, sameErr, ErrMaxRetries},
{2, 0, diffErr, errTest},
}
for _, c := range testCases {
e := NewExecutor().WithDelay(100 * time.Millisecond).WithError(c.withErr)
if err := e.Execute(failTimes(c.times)); err != c.expErr {
t.Error("error found and not expected")
}
if e.Context.LastDelay != c.delay {
t.Error("invalid Context.LastDelay")
}
}
}
func TestWithErrorComparator(t *testing.T) {
var testCases = []struct {
times int
delay time.Duration
withErrComp func(error) bool
expErr error
}{
{0, 0, nil, nil},
{1, 100 * time.Millisecond, nil, nil},
{1, 100 * time.Millisecond, func(error) bool { return true }, nil},
{1, 0, func(error) bool { return false }, errTest},
{2, 100 * time.Millisecond, nil, ErrMaxRetries},
{2, 100 * time.Millisecond, func(error) bool { return true }, ErrMaxRetries},
{2, 0, func(error) bool { return false }, errTest},
}
for _, c := range testCases {
e := NewExecutor().WithDelay(100 * time.Millisecond).WithErrorComparator(c.withErrComp)
if err := e.Execute(failTimes(c.times)); err != c.expErr {
t.Error("error found and not expected")
}
if e.Context.LastDelay != c.delay {
t.Error("invalid Context.LastDelay")
}
}
}
func TestWithErrorChan(t *testing.T) {
var wg sync.WaitGroup
var testCases = []struct {
times int
expErr error
}{
{1, nil},
{5, nil},
{6, ErrMaxRetries},
}
for _, c := range testCases {
e := NewExecutor().WithRetries(5).WithDelay(100 * time.Millisecond).WithErrorChannel()
// Read errors
wg.Add(1)
go func(times int) {
var i int
for range e.ErrorChannel {
i++
}
if i != times {
t.Error("invalid number of errors")
}
wg.Done()
}(c.times)
// Execute
if err := e.Execute(failTimes(c.times)); err != c.expErr {
t.Error("error found and not expected")
}
}
wg.Wait()
}
func TestWithPanic(t *testing.T) {
e := NewExecutor().
WithDelay(100 * time.Millisecond).
WithPanic()
if err := e.Execute(panicTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay != 100*time.Millisecond {
t.Error("invalid Context.LastDelay", e.Context.LastDelay)
}
}
func TestWithBackoff(t *testing.T) {
}
func TestWithDelay(t *testing.T) {
var testCases = []struct {
delay time.Duration
expected time.Duration
}{{0, 0}, {100, 100}, {-100, 0}}
for _, d := range testCases {
e := NewExecutor().WithDelay(d.delay)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay != d.expected {
t.Error("invalid Context.LastDelay")
}
}
}
func TestWithFirstRetryNoDelay(t *testing.T) {
e := NewExecutor().WithFirstRetryNoDelay()
now := time.Now()
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if time.Now().Sub(now) >= 1*time.Second {
t.Error("first retry had a delay")
}
if e.Context.LastDelay != 0 {
t.Error("invalid Context.LastDelay")
}
e = NewExecutor().WithRetries(2).WithFirstRetryNoDelay()
if err := e.Execute(failTimes(2)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay != 1*time.Second {
t.Error("invalid Context.LastDelay", e.Context.LastDelay)
}
}
func TestWithMinDelay(t *testing.T) {
e := NewExecutor().WithMinDelay(100 * time.Millisecond)
now := time.Now()
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if time.Now().Sub(now) <= 1*time.Second {
t.Error("min delay did not work")
}
if e.Context.LastDelay != 1*time.Second {
t.Error("invalid Context.LastDelay")
}
e = NewExecutor().WithMinDelay(2 * time.Second)
now = time.Now()
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if time.Now().Sub(now) < 2*time.Second {
t.Error("min delay did not work")
}
if e.Context.LastDelay != 2*time.Second {
t.Error("invalid Context.LastDelay")
}
}
func TestWithMaxDelay(t *testing.T) {
e := NewExecutor().WithMaxDelay(100 * time.Millisecond)
now := time.Now()
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if time.Now().Sub(now) >= 1*time.Second {
t.Error("max delay did not work")
}
if e.Context.LastDelay != 100*time.Millisecond {
t.Error("invalid Context.LastDelay")
}
e = NewExecutor().WithMaxDelay(2 * time.Second)
now = time.Now()
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if time.Now().Sub(now) >= 2*time.Second {
t.Error("max delay did not work")
}
if e.Context.LastDelay != 1*time.Second {
t.Error("invalid Context.LastDelay")
}
}
func TestWithFixedJitter(t *testing.T) {
e := NewExecutor().
WithDelay(100 * time.Millisecond).
WithFixedJitter(1 * time.Millisecond)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay != 101*time.Millisecond {
t.Error("invalid Context.LastDelay")
}
}
func TestWithUniformJitter(t *testing.T) {
// Force a seed so the random generator does not return 0ms
// causing the test to fail.
// Seed(1) causes a negative jitter.
// Seed(2) causes a positive jitter.
for _, seed := range []int64{1, 2} {
Seed(seed)
e := NewExecutor().
WithDelay(100 * time.Millisecond).
WithUniformJitter(100 * time.Millisecond)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay == 100*time.Millisecond {
t.Error("invalid Context.LastDelay")
}
// Make sure it's uniform
lastDelay := e.Context.LastDelay
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay != lastDelay {
t.Error("jitter is not uniform")
}
}
}
func TestWithProportionalJitter(t *testing.T) {
var testCases = []struct {
multiplier float64
minDelay time.Duration
maxDelay time.Duration
}{
{0, 100 * time.Millisecond, 100 * time.Millisecond}, // +/- 0%
{0.1, 90 * time.Millisecond, 110 * time.Millisecond}, // +/- 10%
{0.5, 50 * time.Millisecond, 150 * time.Millisecond}, // +/- 50%
{1, 0 * time.Millisecond, 200 * time.Millisecond}, // +/- 100%
{2, 0 * time.Millisecond, 300 * time.Millisecond}, // +/- 200%
}
for _, c := range testCases {
e := NewExecutor().
WithDelay(100 * time.Millisecond).
WithProportionalJitter(c.multiplier)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay < c.minDelay || e.Context.LastDelay > c.maxDelay {
t.Error("invalid Context.LastDelay")
}
// With negative multiplier should have the same behavior
e = NewExecutor().
WithDelay(100 * time.Millisecond).
WithProportionalJitter(-c.multiplier)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay < c.minDelay || e.Context.LastDelay > c.maxDelay {
t.Error("invalid Context.LastDelay")
}
}
}
func TestWithRandomJitter(t *testing.T) {
// Force a seed so the random generator does not return 0ms
// causing the test to fail.
// Seed(1) causes a negative jitter.
// Seed(2) causes a positive jitter.
for _, seed := range []int64{1, 2} {
Seed(seed)
e := NewExecutor().
WithDelay(100 * time.Millisecond).
WithRandomJitter(100 * time.Millisecond)
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay == 100*time.Millisecond {
t.Error("invalid Context.LastDelay")
}
// Make sure it's not uniform
lastDelay := e.Context.LastDelay
if err := e.Execute(failTimes(1)); err != nil {
t.Error("error found and not expected")
}
if e.Context.LastDelay == lastDelay {
t.Error("jitter is not random")
}
}
}