-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
647 lines (529 loc) · 15.9 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package httpsling_test
import (
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/theopenlane/httpsling"
"github.com/theopenlane/httpsling/httptestutil"
)
func TestExponentialBackoff_Backoff(t *testing.T) {
tests := []struct {
name string
backoff ExponentialBackoff
expected [5]time.Duration
expectedJitter float64
}{
{
name: "zero base delay",
backoff: ExponentialBackoff{
BaseDelay: 0,
Multiplier: 1,
Jitter: 1,
MaxDelay: time.Second,
},
expected: [5]time.Duration{0, 0, 0, 0, 0},
},
{
name: "zero multiplier",
backoff: ExponentialBackoff{
BaseDelay: time.Second,
Multiplier: 0,
Jitter: .2,
MaxDelay: time.Minute,
},
expected: [5]time.Duration{time.Second, time.Second, time.Second, time.Second, time.Second},
expectedJitter: 0.2,
},
{
name: "zero jitter",
backoff: ExponentialBackoff{
BaseDelay: 1,
Multiplier: 2,
Jitter: 0,
MaxDelay: time.Second,
},
expected: [5]time.Duration{1, 2, 4, 8, 16},
},
{
name: "zero max",
backoff: ExponentialBackoff{
BaseDelay: 1,
Multiplier: 2,
Jitter: 0,
MaxDelay: 0,
},
expected: [5]time.Duration{1, 2, 4, 8, 16},
},
{
name: "constant",
backoff: ExponentialBackoff{
BaseDelay: 30,
Multiplier: 0,
Jitter: 0,
MaxDelay: time.Second,
},
expected: [5]time.Duration{30, 30, 30, 30, 30},
},
{
name: "max",
backoff: ExponentialBackoff{
BaseDelay: 30,
Multiplier: 2,
Jitter: 0,
MaxDelay: 100,
},
expected: [5]time.Duration{30, 60, 100, 100, 100},
},
{
name: "jitter",
backoff: ExponentialBackoff{
BaseDelay: time.Second,
Multiplier: 2,
Jitter: .1,
MaxDelay: time.Minute,
},
expected: [5]time.Duration{1 * time.Second, 2 * time.Second, 4 * time.Second, 8 * time.Second, 16 * time.Second},
expectedJitter: 0.1,
},
{
name: "base more than max",
backoff: ExponentialBackoff{
BaseDelay: 2 * time.Second,
Multiplier: 0,
Jitter: 0,
MaxDelay: time.Second,
},
expected: [5]time.Duration{time.Second, time.Second, time.Second, time.Second, time.Second},
},
{
name: "no delay",
backoff: *NoBackoff(),
expected: [5]time.Duration{0, 0, 0, 0, 0},
},
{
name: "constant delay",
backoff: *ConstantBackoff(time.Second),
expected: [5]time.Duration{time.Second, time.Second, time.Second, time.Second, time.Second},
},
{
name: "constant delay with jitter",
backoff: *ConstantBackoffWithJitter(time.Second),
expected: [5]time.Duration{time.Second, time.Second, time.Second, time.Second, time.Second},
expectedJitter: 0.2,
},
{
name: "jitter wont go over max",
backoff: ExponentialBackoff{
BaseDelay: time.Second,
Jitter: .2,
MaxDelay: time.Second,
},
expected: [5]time.Duration{900 * time.Millisecond, 900 * time.Millisecond, 900 * time.Millisecond, 900 * time.Millisecond, 900 * time.Millisecond},
expectedJitter: 0.1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var results [5]time.Duration
for i := 0; i < 5; i++ {
results[i] = test.backoff.Backoff(i + 1)
}
if test.expectedJitter > 0 {
for i, duration := range test.expected {
assert.InDelta(t, duration, results[i], float64(duration)*test.backoff.Jitter)
}
assert.NotEqual(t, test.expected, results, "shouldn't be exactly equal, missing the jitter")
} else {
assert.Equal(t, test.expected, results)
}
if test.backoff.MaxDelay > 0 {
for _, duration := range results {
assert.LessOrEqual(t, duration, test.backoff.MaxDelay)
}
}
})
}
}
type netError struct {
timeout bool
}
func (m *netError) Error() string {
return "neterror"
}
func (m *netError) Timeout() bool {
return m.timeout
}
func (m *netError) Temporary() bool {
return false
}
func TestDefaultShouldRetry(t *testing.T) {
assert.True(t, DefaultShouldRetry(1, nil, nil, &net.OpError{
Op: "accept",
Err: syscall.ECONNRESET,
}))
assert.True(t, DefaultShouldRetry(1, nil, nil, &net.OpError{
Op: "accept",
Err: syscall.ECONNABORTED,
}))
assert.True(t, DefaultShouldRetry(1, nil, nil, syscall.EPIPE))
assert.True(t, DefaultShouldRetry(1, nil, nil, &netError{timeout: true}))
assert.False(t, DefaultShouldRetry(1, nil, nil, &netError{}))
assert.False(t, DefaultShouldRetry(1, nil, MockResponse(400), nil)) // nolint: bodyclose
assert.True(t, DefaultShouldRetry(1, nil, MockResponse(500), nil)) // nolint: bodyclose
assert.False(t, DefaultShouldRetry(1, nil, MockResponse(501), nil)) // nolint: bodyclose
assert.True(t, DefaultShouldRetry(1, nil, MockResponse(502), nil)) // nolint: bodyclose
assert.True(t, DefaultShouldRetry(1, nil, MockResponse(429), nil)) // nolint: bodyclose
}
func TestOnlyIdempotentShouldRetry(t *testing.T) {
tests := []struct {
method string
expected bool
}{
{http.MethodGet, true},
{http.MethodOptions, true},
{http.MethodHead, true},
{http.MethodTrace, true},
{http.MethodPost, false},
{http.MethodPut, false},
{http.MethodPatch, false},
{http.MethodDelete, false},
}
for _, test := range tests {
t.Run(test.method, func(t *testing.T) {
req, err := http.NewRequestWithContext(context.Background(), test.method, "http://test.com", nil)
require.NoError(t, err)
assert.Equal(t, test.expected, OnlyIdempotentShouldRetry(1, req, nil, nil))
})
}
}
func TestAllRetryers(t *testing.T) {
r := AllRetryers(ShouldRetryerFunc(DefaultShouldRetry), ShouldRetryerFunc(OnlyIdempotentShouldRetry))
// false + false = false
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://test.com", nil)
require.NoError(t, err)
assert.False(t, r.ShouldRetry(1, req, MockResponse(400), nil)) // nolint: bodyclose
// true + false = false
assert.False(t, r.ShouldRetry(1, req, MockResponse(500), nil)) // nolint: bodyclose
// false + true = false
req, err = http.NewRequestWithContext(context.Background(), http.MethodGet, "http://test.com", nil)
require.NoError(t, err)
assert.False(t, r.ShouldRetry(1, req, MockResponse(400), nil)) // nolint: bodyclose
// true + true = true
assert.True(t, r.ShouldRetry(1, req, MockResponse(500), nil)) // nolint: bodyclose
}
func TestRetry(t *testing.T) {
s := httptest.NewServer(MockHandler(500, Header(HeaderContentType, ContentTypeText)))
defer s.Close()
r := httptestutil.Requester(s, Retry(&RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{
BaseDelay: 50 * time.Millisecond,
Multiplier: 2,
Jitter: 0,
MaxDelay: time.Second,
},
}))
i := httptestutil.Inspect(s)
var (
err error
out string
resp *http.Response
)
t0 := time.Now()
done := make(chan bool)
go func() {
// spawn a go routine to call the client. this will block until all the retries
// finish.
resp, err = r.Receive(&out) // nolint: bodyclose
// capture the response, and send a signal that the client finished.
done <- true
}()
// total requests detected
var count int
// how long was the time between each request.
var waits []time.Duration
loop:
for {
// on each loop, wait for the inspector to send a request on its channel.
// break out of the loop if the requester goroutine reported that the client
// call returned, or if we time out.
select {
case <-i.Exchanges:
count++
if count > 1 {
// keep track of the waits between retries
t1 := time.Now()
waits = append(waits, t1.Sub(t0))
t0 = t1
}
case <-time.After(time.Second):
require.Fail(t, "timeout", "after %v requests", count)
case <-done:
break loop
}
}
assert.NoError(t, err)
defer resp.Body.Close()
if assert.NotNil(t, out) {
assert.Equal(t, 500, resp.StatusCode)
}
assert.Equal(t, 4, count)
require.Len(t, waits, 3)
t.Log(waits)
assert.InDelta(t, 50*time.Millisecond, waits[0], float64(10*time.Millisecond))
assert.InDelta(t, 100*time.Millisecond, waits[1], float64(10*time.Millisecond))
assert.InDelta(t, 200*time.Millisecond, waits[2], float64(10*time.Millisecond))
}
func TestRetryPost(t *testing.T) {
s := httptest.NewServer(MockHandler(500))
defer s.Close()
r := httptestutil.Requester(s, Retry(&RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{BaseDelay: 0},
}))
i := httptestutil.Inspect(s)
expectBody := true
// consumes all pending requests in the inspector, asserts they have the right request and body,
// and returns how many there were.
count := func(t *testing.T) int {
var count int
for {
e := i.NextExchange()
if e == nil {
break
}
count++
assert.Equal(t, "POST", e.Request.Method)
if expectBody {
assert.Equal(t, "fudge", e.RequestBody.String())
}
}
return count
}
// most body types will be automatically wrapped with an appropriate GetBody function, so they can
// be correctly replayed.
resp, err := r.Receive(Post(), Body("fudge"))
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 500, resp.StatusCode)
assert.Equal(t, 4, count(t))
// This type of body can't be converted, so the request's GetBody function will be nil.
// This will not be retried.
resp, err = r.Receive(Post(), Body(&dummyReader{next: strings.NewReader("fudge")}))
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 500, resp.StatusCode)
assert.Equal(t, 1, count(t))
// http.NoBody is a special case. It's a non-nil sentinel value indicating the request has
// no body. We should be able to retry this, even though GetBody will be nil.
expectBody = false
resp, err = r.Receive(Post(), Body(http.NoBody))
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 500, resp.StatusCode)
assert.Equal(t, 4, count(t))
}
type dummyReader struct {
next io.Reader
}
func (d *dummyReader) Read(p []byte) (n int, err error) {
return d.next.Read(p)
}
func TestRetryRespDrained(t *testing.T) {
// when retrying a request, the response body of the last attempt must be
// fully drained first, or there will be a leak.
s := httptest.NewServer(MockHandler(500, Body("fudge")))
defer s.Close()
var responses []*http.Response
r := httptestutil.Requester(s, Retry(&RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{BaseDelay: 0},
}), Middleware(func(doer Doer) Doer {
return DoerFunc(func(req *http.Request) (*http.Response, error) {
resp, err := doer.Do(req)
responses = append(responses, resp)
return resp, err
})
}))
var out string
resp, err := r.Receive(&out)
assert.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 4, len(responses))
assert.Equal(t, "fudge", out)
// all the response bodies should have been drained
for i, resp := range responses {
t.Log("checking response", i)
require.NotNil(t, resp)
bytes := make([]byte, 39)
_, err := resp.Body.Read(bytes)
assert.EqualError(t, err, "http: read on closed response body")
}
}
func TestRetryCancelContext(t *testing.T) {
// context cancellation can be used to abort retries
s := httptest.NewServer(MockHandler(500, Body("fudge")))
defer s.Close()
r := httptestutil.Requester(s, Retry(&RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{BaseDelay: 2 * time.Second},
}))
ctx, cancelFunc := context.WithCancel(context.Background())
var err error
done := make(chan bool)
go func() {
_, err = r.ReceiveWithContext(ctx, nil) // nolint: bodyclose
done <- true
}()
cancelFunc()
select {
case <-time.After(time.Second):
require.Fail(t, "timed out")
case <-done:
}
require.ErrorIs(t, err, context.Canceled)
}
func TestRetryShouldRetry(t *testing.T) {
// test a custom ShouldRetry function. also test that Retry calls the ShouldRetry function
// with the right args.
var srvCount int
s := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
srvCount++
writer.WriteHeader(501 + srvCount)
writer.Write([]byte("fudge")) // nolint: errcheck
}))
defer s.Close()
var (
count int
attempts []int
requests []*http.Request
responses []*http.Response
)
r := httptestutil.Requester(s, Retry(&RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{BaseDelay: 0},
ShouldRetry: ShouldRetryerFunc(func(attempt int, req *http.Request, resp *http.Response, err error) bool {
count++
attempts = append(attempts, attempt)
requests = append(requests, req)
responses = append(responses, resp) // nolint: bodyclose
return attempt != 3
}),
}))
resp, err := r.Receive(nil)
require.NoError(t, err)
defer resp.Body.Close()
// our should function should tell it stop after 3 attempts, not 4
assert.Equal(t, 3, count)
assert.Len(t, attempts, 3)
for i := 0; i < 3; i++ {
// attempts should be 1, 2, and 3
assert.Equal(t, i+1, attempts[i])
// requests and responses should be non nil
assert.NotNil(t, requests[i])
if assert.NotNil(t, responses[i]) {
// each response should have a different code: 502, 503, and 504
assert.Equal(t, 501+attempts[i], responses[i].StatusCode)
}
}
}
func TestRetrySuccess(t *testing.T) {
// if request succeeds, no retries
s := httptest.NewServer(MockHandler(200, Body("fudge")))
defer s.Close()
r := httptestutil.Requester(s, Retry(nil))
i := httptestutil.Inspect(s)
var out string
resp, err := r.Receive(&out)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, "fudge", out)
assert.Equal(t, 200, resp.StatusCode)
// it should not have retried, since the first attempt was a success
assert.Len(t, i.Drain(), 1)
}
// poisonedReader returns "fu" in the first call, and a connection
// reset error in the next call.
type poisonedReader struct {
remaining int
}
func (r *poisonedReader) Read(p []byte) (n int, err error) {
if r.remaining > 0 {
n = copy(p, "fu"[r.remaining:])
r.remaining -= n
return n, nil
}
return 0, &net.OpError{
Op: "accept",
Err: syscall.ECONNRESET,
}
}
func TestRetryReadResponse(t *testing.T) {
// optionally, Retry can retry the request if an error occurs in the middle
// of reading the response body. This is accomplished by having Retry
// read the entire response body into memory in the middleware. This is not
// not the default: when downloading a file or a large response, it may not
// be desirable to read the entire response into memory.
// to test, use a mock Doer which simulates a connection reset error halfway
// through reading the response body.
var count int
retryConfig := RetryConfig{
MaxAttempts: 4,
Backoff: &ExponentialBackoff{
BaseDelay: 1,
Multiplier: 1,
Jitter: 0,
MaxDelay: time.Second,
},
}
newRequester := func() *Requester {
r, err := New(
Retry(&retryConfig),
WithDoer(DoerFunc(func(req *http.Request) (*http.Response, error) {
count++
// I can't cause a real connection reset error using httptest, so I need to simulate
// it with a fake Doer. https://groups.google.com/g/golang-nuts/c/AtxmEDJ4zvc
if count > 2 {
// on the third attempt, just return a valid response
return MockResponse(200,
Body("fudge"),
Header(HeaderContentType, ContentTypeText)), nil
}
// return a response with a poisoned response body will will thrown an error after
// a few bytes
resp := MockResponse(200)
resp.Body = io.NopCloser(&poisonedReader{})
return resp, nil
})),
)
require.NoError(t, err)
return r
}
r := newRequester()
// without setting flag, it should fail after the first attempt.
// it will not be retried
resp, err := r.Receive(nil)
assert.ErrorIs(t, err, syscall.ECONNRESET)
assert.Equal(t, 1, count)
defer resp.Body.Close()
// now try the flag
count = 0
retryConfig.ReadResponse = true
r = newRequester()
var out string
resp, err = r.Receive(&out)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, "fudge", out)
assert.Equal(t, 200, resp.StatusCode)
// should have taken 3 tries
assert.Equal(t, 3, count)
}