-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_white_test.go
370 lines (343 loc) · 7.94 KB
/
backoff_white_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
package backoff
import (
"context"
"fmt"
"math"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/rhomel/backoff/test/try"
)
// whitebox tests
type durations struct {
durations []time.Duration
}
// log the received pause durations as `try` backs-off
func afterFnLogger() (*durations, func(time.Duration) <-chan time.Time) {
ds := &durations{}
return ds, func(d time.Duration) <-chan time.Time {
ds.durations = append(ds.durations, d)
return defaultAfterFunc(d)
}
}
func Test_try(t *testing.T) {
var (
shortDelay = 10 * time.Millisecond
shortInterval = Exponential{
Base: 2 * time.Millisecond,
Unit: time.Millisecond,
Initial: 1 * time.Millisecond,
Max: 20 * time.Millisecond,
}
)
cases := map[string]struct {
trueAfterN int
tries int8
initI int8
initWait time.Duration
timeout time.Duration
delay time.Duration
interval Intervals
wantErr error
wantDurations []time.Duration
wantEvents []string
}{
"Succeed After 3 Tries": {
trueAfterN: 3,
tries: 10,
initI: 0,
initWait: 0,
timeout: time.Second,
delay: shortDelay,
interval: shortInterval,
wantErr: nil,
wantDurations: []time.Duration{
1 * time.Millisecond,
2 * time.Millisecond,
4 * time.Millisecond,
},
wantEvents: []string{
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnTrue,
},
},
"Should Not Overflow": {
trueAfterN: 2,
tries: InfiniteTries,
initI: math.MaxInt8 - 1,
initWait: 0,
timeout: time.Second,
delay: shortDelay,
interval: shortInterval,
wantErr: nil,
wantDurations: []time.Duration{
shortInterval.Max,
shortInterval.Max,
},
wantEvents: []string{
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnTrue,
},
},
"Fail after 4 tries": {
trueAfterN: 4,
tries: 4,
initI: 0,
initWait: 0,
timeout: time.Second,
delay: shortDelay,
interval: shortInterval,
wantErr: AllTriesFailed,
wantDurations: []time.Duration{
1 * time.Millisecond,
2 * time.Millisecond,
4 * time.Millisecond,
},
wantEvents: []string{
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
try.CaseAfter,
try.CaseReturnFalse,
},
},
"Fail after context timeout during fn call": {
trueAfterN: 6,
tries: 10,
initI: 0,
initWait: 0,
timeout: 70 * time.Millisecond,
delay: 50 * time.Millisecond,
interval: Exponential{
Base: 0 * time.Millisecond,
Unit: time.Millisecond,
Initial: 0 * time.Millisecond,
Max: 0 * time.Millisecond,
},
wantErr: BackoffContextTimeoutExceeded,
wantDurations: []time.Duration{
0 * time.Millisecond,
0 * time.Millisecond,
},
wantEvents: []string{
try.CaseAfter,
try.CaseReturnFalse,
try.CaseDone,
try.CaseReturnFalse,
},
},
"Fail after context timeout during backoff pause": {
trueAfterN: 6,
tries: 10,
initI: 0,
initWait: 0,
timeout: 70 * time.Millisecond,
delay: 0 * time.Millisecond,
interval: Exponential{
Base: 200 * time.Millisecond,
Unit: time.Millisecond,
Initial: 200 * time.Millisecond,
Max: 200 * time.Millisecond,
},
wantErr: BackoffContextTimeoutExceeded,
wantDurations: []time.Duration{
200 * time.Millisecond,
},
wantEvents: []string{
try.CaseAfter,
try.CaseReturnFalse,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tc := tc
ds, afterFn := afterFnLogger()
events, tryFn := try.FnLogger(tc.delay, tc.trueAfterN)
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
defer cancel()
bo := NewBackoff(tc.interval, withAfterFunc(afterFn))
err := bo.try(ctx, tc.tries, tryFn, tc.initI, tc.initWait)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantDurations, ds.durations)
assert.Equal(t, tc.wantEvents, events.Events)
})
}
}
var defaultExampleCases = map[string]struct {
i int8
last time.Duration
want time.Duration
}{
"initial": {
i: 0,
last: 0 * time.Millisecond,
want: 500 * time.Millisecond,
},
"1": {
i: 1,
last: 500 * time.Millisecond,
want: 1000 * time.Millisecond,
},
"2": {
i: 2,
last: 1000 * time.Millisecond,
want: 2000 * time.Millisecond,
},
"3": {
i: 3,
last: 2000 * time.Millisecond,
want: 4000 * time.Millisecond,
},
"4": {
i: 4,
last: 4000 * time.Millisecond,
want: 8000 * time.Millisecond,
},
"5": {
i: 5,
last: 8000 * time.Millisecond,
want: 16000 * time.Millisecond,
},
"6": {
i: 6,
last: 16000 * time.Millisecond,
want: 20000 * time.Millisecond,
},
"7": {
i: 7,
last: 20000 * time.Millisecond,
want: 20000 * time.Millisecond,
},
"i=0 is always initial value": {
i: 0,
last: 500 * time.Millisecond,
want: 500 * time.Millisecond,
},
"i=MaxInt8 is always max": {
i: math.MaxInt8,
last: 0 * time.Millisecond,
want: DefaultBinaryExponential().Max,
},
}
func Test_DefaultBinaryExponential_NextShouldFollowHappyPath(t *testing.T) {
dbe := DefaultBinaryExponential()
for name, tc := range defaultExampleCases {
t.Run(name, func(t *testing.T) {
tc := tc
got := dbe.Next(tc.i, tc.last)
assert.Equal(t, tc.want, got)
})
}
}
func Test_DefaultBinaryExponentialJitter_NextShouldFollowHappyPath(t *testing.T) {
dbej, err := DefaultBinaryExponentialJitter()
require.NoError(t, err)
for name, tc := range defaultExampleCases {
t.Run(name, func(t *testing.T) {
tc := tc
got := dbej.Next(tc.i, tc.last)
wantMin := tc.want - dbej.JitterMax
wantMax := tc.want + dbej.JitterMax
assert.True(t, wantMin <= got && got <= wantMax)
})
}
}
func Test_Exponential_Base3(t *testing.T) {
t.Parallel()
e := Exponential{
Base: 3 * time.Second,
Unit: time.Second,
Initial: 1 * time.Second,
Max: 30 * time.Second,
}
var cases = map[string]struct {
i int8
last time.Duration
want time.Duration
}{
"initial": {
i: 0,
last: 0 * time.Millisecond,
want: 1000 * time.Millisecond,
},
"1": {
i: 1,
last: 1000 * time.Millisecond,
want: 3000 * time.Millisecond,
},
"2": {
i: 2,
last: 3000 * time.Millisecond,
want: 9000 * time.Millisecond,
},
"3": {
i: 3,
last: 9000 * time.Millisecond,
want: 27000 * time.Millisecond,
},
"4": {
i: 4,
last: 27000 * time.Millisecond,
want: 30000 * time.Millisecond,
},
"5": {
i: 5,
last: 30000 * time.Millisecond,
want: 30000 * time.Millisecond,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tc := tc
got := e.Next(tc.i, tc.last)
assert.Equal(t, tc.want, got)
})
}
}
func Test_Exponential_Base3Initial0IsAlwaysZero(t *testing.T) {
t.Parallel()
e := Exponential{
Base: 3 * time.Second,
Unit: time.Second,
Initial: 0 * time.Second,
Max: 30 * time.Second,
}
for i := 0; i < 7; i++ {
t.Run(fmt.Sprintf("Iteration %d", i), func(t *testing.T) {
i := i
got := e.Next(int8(i), 0)
assert.Equal(t, time.Duration(0), got)
})
}
}
func Test_DefaultBinaryExponentialJitter_RandomInputNextShouldBeWithinRange(t *testing.T) {
dbej, err := DefaultBinaryExponentialJitter()
require.NoError(t, err)
var maxI int8 = 20
minWant := time.Duration(0)
maxWant := dbej.Max + dbej.JitterMax
for iteration := 0; iteration < 1000; iteration++ {
i := int8(rand.Intn(int(maxI)))
last := time.Duration(rand.Int63n(int64(dbej.JitterMax)))
got := dbej.Next(i, last)
assert.True(t, minWant <= got && got <= maxWant,
"Next(%d, %s) got %s is not in range %s and %s",
i, last, got, minWant, maxWant)
}
}