-
Notifications
You must be signed in to change notification settings - Fork 4
/
retry_test.go
297 lines (253 loc) · 8.02 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
package transport
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"time"
gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestRequestCopier(t *testing.T) {
var bodyContent = "TEST"
var e error
var request, _ = http.NewRequest(http.MethodGet, "/", io.NopCloser(bytes.NewBufferString(bodyContent)))
var copier *requestCopier
copier, e = newRequestCopier(request)
if e != nil {
t.Fatal(e.Error())
}
var r1 = copier.Copy()
var buf1 bytes.Buffer
var n int64
n, e = io.Copy(&buf1, r1.Body)
if e != nil {
t.Fatal(e.Error())
}
if n != int64(len([]byte(bodyContent))) {
t.Fatalf("expected to copy %d bytes but only found %d.", len([]byte(bodyContent)), n)
}
var r2 = copier.Copy()
var buf2 bytes.Buffer
n, e = io.Copy(&buf2, r2.Body)
if e != nil {
t.Fatal(e.Error())
}
if n != int64(len([]byte(bodyContent))) {
t.Fatalf("expected to copy %d bytes but only found %d.", len([]byte(bodyContent)), n)
}
var regenerated, _ = r2.GetBody()
n, e = io.Copy(&buf2, regenerated)
if e != nil {
t.Fatal(e.Error())
}
if n != int64(len([]byte(bodyContent))) {
t.Fatalf("expected to copy %d bytes but only found %d.", len([]byte(bodyContent)), n)
}
}
func newRoundTripWithLatencyFunc(resp *http.Response, latency time.Duration) func(r *http.Request) (*http.Response, error) {
return func(r *http.Request) (*http.Response, error) {
resp.Request = r
select {
case <-time.After(latency):
return resp, nil
case <-r.Context().Done():
return nil, r.Context().Err()
}
}
}
func TestRetryTimeIfNoLatency(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewTimeoutRetryPolicy(time.Minute),
)(wrapped)
var roundTripFunc = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, 0)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFunc).Times(1)
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var resp, e = rt.RoundTrip(req)
if e != nil {
t.Fatal(e.Error())
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 but got %d", resp.StatusCode)
}
}
func TestRetryTimeIfLatency(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewTimeoutRetryPolicy(time.Millisecond),
)(wrapped)
var ctx, cancel = context.WithTimeout(context.Background(), time.Hour)
defer cancel()
var roundTripWithLatencyFunc = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, time.Minute)
var roundTripDeadlineExceededFunc = func(r *http.Request) (*http.Response, error) {
cancel()
return nil, context.DeadlineExceeded
}
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripWithLatencyFunc).Times(2)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripDeadlineExceededFunc).Times(1)
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var _, e = rt.RoundTrip(req.WithContext(ctx))
if e == nil {
t.Fatal("expected an error but got nil")
}
}
func TestContextKeepAlive(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewTimeoutRetryPolicy(time.Minute),
)(wrapped)
var roundTripFunc = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, 0)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFunc).Times(1)
var ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var res, e = rt.RoundTrip(req.WithContext(ctx))
if e != nil {
t.Fatalf("expected a nil error but got %s", e.Error())
}
if res.Request.Context().Err() != nil {
t.Fatal("context should not have been canceled but was")
}
}
func TestContextKeepAliveWithRetries(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewTimeoutRetryPolicy(time.Millisecond),
)(wrapped)
var roundTripFuncWithLatency = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, time.Minute)
var roundTripFuncWithoutLatency = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, 0)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFuncWithLatency).Times(1)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFuncWithoutLatency).Times(1)
var ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var res, e = rt.RoundTrip(req.WithContext(ctx))
if e != nil {
t.Fatalf("expected a nil error but got %s", e.Error())
}
if res.Request.Context().Err() != nil {
t.Fatal("context should not have been canceled but was")
}
}
func TestRetryCode(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewStatusCodeRetryPolicy(http.StatusInternalServerError),
)(wrapped)
var roundTripFuncWithError = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusInternalServerError,
Body: http.NoBody,
}, 0)
var roundTripFunc = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}, time.Second)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFuncWithError).Times(4)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFunc).Times(1)
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var _, e = rt.RoundTrip(req)
if e != nil {
t.Fatalf("expected a success but got: %s", e.Error())
}
}
func TestRetryLimit(t *testing.T) {
t.Parallel()
var ctrl = gomock.NewController(t)
defer ctrl.Finish()
var wrapped = NewMockRoundTripper(ctrl)
var rt = NewRetrier(
NewFixedBackoffPolicy(0),
NewLimitedRetryPolicy(3, NewStatusCodeRetryPolicy(http.StatusInternalServerError)),
)(wrapped)
var roundTripFuncWithError = newRoundTripWithLatencyFunc(&http.Response{
StatusCode: http.StatusInternalServerError,
Body: http.NoBody,
}, 0)
wrapped.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(roundTripFuncWithError).Times(4)
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var _, e = rt.RoundTrip(req)
if e != nil {
t.Fatalf("expected a response with bad code but got: %s", e.Error())
}
}
func TestJitterCalculation(t *testing.T) {
var jitterValue = float64(1)
var original = time.Millisecond
var percentage = .1
var randomCalled = false
var random = func() float64 {
if randomCalled {
return 0
}
randomCalled = true
return jitterValue
}
var result = calculateJitteredBackoff(original, percentage, random)
if result != (time.Millisecond + 100*time.Microsecond) {
t.Fatal(result)
}
randomCalled = false
random = func() float64 {
if randomCalled {
return 1
}
randomCalled = true
return jitterValue
}
result = calculateJitteredBackoff(original, percentage, random)
if result != (time.Millisecond - 100*time.Microsecond) {
t.Fatal(result)
}
}
func TestNewExponentialBackofferPolicy(t *testing.T) {
exponentialBackoffPolicy := NewExponentialBackoffPolicy(time.Second)
backoffer1 := exponentialBackoffPolicy()
backoffer2 := exponentialBackoffPolicy()
var req, _ = http.NewRequest(http.MethodGet, "/", nil)
var res = &http.Response{
StatusCode: http.StatusInternalServerError,
Body: http.NoBody,
}
backoffer1DurationRound1 := backoffer1.Backoff(req, res, nil)
backoffer1DurationRound2 := backoffer1.Backoff(req, res, nil)
backoffer2DurationRound1 := backoffer2.Backoff(req, res, nil)
assert.Equal(t, backoffer1DurationRound2, backoffer1DurationRound1*2)
assert.Equal(t, backoffer1DurationRound1, backoffer2DurationRound1)
}