-
Notifications
You must be signed in to change notification settings - Fork 30
/
backoff_test.go
226 lines (183 loc) · 4.61 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
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
package retry_test
import (
"context"
"testing"
"time"
"github.com/sethvargo/go-retry"
)
func ExampleBackoffFunc() {
ctx := context.Background()
// Example backoff middleware that adds the provided duration t to the result.
withShift := func(t time.Duration, next retry.Backoff) retry.BackoffFunc {
return func() (time.Duration, bool) {
val, stop := next.Next()
if stop {
return 0, true
}
return val + t, false
}
}
// Middlewrap wrap another backoff:
b := retry.NewFibonacci(1 * time.Second)
b = withShift(5*time.Second, b)
if err := retry.Do(ctx, b, func(ctx context.Context) error {
// Actual retry logic here
return nil
}); err != nil {
// handle error
}
}
func TestWithJitter(t *testing.T) {
t.Parallel()
for i := 0; i < 100_000; i++ {
b := retry.WithJitter(250*time.Millisecond, retry.BackoffFunc(func() (time.Duration, bool) {
return 1 * time.Second, false
}))
val, stop := b.Next()
if stop {
t.Errorf("should not stop")
}
if min, max := 750*time.Millisecond, 1250*time.Millisecond; val < min || val > max {
t.Errorf("expected %v to be between %v and %v", val, min, max)
}
}
}
func ExampleWithJitter() {
ctx := context.Background()
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithJitter(1*time.Second, b)
if err := retry.Do(ctx, b, func(_ context.Context) error {
// TODO: logic here
return nil
}); err != nil {
// handle error
}
}
func TestWithJitterPercent(t *testing.T) {
t.Parallel()
for i := 0; i < 100_000; i++ {
b := retry.WithJitterPercent(5, retry.BackoffFunc(func() (time.Duration, bool) {
return 1 * time.Second, false
}))
val, stop := b.Next()
if stop {
t.Errorf("should not stop")
}
if min, max := 950*time.Millisecond, 1050*time.Millisecond; val < min || val > max {
t.Errorf("expected %v to be between %v and %v", val, min, max)
}
}
}
func ExampleWithJitterPercent() {
ctx := context.Background()
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithJitterPercent(5, b)
if err := retry.Do(ctx, b, func(_ context.Context) error {
// TODO: logic here
return nil
}); err != nil {
// handle error
}
}
func TestWithMaxRetries(t *testing.T) {
t.Parallel()
b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) {
return 1 * time.Second, false
}))
// First 3 attempts succeed
for i := 0; i < 3; i++ {
val, stop := b.Next()
if stop {
t.Errorf("should not stop")
}
if val != 1*time.Second {
t.Errorf("expected %v to be %v", val, 1*time.Second)
}
}
// Now we stop
val, stop := b.Next()
if !stop {
t.Errorf("should stop")
}
if val != 0 {
t.Errorf("expected %v to be %v", val, 0)
}
}
func ExampleWithMaxRetries() {
ctx := context.Background()
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithMaxRetries(3, b)
if err := retry.Do(ctx, b, func(_ context.Context) error {
// TODO: logic here
return nil
}); err != nil {
// handle error
}
}
func TestWithCappedDuration(t *testing.T) {
t.Parallel()
b := retry.WithCappedDuration(3*time.Second, retry.BackoffFunc(func() (time.Duration, bool) {
return 5 * time.Second, false
}))
val, stop := b.Next()
if stop {
t.Errorf("should not stop")
}
if val != 3*time.Second {
t.Errorf("expected %v to be %v", val, 3*time.Second)
}
}
func ExampleWithCappedDuration() {
ctx := context.Background()
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithCappedDuration(3*time.Second, b)
if err := retry.Do(ctx, b, func(_ context.Context) error {
// TODO: logic here
return nil
}); err != nil {
// handle error
}
}
func TestWithMaxDuration(t *testing.T) {
t.Parallel()
b := retry.WithMaxDuration(250*time.Millisecond, retry.BackoffFunc(func() (time.Duration, bool) {
return 1 * time.Second, false
}))
// Take once, within timeout.
val, stop := b.Next()
if stop {
t.Error("should not stop")
}
if val > 250*time.Millisecond {
t.Errorf("expected %v to be less than %v", val, 250*time.Millisecond)
}
time.Sleep(200 * time.Millisecond)
// Take again, remainder contines
val, stop = b.Next()
if stop {
t.Error("should not stop")
}
if val > 50*time.Millisecond {
t.Errorf("expected %v to be less than %v", val, 50*time.Millisecond)
}
time.Sleep(50 * time.Millisecond)
// Now we stop
val, stop = b.Next()
if !stop {
t.Errorf("should stop")
}
if val != 0 {
t.Errorf("expected %v to be %v", val, 0)
}
}
func ExampleWithMaxDuration() {
ctx := context.Background()
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithMaxDuration(5*time.Second, b)
if err := retry.Do(ctx, b, func(_ context.Context) error {
// TODO: logic here
return nil
}); err != nil {
// handle error
}
}