-
Notifications
You must be signed in to change notification settings - Fork 8
/
retry_test.go
123 lines (92 loc) · 3.32 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
package httpbackoff
// See test_setup_test.go for test setup...
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Stub server to send three 5xx failure status code responses
// before finally sending a 200 resp. Make sure the retry
// library retries until it gets the 200 resp.
func TestRetry5xx(t *testing.T) {
handler.QueueResponse(500)
handler.QueueResponse(501)
handler.QueueResponse(502)
handler.QueueResponse(200)
handler.QueueResponse(502)
// defer clean up in case we have t.Fatalf calls
defer handler.ClearResponseQueue()
resp, _, err := testClient.Get("http://localhost:50849/TestRetry5xx")
if err != nil {
t.Fatalf("%v\n", err)
}
if statusCode := resp.StatusCode; statusCode != 200 {
t.Errorf("API retry logic broken - expected response code 200, but received code %v...\n", statusCode)
}
}
// Check that when retries run out, the last temporary
// error is returned, even if htat was a 500.
func TestRetry5xxAndFail(t *testing.T) {
testClient.BackOffSettings.InitialInterval = 10 * time.Millisecond
handler.QueueResponse(500)
handler.QueueResponse(500)
handler.QueueResponse(500)
handler.QueueResponse(500)
handler.QueueResponse(500)
// defer clean up in case we have t.Fatalf calls
defer handler.ClearResponseQueue()
resp, _, err := testClient.Get("http://localhost:50849/TestRetry5xx")
if assert.Error(t, err) {
herr, ok := err.(BadHttpResponseCode)
require.True(t, ok)
require.Equal(t, herr.HttpResponseCode, 500)
}
if statusCode := resp.StatusCode; statusCode != 500 {
t.Errorf("API retry logic broken - expected response code 500, but received code %v...\n", statusCode)
}
}
// Want to make sure 4xx errors (except 429) are not retried...
func TestRetry4xx(t *testing.T) {
handler.QueueResponse(409)
handler.QueueResponse(200)
// defer clean up in case we have t.Fatalf calls
defer handler.ClearResponseQueue()
resp, _, err := testClient.Get("http://localhost:50849/TestRetry4xx")
// NB: this is == not != since we *want* an error
if err == nil {
t.Errorf("Was expecting Get call to return an error, due to 409 status code\n")
}
if statusCode := resp.StatusCode; statusCode != 409 {
t.Errorf("API retry logic broken - expected response code 409, but received code %v...\n", statusCode)
}
}
// Want to make sure 429 is retried...
func TestRetry429(t *testing.T) {
handler.QueueResponse(429)
handler.QueueResponse(200)
// defer clean up in case we have t.Fatalf calls
defer handler.ClearResponseQueue()
resp, attempts, err := testClient.Get("http://localhost:50849/TestRetry429")
if err != nil {
t.Errorf("Unexpected error: %v\n", err)
}
if statusCode := resp.StatusCode; statusCode != 200 {
t.Errorf("API retry logic broken - expected response code 200, but received code %v...\n", statusCode)
}
if attempts != 2 {
t.Errorf("Was expecting 2 retry attempts, but had %v...\n", attempts)
}
}
// Test network failures get retried
func TestNetworkFailure(t *testing.T) {
// bad port
_, attempts, err := testClient.Get("http://localhost:40849/TestNetworkFailure")
// NB: this is == not != since we *want* an error
if err == nil {
t.Errorf("Was expecting Get call to return an error, due to 409 status code\n")
}
if attempts < 4 {
t.Errorf("Was expecting at least 4 retry attempts, but were only %v...\n", attempts)
}
}