-
Notifications
You must be signed in to change notification settings - Fork 5
/
retry_test.go
443 lines (409 loc) · 13.7 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package retry_test
import (
"math"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/retry"
)
type retrySuite struct {
testing.LoggingSuite
}
var _ = gc.Suite(&retrySuite{})
type mockClock struct {
now time.Time
delays []time.Duration
}
func (mock *mockClock) Now() time.Time {
return mock.now
}
func (mock *mockClock) After(wait time.Duration) <-chan time.Time {
mock.delays = append(mock.delays, wait)
mock.now = mock.now.Add(wait)
// Note (jam): 2024-09-16 on my machine,
// go test -count 500 -failfast -check.f StopChannel
// Fails reliably at 1 Microsecond. I think the issue is that at 1us,
// the clock can tick while the after func is being evaluated.
return time.After(10 * time.Microsecond)
}
func (*retrySuite) TestSuccessHasNoDelay(c *gc.C) {
clock := &mockClock{}
err := retry.Call(retry.CallArgs{
Func: func() error { return nil },
Attempts: 5,
Delay: time.Minute,
Clock: clock,
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(clock.delays, gc.HasLen, 0)
}
func (*retrySuite) TestCalledOnceEvenIfStopped(c *gc.C) {
stop := make(chan struct{})
clock := &mockClock{}
called := false
close(stop)
err := retry.Call(retry.CallArgs{
Func: func() error {
called = true
return nil
},
Attempts: 5,
Delay: time.Minute,
Clock: clock,
Stop: stop,
})
c.Assert(called, jc.IsTrue)
c.Assert(err, jc.ErrorIsNil)
c.Assert(clock.delays, gc.HasLen, 0)
}
func (*retrySuite) TestAttempts(c *gc.C) {
clock := &mockClock{}
funcErr := errors.New("bah")
err := retry.Call(retry.CallArgs{
Func: func() error { return funcErr },
Attempts: 4,
Delay: time.Minute,
Clock: clock,
})
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
// We delay between attempts, and don't delay after the last one.
c.Assert(clock.delays, jc.DeepEquals, []time.Duration{
time.Minute,
time.Minute,
time.Minute,
})
}
func (*retrySuite) TestAttemptsExceededError(c *gc.C) {
clock := &mockClock{}
funcErr := errors.New("bah")
err := retry.Call(retry.CallArgs{
Func: func() error { return funcErr },
Attempts: 5,
Delay: time.Minute,
Clock: clock,
})
c.Assert(err, gc.ErrorMatches, `attempt count exceeded: bah`)
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
c.Assert(retry.LastError(err), gc.Equals, funcErr)
}
func (*retrySuite) TestFatalErrorsNotRetried(c *gc.C) {
clock := &mockClock{}
funcErr := errors.New("bah")
err := retry.Call(retry.CallArgs{
Func: func() error { return funcErr },
IsFatalError: func(error) bool { return true },
Attempts: 5,
Delay: time.Minute,
Clock: clock,
})
c.Assert(errors.Cause(err), gc.Equals, funcErr)
c.Assert(clock.delays, gc.HasLen, 0)
}
func (*retrySuite) TestBackoffFactor(c *gc.C) {
clock := &mockClock{}
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Clock: clock,
Attempts: 5,
Delay: time.Minute,
BackoffFunc: retry.DoubleDelay,
})
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
c.Assert(clock.delays, jc.DeepEquals, []time.Duration{
time.Minute,
time.Minute * 2,
time.Minute * 4,
time.Minute * 8,
})
}
func (*retrySuite) TestStopChannel(c *gc.C) {
clock := &mockClock{}
stop := make(chan struct{})
count := 0
err := retry.Call(retry.CallArgs{
Func: func() error {
if count == 2 {
close(stop)
}
count++
return errors.New("bah")
},
Attempts: 5,
Delay: time.Minute,
Clock: clock,
Stop: stop,
})
c.Assert(err, jc.Satisfies, retry.IsRetryStopped)
c.Assert(clock.delays, gc.HasLen, 3)
}
func (*retrySuite) TestNotifyFunc(c *gc.C) {
var (
clock = &mockClock{}
funcErr = errors.New("bah")
attempts []int
funcErrors []error
)
err := retry.Call(retry.CallArgs{
Func: func() error {
return funcErr
},
NotifyFunc: func(lastError error, attempt int) {
funcErrors = append(funcErrors, lastError)
attempts = append(attempts, attempt)
},
Attempts: 3,
Delay: time.Minute,
Clock: clock,
})
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
c.Assert(clock.delays, gc.HasLen, 2)
c.Assert(funcErrors, jc.DeepEquals, []error{funcErr, funcErr, funcErr})
c.Assert(attempts, jc.DeepEquals, []int{1, 2, 3})
}
func (*retrySuite) TestInfiniteRetries(c *gc.C) {
// OK, we can't test infinite, but we'll go for lots.
clock := &mockClock{}
stop := make(chan struct{})
count := 0
err := retry.Call(retry.CallArgs{
Func: func() error {
if count == 111 {
close(stop)
}
count++
return errors.New("bah")
},
Attempts: retry.UnlimitedAttempts,
Delay: time.Minute,
Clock: clock,
Stop: stop,
})
c.Assert(err, jc.Satisfies, retry.IsRetryStopped)
c.Assert(clock.delays, gc.HasLen, count)
}
func (*retrySuite) TestMaxDuration(c *gc.C) {
clock := &mockClock{}
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Delay: time.Minute,
MaxDuration: 5 * time.Minute,
Clock: clock,
})
c.Assert(err, jc.Satisfies, retry.IsDurationExceeded)
c.Assert(clock.delays, jc.DeepEquals, []time.Duration{
time.Minute,
time.Minute,
time.Minute,
time.Minute,
time.Minute,
})
}
func (*retrySuite) TestMaxDurationDoubling(c *gc.C) {
clock := &mockClock{}
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Delay: time.Minute,
MaxDuration: 10 * time.Minute,
BackoffFunc: retry.DoubleDelay,
Clock: clock,
})
c.Assert(err, jc.Satisfies, retry.IsDurationExceeded)
// Stops after seven minutes, because the next wait time
// would take it to 15 minutes.
c.Assert(clock.delays, jc.DeepEquals, []time.Duration{
time.Minute,
2 * time.Minute,
4 * time.Minute,
})
}
func (*retrySuite) TestMaxDelay(c *gc.C) {
clock := &mockClock{}
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 7,
Delay: time.Minute,
MaxDelay: 10 * time.Minute,
BackoffFunc: retry.DoubleDelay,
Clock: clock,
})
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
c.Assert(clock.delays, jc.DeepEquals, []time.Duration{
time.Minute,
2 * time.Minute,
4 * time.Minute,
8 * time.Minute,
10 * time.Minute,
10 * time.Minute,
})
}
func (*retrySuite) TestWithWallClock(c *gc.C) {
var attempts []int
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
NotifyFunc: func(lastError error, attempt int) {
attempts = append(attempts, attempt)
},
Attempts: 5,
Delay: time.Microsecond,
Clock: clock.WallClock,
})
c.Assert(err, jc.Satisfies, retry.IsAttemptsExceeded)
c.Assert(attempts, jc.DeepEquals, []int{1, 2, 3, 4, 5})
}
func (*retrySuite) TestMissingFuncNotValid(c *gc.C) {
err := retry.Call(retry.CallArgs{
Attempts: 5,
Delay: time.Minute,
Clock: clock.WallClock,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Func not valid`)
}
func (*retrySuite) TestMissingAttemptsNotValid(c *gc.C) {
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Delay: time.Minute,
Clock: clock.WallClock,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Attempts or MaxDuration not valid`)
}
func (*retrySuite) TestMissingDelayNotValid(c *gc.C) {
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 5,
Clock: clock.WallClock,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Delay not valid`)
}
func (*retrySuite) TestMissingClockNotValid(c *gc.C) {
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 5,
Delay: time.Minute,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Clock not valid`)
}
type expBackoffSuite struct {
testing.LoggingSuite
}
var _ = gc.Suite(&expBackoffSuite{})
func (*expBackoffSuite) TestExpBackoffWithoutJitter(c *gc.C) {
backoffFunc := retry.ExpBackoff(200*time.Millisecond, 2*time.Second, 2.0, false)
expDurations := []time.Duration{
200 * time.Millisecond,
400 * time.Millisecond,
800 * time.Millisecond,
1600 * time.Millisecond,
2000 * time.Millisecond, // capped to maxDelay
}
for attempt, expDuration := range expDurations {
got := backoffFunc(0, attempt)
c.Assert(got, gc.Equals, expDuration, gc.Commentf("unexpected duration for attempt %d", attempt))
}
}
func (*expBackoffSuite) TestExpBackoffWithJitter(c *gc.C) {
minDelay := 200 * time.Millisecond
backoffFunc := retry.ExpBackoff(minDelay, 2*time.Second, 2.0, true)
// All of these are allowed to go up to 20% over the expected value
maxDurations := []time.Duration{
240 * time.Millisecond,
480 * time.Millisecond,
960 * time.Millisecond,
1920 * time.Millisecond,
2000 * time.Millisecond, // capped to maxDelay
}
for attempt, maxDuration := range maxDurations {
got := backoffFunc(0, attempt)
c.Assert(got, jc.GreaterThan, minDelay-1, gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s", attempt, minDelay, maxDuration, got))
c.Assert(got, jc.LessThan, maxDuration+1, gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s", attempt, minDelay, maxDuration, got))
}
}
// TestExpBackofWithJitterAverage makes sure that turning on Jitter doesn't
// dramatically change the average wait times for sampling. (eg, if we say wait
// 200ms to 2000ms, turning on jitter should keep the wait times roughly aligned with those times).
// This is a little bit tricky, because we expect it to be random, but we'll
// look at the average ratio of the jittered value and the expected backoff value.
func (*expBackoffSuite) TestExpBackofWithJitterAverage(c *gc.C) {
const (
// 1.02^100 ~= 10, causing us to go from 200ms to 2s in 100 steps
minDelay = 200 * time.Millisecond
maxDelay = 2 * time.Second
maxAttempts = 100
backoff = 1.02
)
jitterBackoffFunc := retry.ExpBackoff(minDelay, maxDelay, backoff, true)
noJitterBackoffFunc := retry.ExpBackoff(minDelay, maxDelay, backoff, false)
ratioSum := 0.0
for attempt := 0; attempt < maxAttempts; attempt++ {
jitterValue := jitterBackoffFunc(0, attempt)
nonJitterValue := noJitterBackoffFunc(0, attempt)
ratio := float64(jitterValue) / float64(nonJitterValue)
ratioSum += ratio
// We have > and < not >=, so we need a bit of flexibility,
// also float64 imprecision gives us a bit more than 1ns of
// inaccuracy
minJitter := time.Duration(0.8*float64(nonJitterValue)) - time.Millisecond
maxJitter := time.Duration(1.2*float64(nonJitterValue)) + time.Millisecond
c.Assert(jitterValue, jc.GreaterThan, minJitter,
gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minJitter, maxJitter, jitterValue))
c.Assert(jitterValue, jc.LessThan, maxJitter,
gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minJitter, maxJitter, jitterValue))
}
// We could do a geometric mean instead of a arithmetic mean because we
// are dealing with ratios, but ratios should stay in the range of 0-2,
// so arithmetic makes sense.
ratioAvg := ratioSum / maxAttempts
// In practice, while individual attempts might vary by +/- 20%, they
// average out over 100 steps, and we actually end up within +/- 1% on
// average. The most I've seen is a 3.6% variation.
// We move this out to 10% to avoid an annoying test (eg, string of low
// randoms).
c.Check(ratioAvg, jc.GreaterThan, 0.9,
gc.Commentf("jitter reduced the average duration by %.3f, we expected it to be +/- 2%% on average", ratioAvg))
c.Check(ratioAvg, jc.LessThan, 1.1,
gc.Commentf("jitter increased the average duration by %.3f, we expected it to be +/- 2%% on average", ratioAvg))
}
// TestExpBackoffBadExponent says that we'll at least roughly conform to
// expectations even if the user gives us a bad backoff factor.
// We don't have a mechanism for giving an error, but at least hold to min and
// max values.
func (*expBackoffSuite) TestExpBackoffBadExponent(c *gc.C) {
const (
// a backoff of 0.8 would put us below 200ms
minDelay = 200 * time.Millisecond
maxDelay = 2 * time.Second
maxAttempts = 10
backoff = 0.8
)
jitterBackoffFunc := retry.ExpBackoff(minDelay, maxDelay, backoff, true)
noJitterBackoffFunc := retry.ExpBackoff(minDelay, maxDelay, backoff, false)
for attempt := 0; attempt < maxAttempts; attempt++ {
jitterValue := jitterBackoffFunc(0, attempt)
nonJitterValue := noJitterBackoffFunc(0, attempt)
c.Check(nonJitterValue, jc.GreaterThan, minDelay-1,
gc.Commentf("expected duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minDelay, maxDelay, nonJitterValue))
c.Check(nonJitterValue, jc.LessThan, maxDelay+1,
gc.Commentf("expected duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minDelay, maxDelay, nonJitterValue))
// Ensure that even with jitter we are still capped at min and max delay
minJitter := time.Duration(math.Max(0.8*float64(nonJitterValue), float64(minDelay))) - time.Microsecond
maxJitter := time.Duration(math.Min(1.2*float64(nonJitterValue), float64(maxDelay))) + time.Microsecond
c.Check(jitterValue, jc.GreaterThan, minJitter,
gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minJitter, maxJitter, jitterValue))
c.Check(jitterValue, jc.LessThan, maxJitter,
gc.Commentf("expected jittered duration for attempt %d to be in the [%s, %s] range; got %s",
attempt, minJitter, maxJitter, jitterValue))
}
}