-
Notifications
You must be signed in to change notification settings - Fork 70
/
retries_test.go
378 lines (354 loc) · 11.1 KB
/
retries_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
package session
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/internal/test"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegrid"
"github.com/apex/log"
"github.com/apex/log/handlers/discard"
"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newRequest(t *testing.T, method, url string) *http.Request {
r, err := http.NewRequest(method, url, nil)
assert.NoError(t, err)
return r
}
func TestOverrideRetryPolicy(t *testing.T) {
basePolicy := func(ctx context.Context, resp *http.Response, err error) (bool, error) {
return false, errors.New("base policy: dummy, not implemented")
}
policy := overrideRetryPolicy(basePolicy, []string{"/excluded"})
tests := map[string]struct {
ctx context.Context
resp *http.Response
err error
expectedResult bool
expectedError string
}{
"should retry for PAPI GET with status 429": {
ctx: context.Background(),
resp: &http.Response{
Request: newRequest(t, http.MethodGet, "/papi/v1/sth"),
StatusCode: http.StatusTooManyRequests,
},
expectedResult: true,
},
"should retry for GET with status 409 conflict": {
ctx: context.Background(),
resp: &http.Response{
Request: &http.Request{Method: http.MethodGet},
StatusCode: http.StatusConflict,
},
expectedResult: true,
},
"should call base policy for other GETs": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodGet}},
expectedError: "base policy: dummy, not implemented",
},
"should forward context error when present": {
ctx: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
resp: &http.Response{Request: &http.Request{Method: http.MethodGet}},
expectedError: "context canceled",
},
"should not retry for POST": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodPost}},
expectedResult: false,
},
"should not retry for PUT": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodPut}},
expectedResult: false,
},
"should not retry for PATCH": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodPatch}},
expectedResult: false,
},
"should not retry for HEAD": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodHead}},
expectedResult: false,
},
"should not retry for DELETE": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodDelete}},
expectedResult: false,
},
"should not retry excluded endpoints": {
ctx: context.Background(),
resp: &http.Response{Request: &http.Request{Method: http.MethodGet, URL: &url.URL{Path: "/excluded"}}},
expectedResult: false,
},
"nil request with error": {
ctx: context.Background(),
resp: nil,
err: errors.New("test error"),
expectedResult: false,
expectedError: "test error",
},
"should call base policy for GET url.Error": {
ctx: context.Background(),
resp: nil,
err: &url.Error{
Op: http.MethodGet,
URL: "",
Err: nil,
},
expectedError: "base policy: dummy, not implemented",
},
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
shouldRetry, err := policy(tst.ctx, tst.resp, tst.err)
if len(tst.expectedError) > 0 {
assert.ErrorContains(t, err, tst.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, tst.expectedResult, shouldRetry)
}
})
}
}
func stat429ResponseWaiting(wait time.Duration) *http.Response {
res := http.Response{
StatusCode: http.StatusTooManyRequests,
Header: http.Header{},
}
now := time.Now().UTC().Round(time.Second)
date := strings.Replace(now.Format(time.RFC1123), "UTC", "GMT", 1)
res.Header.Add("Date", date)
if wait != 0 {
// Add: allow to canonicalize to X-Ratelimit-Next or the header won't be recognized
res.Header.Add("X-RateLimit-Next", now.Add(wait).Format(time.RFC3339Nano))
}
return &res
}
func Test_overrideBackoff(t *testing.T) {
baseWait := time.Duration(24) * time.Hour
baseBackoff := func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
return baseWait
}
backoff := overrideBackoff(baseBackoff, nil)
tests := map[string]struct {
resp *http.Response
expectedResult time.Duration
}{
"correctly calculates backoff from X-RateLimit-Next": {
resp: stat429ResponseWaiting(time.Duration(5729) * time.Millisecond),
expectedResult: time.Duration(5729) * time.Millisecond,
},
"falls back for next in the past": {
resp: stat429ResponseWaiting(-time.Duration(5729) * time.Millisecond),
expectedResult: baseWait,
},
"falls back for no X-RateLimit-Next header": {
resp: stat429ResponseWaiting(0),
expectedResult: baseWait,
},
"falls back for invalid X-RateLimit-Next header": {
resp: func() *http.Response {
r := stat429ResponseWaiting(time.Duration(5729) * time.Millisecond)
r.Header.Set("X-RateLimit-Next", "2024-07-01T14:32:28.645???")
return r
}(),
expectedResult: baseWait,
},
"falls back for no Date header": {
resp: func() *http.Response {
r := stat429ResponseWaiting(time.Duration(5729) * time.Millisecond)
r.Header.Del("Date")
return r
}(),
expectedResult: baseWait,
},
"falls back for invalid Date header": {
resp: func() *http.Response {
r := stat429ResponseWaiting(time.Duration(5729) * time.Millisecond)
r.Header.Set("Date", "Mon, 01 Jul 2024 99:99:99 GMT")
return r
}(),
expectedResult: baseWait,
},
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
wait := backoff(1, 30, 1, tst.resp)
assert.Equal(t, tst.expectedResult, wait)
})
}
}
func TestXRateLimitGet(t *testing.T) {
xrlHandler := test.XRateLimitHTTPHandler{
T: t,
SuccessCode: http.StatusOK,
}
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/papi/test", r.URL.String())
assert.Equal(t, http.MethodGet, r.Method)
xrlHandler.ServeHTTP(w, r)
}))
defer mockServer.Close()
mockSession := mockRetrySession(t, mockServer)
client := mockSession.Client()
_, err := client.Get(fmt.Sprintf("%s/papi/test", mockServer.URL))
assert.NoError(t, err)
// We expect exactly two requests to the server:
// - the first resulting in code 429
// - the second after a proper backoff, resulting in status 200
assert.Equal(t, []int{http.StatusTooManyRequests, http.StatusOK}, xrlHandler.ReturnedCodes())
assert.Less(t,
xrlHandler.ReturnTimes()[1],
xrlHandler.AvailableAt().Add(time.Duration(time.Millisecond)*1100))
}
func mockRetrySession(t *testing.T, mockServer *httptest.Server) Session {
serverURL, err := url.Parse(mockServer.URL)
require.NoError(t, err)
config := edgegrid.Config{Host: serverURL.Host}
retryConf := NewRetryConfig()
retrySession, err := New(WithRetries(retryConf), WithSigner(&config))
assert.NoError(t, err)
return retrySession
}
func Test_configureRetryClient(t *testing.T) {
tests := map[string]struct {
retryMax int
retryWaitMin time.Duration
retryWaitMax time.Duration
excludedEndpoints []string
expected *retryablehttp.Client
expectedError string
}{
"happy path": {
retryMax: 5,
retryWaitMin: 5 * time.Second,
retryWaitMax: 31 * time.Second,
excludedEndpoints: []string{"aaa/bbb/ccc", "aaa/*/ccc"},
expected: &retryablehttp.Client{
RetryWaitMin: 5 * time.Second,
RetryWaitMax: 31 * time.Second,
RetryMax: 5,
},
},
"negative number of retries": {
retryMax: -5,
retryWaitMin: 5 * time.Second,
retryWaitMax: 30 * time.Second,
expected: nil,
expectedError: `maximum number of retries cannot be negative`,
},
"negative wait times": {
retryMax: 5,
retryWaitMin: -5 * time.Second,
retryWaitMax: -5 * time.Second,
expected: nil,
expectedError: `minimum retry wait time cannot be negative
maximum retry wait time cannot be negative`,
},
"minimum wait time higher that maximum wait time": {
retryMax: 5,
retryWaitMin: 30 * time.Second,
retryWaitMax: 5 * time.Second,
expected: nil,
expectedError: `maximum retry wait time cannot be shorter than minimum retry wait time`,
},
"malformed excluded endpoint pattern": {
excludedEndpoints: []string{"[-]"},
expected: nil,
expectedError: "malformed exclude endpoint pattern: syntax error in pattern: [-]",
},
"test error formation": {
retryMax: -5,
retryWaitMin: -3 * time.Second,
retryWaitMax: -7 * time.Second,
excludedEndpoints: []string{"[-]"},
expected: nil,
expectedError: `maximum number of retries cannot be negative
minimum retry wait time cannot be negative
maximum retry wait time cannot be negative
maximum retry wait time cannot be shorter than minimum retry wait time
malformed exclude endpoint pattern: syntax error in pattern: [-]`,
},
}
sessionLogger := &log.Logger{
Handler: discard.New(),
Level: 1,
}
testSession := &session{log: sessionLogger}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
conf := RetryConfig{
RetryMax: test.retryMax,
RetryWaitMin: test.retryWaitMin,
RetryWaitMax: test.retryWaitMax,
ExcludedEndpoints: test.excludedEndpoints,
}
got, err := configureRetryClient(conf, testSession.Sign, testSession.log)
if len(test.expectedError) > 0 {
assert.ErrorContains(t, err, test.expectedError)
} else {
assert.NoError(t, err)
}
if test.expected != nil {
assert.Equal(t, test.expected.RetryMax, got.RetryMax)
assert.Equal(t, test.expected.RetryWaitMax, got.RetryWaitMax)
assert.Equal(t, test.expected.RetryWaitMin, got.RetryWaitMin)
} else {
assert.Nil(t, got)
}
})
}
}
func Test_isBlocked(t *testing.T) {
tests := map[string]struct {
url string
blocked []string
expected bool
}{
"blocked - no wildcards": {
url: "/api/blocked",
blocked: []string{"/api/blocked"},
expected: true,
},
"blocked - with wildcard": {
url: "/api/blocked/123",
blocked: []string{"/api/blocked/*"},
expected: true,
},
"allowed - all": {
url: "/api/not-blocked/123/data",
blocked: []string{},
expected: false,
},
"allowed - with wildcard": {
url: "/api/not-blocked/123/data",
blocked: []string{"/api/not-blocked/*"},
expected: false,
},
"allowed - wildcard does not match beginning of url": {
url: "/api/not-blocked/123/data",
blocked: []string{"/not-blocked/*/data"},
expected: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := isBlocked(test.url, test.blocked)
assert.Equal(t, test.expected, got)
})
}
}