-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
200 lines (182 loc) · 6.9 KB
/
Copy pathclient_test.go
File metadata and controls
200 lines (182 loc) · 6.9 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
package monta
import (
"context"
"errors"
"io"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
"gotest.tools/v3/assert"
)
// roundTripFunc adapts a function to an [http.RoundTripper].
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
func newTestResponse(statusCode int, body string) *http.Response {
return &http.Response{
StatusCode: statusCode,
Status: http.StatusText(statusCode),
Body: io.NopCloser(strings.NewReader(body)),
Header: make(http.Header),
}
}
func newTestClient(transport http.RoundTripper, maxRetries int) *clientImpl {
return &clientImpl{
httpClient: &http.Client{Transport: transport},
config: clientConfig{
maxRetries: maxRetries,
retryBaseDelay: time.Millisecond,
},
}
}
func closeResponse(resp *http.Response) {
if resp != nil {
_ = resp.Body.Close()
}
}
func TestDoWithRetry_RetriesGETOn5xxThenSucceeds(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
if calls.Add(1) <= 2 {
return newTestResponse(http.StatusInternalServerError, `{"message":"Read Timeout"}`), nil
}
return newTestResponse(http.StatusOK, `{}`), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
assert.NilError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, int32(3), calls.Load())
}
func TestDoWithRetry_RetriesGETOn429(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
if calls.Add(1) == 1 {
return newTestResponse(http.StatusTooManyRequests, "slow down"), nil
}
return newTestResponse(http.StatusOK, `{}`), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
assert.NilError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, int32(2), calls.Load())
}
func TestDoWithRetry_ExhaustsRetriesAndReturnsStatusError(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
calls.Add(1)
return newTestResponse(http.StatusInternalServerError, "boom"), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
var statusErr *StatusError
assert.Assert(t, errors.As(err, &statusErr))
assert.Equal(t, http.StatusInternalServerError, statusErr.StatusCode)
// 1 initial attempt + 3 retries.
assert.Equal(t, int32(4), calls.Load())
}
func TestDoWithRetry_DoesNotRetryNon5xx(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
calls.Add(1)
return newTestResponse(http.StatusNotFound, "not found"), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
var statusErr *StatusError
assert.Assert(t, errors.As(err, &statusErr))
assert.Equal(t, http.StatusNotFound, statusErr.StatusCode)
assert.Equal(t, int32(1), calls.Load())
}
func TestDoWithRetry_DoesNotRetryNonGET(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
calls.Add(1)
return newTestResponse(http.StatusInternalServerError, "boom"), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodPost)
defer closeResponse(resp)
var statusErr *StatusError
assert.Assert(t, errors.As(err, &statusErr))
assert.Equal(t, int32(1), calls.Load())
}
func TestDoWithRetry_RetriesNetworkErrorOnGET(t *testing.T) {
var calls atomic.Int32
netErr := errors.New("connection reset")
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
if calls.Add(1) == 1 {
return nil, netErr
}
return newTestResponse(http.StatusOK, `{}`), nil
}), 3)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
assert.NilError(t, err)
assert.Equal(t, int32(2), calls.Load())
}
func TestDoWithRetry_Disabled(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
calls.Add(1)
return newTestResponse(http.StatusInternalServerError, "boom"), nil
}), 0)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(context.Background(), req, http.MethodGet)
defer closeResponse(resp)
assert.Assert(t, err != nil)
assert.Equal(t, int32(1), calls.Load())
}
func TestDoWithRetry_RespectsContextCancellation(t *testing.T) {
var calls atomic.Int32
client := newTestClient(roundTripFunc(func(*http.Request) (*http.Response, error) {
calls.Add(1)
return newTestResponse(http.StatusInternalServerError, "boom"), nil
}), 5)
client.config.retryBaseDelay = time.Hour // force the backoff to block until cancellation
ctx, cancel := context.WithCancel(context.Background())
cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiHost+"/foo", nil)
assert.NilError(t, err)
resp, err := client.doWithRetry(ctx, req, http.MethodGet)
defer closeResponse(resp)
assert.Assert(t, errors.Is(err, context.Canceled))
// One attempt before the (cancelled) backoff wait stops the loop.
assert.Equal(t, int32(1), calls.Load())
}
func TestBackoffDelay(t *testing.T) {
base := 100 * time.Millisecond
assert.Equal(t, base, backoffDelay(base, 1))
assert.Equal(t, 2*base, backoffDelay(base, 2))
assert.Equal(t, 4*base, backoffDelay(base, 3))
// Exponent is capped to avoid overflow on large attempt counts.
assert.Equal(t, 64*base, backoffDelay(base, 100))
}
func TestNewClient_RetryDefaults(t *testing.T) {
client, ok := NewClient().(*clientImpl)
assert.Assert(t, ok)
assert.Equal(t, defaultMaxRetries, client.config.maxRetries)
assert.Equal(t, defaultRetryBaseDelay, client.config.retryBaseDelay)
}
func TestWithRetry(t *testing.T) {
client, ok := NewClient(WithRetry(1, 5*time.Second)).(*clientImpl)
assert.Assert(t, ok)
assert.Equal(t, 1, client.config.maxRetries)
assert.Equal(t, 5*time.Second, client.config.retryBaseDelay)
}