-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretry_test.go
282 lines (256 loc) · 6.95 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
package retry
import (
"context"
"errors"
"fmt"
"math/rand"
"net/http"
"reflect"
"sync"
"testing"
"time"
)
var errTest = errors.New("test error")
func TestBackoffBacksOff(t *testing.T) {
t.Run("r.Run", func(t *testing.T) {
tries := 0
start := time.Now()
var last time.Time
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.Run(func() error {
tries++
last = time.Now()
return errTest
})
if tries != 5 {
t.Errorf("expected 5 tries, got %d", tries)
}
if err != errTest {
t.Errorf("err should equal errTest, got: %v", err)
}
max := 5 * (50 + 50) * time.Millisecond
if last.Sub(start) > max {
t.Errorf("should have taken less than %v, took %d", max, last.Sub(start).Nanoseconds()/1000000)
}
})
t.Run("r.RunContext", func(t *testing.T) {
tries := 0
start := time.Now()
var last time.Time
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.RunContext(context.Background(), func(ctx context.Context) error {
tries++
last = time.Now()
return errTest
})
if tries != 5 {
t.Errorf("expected 5 tries, got %d", tries)
}
if err != errTest {
t.Errorf("err should equal errTest, got: %v", err)
}
max := 5 * (50 + 50) * time.Millisecond
if last.Sub(start) > max {
t.Errorf("should have taken less than %v, took %d", max, last.Sub(start).Nanoseconds()/1000000)
}
})
}
func TestEventualSuccessSucceedsTransparently(t *testing.T) {
t.Run("r.Run", func(t *testing.T) {
tries := 0
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.Run(func() error {
tries++
if tries == 2 {
return nil
}
return errTest
})
if tries != 2 {
t.Errorf("expected 2 tries, got %d", tries)
}
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
})
t.Run("r.RunContext", func(t *testing.T) {
tries := 0
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.RunContext(context.Background(), func(ctx context.Context) error {
tries++
if tries == 2 {
return nil
}
return errTest
})
if tries != 2 {
t.Errorf("expected 2 tries, got %d", tries)
}
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
})
}
func TestRunContextExitsEarlyWhenContextCanceled(t *testing.T) {
var err error
var wg sync.WaitGroup
tries := 0
ctx, canceler := context.WithCancel(context.Background())
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
wg.Add(1)
go func() {
err = retrier.RunContext(ctx, func(ctx context.Context) error {
tries++
return errTest
})
wg.Done()
}()
time.Sleep(200 * time.Millisecond)
canceler()
wg.Wait()
if tries < 1 {
t.Errorf("expected at least one retry, got %d", tries)
}
if tries >= 100 {
t.Error("reached MaxTries, but should not have")
}
if err != errTest {
t.Errorf("err should equal errTest, got: %v", err)
}
}
func TestStopStopsImmediately(t *testing.T) {
t.Run("r.Run", func(t *testing.T) {
tries := 0
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.Run(func() error {
tries++
return Stop(errTest)
})
if tries != 1 {
t.Errorf("expected 1 tries, got %d", tries)
}
if err != errTest {
t.Errorf("err should equal errTest, got: %v", err)
}
})
t.Run("r.RunContext", func(t *testing.T) {
tries := 0
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.RunContext(context.Background(), func(ctx context.Context) error {
tries++
return Stop(errTest)
})
if tries != 1 {
t.Errorf("expected 1 tries, got %d", tries)
}
if err != errTest {
t.Errorf("err should equal errTest, got: %v", err)
}
})
}
func TestRetrierGetsDefaultsIfLessThanZero(t *testing.T) {
r := NewRetrier(-1, -1, -1)
if r.maxTries != DefaultMaxTries {
t.Errorf("expected maxTries to be %d, got %d", DefaultMaxTries, r.maxTries)
}
if r.initialDelay != DefaultInitialDelay {
t.Errorf("expected initialDelay to be %d, got %d", DefaultInitialDelay, r.initialDelay)
}
if r.maxDelay != DefaultMaxDelay {
t.Errorf("expected maxDelay to be %d, got %d", DefaultMaxDelay, r.maxDelay)
}
}
func TestTerminalErrorImplementsError(t *testing.T) {
testError := fmt.Errorf("EG 8=D")
fatalError := Stop(testError)
if fatalError.Error() != testError.Error() {
t.Errorf("expected fatalError.Error() to be %s, got %s", testError.Error(), fatalError.Error())
}
}
type myErrorType struct{}
func (m myErrorType) Error() string { return "myErrorType" }
func TestTerminalErrorRetainsOriginalError(t *testing.T) {
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
tries := 0
err := retrier.Run(func() error {
tries++
return Stop(myErrorType{})
})
if tries != 1 {
t.Errorf("expected 1 tries, got %d", tries)
}
errType := reflect.TypeOf(err).String()
if errType != "retry.myErrorType" {
t.Errorf("expected retry.myErrorType, got %s", errType)
}
}
func ExampleRetrier_Run() {
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
err := retrier.Run(func() error {
resp, err := http.Get("http://golang.org")
switch {
case err != nil:
return err
case resp.StatusCode == 0 || resp.StatusCode >= 500:
return fmt.Errorf("Retryable HTTP status: %s", http.StatusText(resp.StatusCode))
case resp.StatusCode != 200:
return Stop(fmt.Errorf("Non-retryable HTTP status: %s", http.StatusText(resp.StatusCode)))
}
return nil
})
fmt.Println(err)
}
func ExampleRetrier_RunContext_output() {
retrier := NewRetrier(5, 50*time.Millisecond, 50*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
err := retrier.RunContext(ctx, func(ctx context.Context) error {
req, _ := http.NewRequest("GET", "http://golang.org/notfastenough", nil)
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("OMG AWFUL CODE %d", resp.StatusCode)
// or decide not to retry
}
return nil
})
fmt.Println(err)
// Output: Get "http://golang.org/notfastenough": context deadline exceeded
}
// Ref: https://github.com/flowchartsman/retry/issues/4
func TestBackoffPanicFix(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("Backoff underflows and panics")
}
}()
initialDelay := 500 * time.Millisecond
maxDelay := 1 * time.Millisecond
randSource := rand.New(rand.NewSource(time.Now().UnixNano()))
for attempts := 0; attempts < 100; attempts++ {
_ = getnextBackoff(attempts, initialDelay, maxDelay, randSource)
}
}
// Ref: https://github.com/flowchartsman/retry/issues/7
func TestZeroValueRetrierDoesNotPanic(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
defer func() {
if r := recover(); r != nil {
t.Error("Zero-value retrier panics when used")
}
}()
tries := 0
r := Retrier{}
r.Run(func() error {
tries++
if tries > 1 {
return Stop(errors.New("nope"))
}
return errors.New("nope")
})
}