/
retry_test.go
339 lines (312 loc) · 8.41 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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package retry_test
import (
"time"
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/clock"
gc "gopkg.in/check.v1"
"github.com/juju/retry"
)
type retrySuite struct {
testing.LoggingSuite
}
var _ = gc.Suite(&retrySuite{})
type mockClock struct {
delays []time.Duration
}
func (*mockClock) Now() time.Time {
return time.Now()
}
func (mock *mockClock) After(wait time.Duration) <-chan time.Time {
mock.delays = append(mock.delays, wait)
return time.After(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(errors.Cause(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`)
cause := errors.Cause(err)
c.Assert(cause, jc.Satisfies, retry.IsAttemptsExceeded)
retryError, _ := cause.(*retry.AttemptsExceeded)
c.Assert(retryError.LastError, 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,
BackoffFactor: 2,
})
c.Assert(errors.Cause(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(errors.Cause(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(errors.Cause(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(errors.Cause(err), jc.Satisfies, retry.IsRetryStopped)
c.Assert(clock.delays, gc.HasLen, count)
}
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,
BackoffFactor: 2,
Clock: clock,
})
c.Assert(errors.Cause(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,
})
c.Assert(errors.Cause(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,
})
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,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Attempts not valid`)
}
func (*retrySuite) TestMissingDelayNotValid(c *gc.C) {
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 5,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `missing Delay not valid`)
}
func (*retrySuite) TestBackoffErrors(c *gc.C) {
// Backoff values of less than one are a validation error.
for _, factor := range []float64{-2, 0.5} {
err := retry.Call(retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 5,
Delay: time.Minute,
BackoffFactor: factor,
})
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `BackoffFactor of .* not valid`)
}
}
func (*retrySuite) TestCallArgsDefaults(c *gc.C) {
// BackoffFactor is one of the two values with reasonable
// defaults, and the default is linear if not specified.
// The other default is the Clock. If not specified, it is the
// wall clock.
args := retry.CallArgs{
Func: func() error { return errors.New("bah") },
Attempts: 5,
Delay: time.Minute,
}
err := args.Validate()
c.Assert(err, jc.ErrorIsNil)
c.Assert(args.BackoffFactor, gc.Equals, float64(1))
c.Assert(args.Clock, gc.Equals, clock.WallClock)
}
func (*retrySuite) TestScaleDuration(c *gc.C) {
for i, test := range []struct {
current time.Duration
max time.Duration
scale float64
expect time.Duration
}{{
current: time.Minute,
scale: 1,
expect: time.Minute,
}, {
current: time.Minute,
scale: 2.5,
expect: 2*time.Minute + 30*time.Second,
}, {
current: time.Minute,
max: 3 * time.Minute,
scale: 10,
expect: 3 * time.Minute,
}, {
current: time.Minute,
max: 3 * time.Minute,
scale: 2,
expect: 2 * time.Minute,
}, {
// scale factors of < 1 are not passed in from the Call function
// but are supported by ScaleDuration
current: time.Minute,
scale: 0.5,
expect: 30 * time.Second,
}, {
current: time.Minute,
scale: 0,
expect: 0,
}, {
// negative scales are treated as positive
current: time.Minute,
scale: -2,
expect: 2 * time.Minute,
}} {
c.Logf("test %d", i)
c.Check(retry.ScaleDuration(test.current, test.max, test.scale), gc.Equals, test.expect)
}
}