-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdefault_test.go
More file actions
322 lines (318 loc) · 7.64 KB
/
default_test.go
File metadata and controls
322 lines (318 loc) · 7.64 KB
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
package retryhttp_test
import (
"net"
"net/http"
"testing"
"time"
"github.com/justinrixx/retryhttp"
)
func TestDefaultShouldRetryFn(t *testing.T) {
tests := []struct {
name string
attempt retryhttp.Attempt
want bool
}{
{
name: "should retry on dns error for a GET request",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodGet,
},
Err: &net.DNSError{
IsNotFound: true,
},
},
want: true,
},
{
name: "should retry on dns error for a POST request",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Err: &net.DNSError{
IsNotFound: true,
},
},
want: true,
},
{
name: "should retry idempotent requests that timed out",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodGet,
},
Err: &net.OpError{
Err: timeoutErr{},
},
},
want: true,
},
{
name: "should not retry non-idempotent requests that timed out",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Err: &net.OpError{
Err: timeoutErr{},
},
},
want: false,
},
{
name: "should recognize requests with idempotency key headers as idempotent",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
Header: http.Header{"Idempotency-Key": []string{"foobar"}},
},
Err: &net.OpError{
Err: timeoutErr{},
},
},
want: true,
},
{
name: "should retry on 429 status",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodGet,
},
Res: &http.Response{
StatusCode: http.StatusTooManyRequests,
},
},
want: true,
},
{
name: "should retry on 429 status even for non-idempotent methods",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Res: &http.Response{
StatusCode: http.StatusTooManyRequests,
},
},
want: true,
},
{
name: "should retry if retry-after header is present",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodGet,
},
Res: &http.Response{
StatusCode: http.StatusInternalServerError,
Header: http.Header{"Retry-After": []string{"3"}},
},
},
want: true,
},
{
name: "should retry if retry-after header is present even for non-idempotent methods",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Res: &http.Response{
StatusCode: http.StatusInternalServerError,
Header: http.Header{"Retry-After": []string{"3"}},
},
},
want: true,
},
{
name: "should not retry if status is not retryable even if guessed idempotent",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodGet,
},
Res: &http.Response{
StatusCode: http.StatusInternalServerError,
},
},
want: false,
},
{
name: "should not retry if request is guessed non-idempotent, even if status code is retryable",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Res: &http.Response{
StatusCode: http.StatusServiceUnavailable,
},
},
want: false,
},
{
name: "should not retry if request is guessed non-idempotent, or status code is not retryable",
attempt: retryhttp.Attempt{
Count: 1,
Req: &http.Request{
Method: http.MethodPost,
},
Res: &http.Response{
StatusCode: http.StatusNotFound,
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := retryhttp.DefaultShouldRetryFn(tt.attempt)
if actual != tt.want {
t.Errorf("actual != expected: got %t, want %t", actual, tt.want)
}
})
}
}
func TestDefaultDelayFn(t *testing.T) {
tests := []struct {
name string
retryAfter string
attempt int
wantLow time.Duration
wantHigh time.Duration
}{
{
name: "should respect retry-after when provided as 1s",
retryAfter: "1",
attempt: 1,
wantLow: time.Millisecond * 666,
wantHigh: time.Millisecond * 1333,
},
{
name: "should respect retry-after when provided as 2s",
retryAfter: "2",
attempt: 1,
wantLow: time.Millisecond * 1333,
wantHigh: time.Millisecond * 2666,
},
{
name: "should respect retry-after when provided as 10s",
retryAfter: "10",
attempt: 1,
wantLow: time.Millisecond * 6666,
wantHigh: time.Millisecond * 13333,
},
{
name: "should respect retry-after when provided as date 2s in the future",
retryAfter: time.Now().UTC().Add(time.Second * 2).Format(http.TimeFormat),
attempt: 1,
wantLow: time.Millisecond * 666,
wantHigh: time.Millisecond * 2666,
},
{
name: "should respect retry-after when provided as date 10s in the future",
retryAfter: time.Now().UTC().Add(time.Second * 10).Format(http.TimeFormat),
attempt: 1,
wantLow: time.Millisecond * 5555,
wantHigh: time.Millisecond * 13333,
},
{
name: "should respect retry-after when provided as date 2h in the past",
retryAfter: time.Now().UTC().Add(time.Hour * -2).Format(http.TimeFormat),
attempt: 1,
wantLow: time.Minute * -160,
wantHigh: time.Minute * -80,
},
// retry-after with non-numeric / non-date value
{
name: "should fall back to exponential backoff when retry-after header is malformed",
retryAfter: "not a date",
attempt: 1,
wantLow: 0,
wantHigh: time.Millisecond * 250,
},
// exp backoff with varying values
{
name: "should return result consistent with exponential backoff on attempt 1",
attempt: 1,
wantLow: 0,
wantHigh: time.Millisecond * 250,
},
{
name: "should return result consistent with exponential backoff on attempt 2",
attempt: 2,
wantLow: 0,
wantHigh: time.Millisecond * 500,
},
{
name: "should return result consistent with exponential backoff on attempt 3",
attempt: 3,
wantLow: 0,
wantHigh: time.Second,
},
{
name: "should return result consistent with exponential backoff on attempt 4",
attempt: 4,
wantLow: 0,
wantHigh: time.Second * 2,
},
{
name: "should return result consistent with exponential backoff on attempt 5",
attempt: 5,
wantLow: 0,
wantHigh: time.Second * 4,
},
{
name: "should return result consistent with exponential backoff on attempt 6",
attempt: 6,
wantLow: 0,
wantHigh: time.Second * 8,
},
{
name: "exponential backoff should be capped by attempt 7",
attempt: 7,
wantLow: 0,
wantHigh: time.Second * 10,
},
{
name: "exponential backoff should be capped beyond attempt 7",
attempt: 100,
wantLow: 0,
wantHigh: time.Second * 10,
},
{
name: "exponential backoff should be capped beyond attempt 7",
attempt: 100,
wantLow: 0,
wantHigh: time.Second * 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := http.Response{
Header: http.Header{},
}
if tt.retryAfter != "" {
res.Header.Set("Retry-After", tt.retryAfter)
}
actual := retryhttp.DefaultDelayFn(retryhttp.Attempt{
Count: tt.attempt,
Res: &res,
})
if actual < tt.wantLow {
t.Errorf("actual less than expected range; expected between %s and %s, got %s", tt.wantLow, tt.wantHigh, actual)
}
if actual > tt.wantHigh {
t.Errorf("actual greater than expected range; expected between %s and %s, got %s", tt.wantLow, tt.wantHigh, actual)
}
})
}
}