-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhttpclient_internal_test.go
333 lines (304 loc) · 7.68 KB
/
httpclient_internal_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
package httpclient
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"sync"
"testing"
"time"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"github.com/circleci/ex/httpserver"
"github.com/circleci/ex/o11y"
"github.com/circleci/ex/testing/testcontext"
)
func TestNewRequest_Formats(t *testing.T) {
req := NewRequest("POST", "/%s.txt",
RouteParams("the-path"),
)
assert.Check(t, cmp.Equal(req.url, "/the-path.txt"))
assert.Check(t, cmp.Equal(req.route, "/%s.txt"))
assert.Check(t, cmp.Equal(req.method, "POST"))
}
func TestNewRequest_NoParams(t *testing.T) {
req := NewRequest("POST", "/api/foo")
assert.Check(t, cmp.Equal(req.url, "/api/foo"))
assert.Check(t, cmp.Equal(req.route, "/api/foo"))
assert.Check(t, cmp.Equal(req.method, "POST"))
}
func TestHTTPError_Is(t *testing.T) {
tests := []struct {
code int
is bool
}{
{code: 100, is: false},
{code: 101, is: false},
{code: 400, is: false},
{code: 401, is: true},
{code: 403, is: true},
{code: 404, is: true},
{code: 405, is: false},
{code: 500, is: false},
{code: 503, is: false},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("code-:%d", tt.code), func(t *testing.T) {
// all errors start off as warnings - since they default to retrying
var err error
err = &HTTPError{code: tt.code}
assert.Check(t, cmp.Equal(o11y.IsWarning(err), true))
err = doneRetrying(err)
assert.Check(t, cmp.Equal(o11y.IsWarning(err), tt.is))
// confirm wrapped it is still checked as a do not trace
wErr := fmt.Errorf("foo :%w", err)
assert.Check(t, cmp.Equal(o11y.IsWarning(err), tt.is))
// and check the wrapped err it still is an HTTPError and that we can get the code back
ne := &HTTPError{}
assert.Check(t, errors.As(wErr, &ne))
assert.Check(t, cmp.Equal(ne.code, tt.code))
// ne should be equivalent to wErr now
assert.Check(t, !errors.Is(err, wErr))
// check that no two instances are Is-quivalent
err2 := &HTTPError{}
// and confirm they are not equivalent
assert.Check(t, !errors.Is(err, err2))
})
}
}
func TestHasStatusCode(t *testing.T) {
tests := []struct {
name string
err error
codes []int
want bool
}{
{
name: "With matching code",
err: &HTTPError{
code: 400,
},
codes: []int{400, 500},
want: true,
},
{
name: "With different code",
err: &HTTPError{
code: 200,
},
codes: []int{400, 500},
want: false,
},
{
name: "Empty error",
err: &HTTPError{},
codes: []int{400},
want: false,
},
{
name: "Nil error",
err: nil,
codes: []int{400},
want: false,
},
{
name: "Other kind of error",
err: errors.New("some other error"),
codes: []int{400},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Check(t, cmp.Equal(HasStatusCode(tt.err, tt.codes...), tt.want))
})
}
}
func TestIsRequestProblem(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{
name: "With problem code",
err: &HTTPError{
code: 400,
},
want: true,
},
{
name: "With non-Request error code",
err: &HTTPError{
code: 500,
},
want: false,
},
{
name: "With good code",
err: &HTTPError{
code: 200,
},
want: false,
},
{
name: "Empty error",
err: &HTTPError{},
want: false,
},
{
name: "Nil error",
err: nil,
want: false,
},
{
name: "Other kind of error",
err: errors.New("some other error"),
want: false,
},
{
name: "No content error",
err: ErrNoContent,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Check(t, cmp.Equal(IsRequestProblem(tt.err), tt.want))
})
}
}
func TestClient_ExplicitBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(testcontext.Background())
var mu sync.RWMutex
send429 := false
handlerCount := 0
now := time.Now()
nowFn := func() time.Time {
mu.RLock()
defer mu.RUnlock()
return now
}
// start our server with a handler that writes a response and in a certain range of
// requests returns 429's
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
doSend := send429
handlerCount++
mu.Unlock()
if doSend {
w.WriteHeader(http.StatusTooManyRequests)
return
}
_, _ = io.WriteString(w, `{"hello": "world!"} ...`)
// to help the client have the full number of concurrent requests in flight
time.Sleep(2 * time.Millisecond)
})
srv, err := httpserver.New(ctx, httpserver.Config{
Name: "test server",
Addr: "localhost:0",
Handler: h,
})
assert.Assert(t, err)
g, ctx := errgroup.WithContext(ctx)
t.Cleanup(func() {
cancel()
assert.Check(t, g.Wait())
})
g.Go(func() error {
return srv.Serve(ctx)
})
t.Run("backoff", func(t *testing.T) {
client := New(Config{
Name: "keep-alive",
BaseURL: "http://" + srv.Addr(),
Timeout: time.Second,
})
client.now = nowFn
req := NewRequest("POST", "/")
// Making concurrent calls in this test to increase the chance of
// flushing out any race in the client
const numReq = 50
var wg sync.WaitGroup
wg.Add(numReq)
for n := 0; n < numReq; n++ {
go func() {
err := client.Call(context.Background(), req)
assert.Assert(t, err)
wg.Done()
}()
}
wg.Wait()
// At some random point start sending 429's (and explicitly stop setting once we have)
ctx429, cancel429 := context.WithCancel(ctx)
go func() {
for {
if ctx429.Err() != nil {
return
}
// start sending 429's
mu.Lock()
if handlerCount > numReq+5 {
send429 = true
cancel429()
}
mu.Unlock()
time.Sleep(time.Microsecond * 10)
}
}()
// hopefully during these concurrent calls we will see the 429 and the explicit backoff
// It is not critical that we do, it is just statistically likely, and in that case
// we can be confident that we would see if the client was racy.
wg.Add(numReq)
for n := 0; n < numReq; n++ {
go func() {
// these calls may see a mix of nil error, explicit backoff
// and 429's most likely all 429's, so no point testing the error
_ = client.Call(context.Background(), req)
wg.Done()
}()
}
wg.Wait()
// make sure we stop sending 429's after the backoff time has elapsed
// wait until we are sure the 429 setting loop above is complete
<-ctx429.Done()
send429 = false
// confirm the server may have seen all or none of the calls whilst the 429 was being set
assert.Check(t, handlerCount > numReq && handlerCount <= numReq*2, handlerCount)
// there is a v slim chance this call is the first one to see the 429
_ = client.Call(context.Background(), req)
// but this one will definitely be an explicit backoff
curHandlerCount := handlerCount
err = client.Call(context.Background(), req)
assert.Check(t, cmp.ErrorContains(err, "explicit backoff"))
// and will not have called the server
assert.Check(t, cmp.Equal(curHandlerCount, handlerCount))
// during some concurrent calls set the time to have elapsed past the 10s last 429 time
// to close the circuit - these calls may not see the close, or they may all see it
// or something in between, so there is not much we can assert.
wg.Add(numReq)
for n := 0; n < numReq; n++ {
// at some random point boost the time to close the circuit
if n == 10 {
go func() {
mu.Lock()
now = now.Add(time.Second * 20)
mu.Unlock()
}()
}
go func() {
err := client.Call(context.Background(), req)
if err != nil {
assert.Check(t, cmp.ErrorContains(err, "explicit backoff"))
}
wg.Done()
}()
}
wg.Wait()
// this call will definitely nt see the explicit backoff
err = client.Call(context.Background(), req)
assert.Assert(t, err)
})
}