-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretry_test.go
80 lines (77 loc) · 2.15 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
package que
import (
"testing"
"time"
)
var intervalCases = []struct {
name string
retryPolicy RetryPolicy
min []time.Duration
max []time.Duration
retryCount int32
}{
{
name: "default",
},
{
name: "default but MaxRetryCount=3",
retryPolicy: RetryPolicy{
MaxRetryCount: 3,
},
min: []time.Duration{10 * time.Second, 10 * time.Second, 10 * time.Second},
max: []time.Duration{10 * time.Second, 10 * time.Second, 10 * time.Second},
retryCount: 3,
},
{
name: "default MaxInterval",
retryPolicy: RetryPolicy{
InitialInterval: 8 * time.Second,
MaxInterval: 24 * time.Second,
NextIntervalMultiplier: 2.0,
MaxRetryCount: 4,
},
min: []time.Duration{8 * time.Second, 16 * time.Second, 24 * time.Second, 24 * time.Second},
max: []time.Duration{8 * time.Second, 16 * time.Second, 24 * time.Second, 24 * time.Second},
retryCount: 4,
},
{
name: "default IntervalRandomPercent",
retryPolicy: RetryPolicy{
InitialInterval: 10 * time.Second,
NextIntervalMultiplier: 2.0,
IntervalRandomPercent: 20,
MaxRetryCount: 4,
},
min: []time.Duration{8 * time.Second, 16 * time.Second, 32 * time.Second, 64 * time.Second},
max: []time.Duration{12 * time.Second, 24 * time.Second, 48 * time.Second, 96 * time.Second},
retryCount: 4,
},
}
func TestNextInterval(t *testing.T) {
for i := 0; i < 100; i++ {
for _, tc := range intervalCases {
policy := tc.retryPolicy
var retryCount int32
for i := range tc.min {
interval, ok := policy.NextInterval(int32(i))
if !ok {
t.Fatalf("%s(%v): want interval in [%v, %v] but retry finished", tc.name, i, tc.min[i], tc.max[i])
}
if !(interval >= tc.min[i] && interval <= tc.max[i]) {
t.Fatalf("%s(%v): want interval in [%v, %v] but get %v", tc.name, i, tc.min[i], tc.max[i], interval)
}
retryCount++
}
for {
if _, ok := policy.NextInterval(retryCount); ok {
retryCount++
} else {
break
}
}
if retryCount != tc.retryCount {
t.Fatalf("%s: want retry count is %v but %v", tc.name, tc.retryCount, retryCount)
}
}
}
}