-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackoff_test.go
151 lines (141 loc) · 4.11 KB
/
backoff_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
package retry
import (
"testing"
"time"
)
func TestFixedDelayBackoff(t *testing.T) {
var testCases = []struct {
count int
delay time.Duration
}{
{0, 0 * time.Millisecond},
{1, 100 * time.Millisecond},
{2, 200 * time.Millisecond},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := FixedDelayBackoff(c.delay)
if backoff.GetDelay(ctx) != c.delay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
}
func TestDefaultFixedDelayBackoff(t *testing.T) {
var testCases = []struct {
count int
delay time.Duration
}{
{0, 1 * time.Second},
{1, 1 * time.Second},
{2, 1 * time.Second},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := DefaultFixedDelayBackoff()
if backoff.GetDelay(ctx) != c.delay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
}
func TestUniformRandomBackoff(t *testing.T) {
var testCases = []struct {
count int
delay time.Duration
}{
{0, 0 * time.Millisecond},
{1, 100 * time.Millisecond},
{2, 200 * time.Millisecond},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := UniformRandomBackoff(c.delay)
if backoff.GetDelay(ctx) > c.delay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
// Safe bounds
for i, maxDelay := range []time.Duration{0, -1, -100} {
ctx := Context{RetryCount: i + 1}
backoff := UniformRandomBackoff(maxDelay)
if backoff.GetDelay(ctx) != 0*time.Millisecond {
t.Error("invalid return value for GetDelay(ctx)")
}
}
}
func TestDefaultUniformRandomBackoff(t *testing.T) {
var testCases = []struct {
count int
delay time.Duration
}{
{0, 1 * time.Second},
{1, 1 * time.Second},
{2, 1 * time.Second},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := DefaultUniformRandomBackoff()
if backoff.GetDelay(ctx) > c.delay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
}
func TestExponentialDelayBackoffBackoff(t *testing.T) {
var testCases = []struct {
count int
multiplier float64
delay time.Duration
expDelay time.Duration
}{
{1, 0, 0 * time.Millisecond, 0 * time.Millisecond},
{1, 1, 100 * time.Millisecond, 100 * time.Millisecond},
{1, 2, 100 * time.Millisecond, 100 * time.Millisecond},
{1, 2, 200 * time.Millisecond, 200 * time.Millisecond},
{2, 0, 0 * time.Millisecond, 0 * time.Millisecond},
{2, 1, 100 * time.Millisecond, 100 * time.Millisecond},
{2, 2, 100 * time.Millisecond, 200 * time.Millisecond},
{2, 2, 200 * time.Millisecond, 400 * time.Millisecond},
{3, 0, 0 * time.Millisecond, 0 * time.Millisecond},
{3, 1, 100 * time.Millisecond, 100 * time.Millisecond},
{3, 2, 100 * time.Millisecond, 400 * time.Millisecond},
{3, 2, 200 * time.Millisecond, 800 * time.Millisecond},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := ExponentialDelayBackoff(c.delay, c.multiplier)
if backoff.GetDelay(ctx) != c.expDelay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
e := NewExecutor().WithRetries(5).WithBackoff(ExponentialDelayBackoff(10*time.Millisecond, 2))
if err := e.Execute(failTimes(5)); err != nil {
t.Error("error found and not expected")
}
if e.Context.RetryCount != 5 {
t.Error("invalid value for RetryCount", e.Context.RetryCount)
}
}
func TestBoundedDelayBackoff(t *testing.T) {
var testCases = []struct {
count int
minDelay time.Duration
maxDelay time.Duration
}{
{0, 0 * time.Millisecond, 0 * time.Millisecond},
{1, 100 * time.Millisecond, 100 * time.Millisecond},
{2, 100 * time.Millisecond, 200 * time.Millisecond},
{3, 200 * time.Millisecond, 400 * time.Millisecond},
}
for _, c := range testCases {
ctx := Context{RetryCount: c.count}
backoff := BoundedDelayBackoff(c.minDelay, c.maxDelay)
if backoff.GetDelay(ctx) < c.minDelay || backoff.GetDelay(ctx) > c.maxDelay {
t.Error("invalid return value for GetDelay(ctx)")
}
}
// Safe bounds
ctx := Context{RetryCount: 1}
backoff := BoundedDelayBackoff(200*time.Millisecond, 100*time.Millisecond)
if backoff.GetDelay(ctx) != 200*time.Millisecond {
t.Error("invalid return value for GetDelay(ctx)")
}
}