-
Notifications
You must be signed in to change notification settings - Fork 2
/
strategies_test.go
326 lines (292 loc) · 8.94 KB
/
strategies_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
package reqstrategy
import (
"fmt"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
)
type transport func(*http.Request) (*http.Response, error)
func (t transport) RoundTrip(r *http.Request) (*http.Response, error) {
return t(r)
}
func newRequest(t *testing.T, path ...string) *http.Request {
request, err := http.NewRequest("GET", "http://localhost/"+strings.Join(path, "/"), nil)
if err != nil {
t.Fatalf(`failed to create "%s" request: %s`, path, err)
}
return request
}
func newClient(roundTrip func(r *http.Request) (*http.Response, error)) *http.Client {
return &http.Client{Transport: transport(roundTrip)}
}
func Test_Do(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{Request: r, StatusCode: 200}, nil
})
response, err := Do(client, newRequest(t))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if response.StatusCode != 200 {
t.Fatalf("unexpected response")
}
}
func Test_Do_error(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("request failed")
})
response, err := Do(client, newRequest(t))
if response != nil {
t.Fatalf(`response should be <nil>, got %v`, response)
}
if err == nil {
t.Fatal("expected error, got <nil>")
}
if err.Error() != "Get http://localhost/: request failed" {
t.Fatalf(`expected error "Get http://localhost/: request failed", got "%s"`, err.Error())
}
}
func Test_Do_validation(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{Request: r, StatusCode: 404}, nil
})
req := WithStatusRequired(newRequest(t), 200)
resp, err := Do(client, req)
if resp == nil || resp.StatusCode != 404 {
t.Fatal("response expected")
}
if err == nil {
t.Fatal("validator is expected to fail")
}
want := "GET http://localhost/: expected response status [200], got 404"
if err.Error() != want {
t.Fatalf(`expected "%s" error, got "%s"`, want, err.Error())
}
}
func Test_WithStatusRequired(t *testing.T) {
req := WithStatusRequired(newRequest(t), 200)
validators, _ := req.Context().Value(keyValidators).([]validator)
if len(validators) != 1 {
t.Fatalf("expected 1 validator to be set, got %d", len(validators))
}
res := &http.Response{Request: req, StatusCode: 404}
err := validators[0](res)
if err == nil {
t.Fatal("validator is expected to fail")
}
want := "GET http://localhost/: expected response status [200], got 404"
if err.Error() != want {
t.Fatalf(`expected "%s" error, got "%s"`, want, err.Error())
}
}
func Test_Race(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/a":
<-time.After(200 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
case "/b":
<-time.After(100 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 500}, nil
case "/c":
<-time.After(300 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
default:
panic("wrong URL: " + r.URL.String())
}
})
response, err := Race(client,
WithStatusRequired(newRequest(t, "a"), 200), // second fastest
WithStatusRequired(newRequest(t, "b"), 200), // fastest but failed
WithStatusRequired(newRequest(t, "c"), 200),
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if response.Request.URL.Path != "/a" {
t.Fatalf(`expected "/a" to win, got "%s"`, response.Request.URL)
}
}
func Test_Race_all_failed(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{Request: r, StatusCode: 500}, nil
})
response, err := Race(client,
WithStatusRequired(newRequest(t, "a"), 200),
WithStatusRequired(newRequest(t, "b"), 200),
WithStatusRequired(newRequest(t, "c"), 200),
)
if response != nil {
t.Fatalf("expected <nil> response")
}
if err == nil {
t.Fatalf("expected error")
}
if err.Error() != "all requests failed" {
t.Fatalf(`expected "all requests failed" error, got "%s"`, err.Error())
}
}
func Test_All(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/a":
<-time.After(200 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
case "/b":
<-time.After(100 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
case "/c":
<-time.After(300 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
default:
panic("wrong URL: " + r.URL.String())
}
})
responses, err := All(client,
WithStatusRequired(newRequest(t, "a"), 200), // second fastest
WithStatusRequired(newRequest(t, "b"), 200), // fastest but failed
WithStatusRequired(newRequest(t, "c"), 200),
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
for i, path := range []string{"/a", "/b", "/c"} {
if responses[i].Request.URL.Path != path {
t.Fatalf(`expected #%d response to be from "%s", got "%s"`, i, path, responses[i].Request.URL.Path)
}
}
}
func Test_All_error(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/a":
<-time.After(200 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
case "/b":
<-time.After(100 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 500}, nil
case "/c":
<-time.After(300 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
default:
panic("wrong URL: " + r.URL.String())
}
})
responses, err := All(client,
WithStatusRequired(newRequest(t, "a"), 200), // second fastest
WithStatusRequired(newRequest(t, "b"), 200), // fastest but failed
WithStatusRequired(newRequest(t, "c"), 200),
)
if responses != nil {
t.Fatalf("expected no responses, got %v", responses)
}
if err == nil {
t.Fatal("expected error")
}
want := "GET http://localhost/b: expected response status [200], got 500"
if err.Error() != want {
t.Fatalf(`expected "%s" error, got "%s"`, want, err.Error())
}
}
func Test_Some(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/a":
<-time.After(200 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
case "/b":
<-time.After(100 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 500}, nil
case "/c":
<-time.After(300 * time.Millisecond)
return &http.Response{Request: r, StatusCode: 200}, nil
default:
panic("wrong URL: " + r.URL.String())
}
})
responses, err := Some(client,
WithStatusRequired(newRequest(t, "a"), 200), // second fastest
WithStatusRequired(newRequest(t, "b"), 200), // fastest but failed
WithStatusRequired(newRequest(t, "c"), 200),
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if responses[0].Request.URL.Path != "/a" {
t.Fatalf(`expected #0 response to be from "/a", got "%s"`, responses[0].Request.URL.Path)
}
if responses[1] != nil {
t.Fatalf(`expected #1 response to be <nil>, got %v`, responses[1])
}
if responses[2].Request.URL.Path != "/c" {
t.Fatalf(`expected #2 response to be from "/c", got "%s"`, responses[2].Request.URL.Path)
}
}
func Test_Some_all_failed(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{Request: r, StatusCode: 500}, nil
})
response, err := Some(client,
WithStatusRequired(newRequest(t, "a"), 200),
WithStatusRequired(newRequest(t, "b"), 200),
WithStatusRequired(newRequest(t, "c"), 200),
)
if response != nil {
t.Fatalf("expected <nil> response")
}
if err == nil {
t.Fatalf("expected error")
}
if err.Error() != "all requests failed" {
t.Fatalf(`expected "all requests failed" error, got "%s"`, err.Error())
}
}
func Test_Retry(t *testing.T) {
var count int32
client := newClient(func(r *http.Request) (*http.Response, error) {
if atomic.AddInt32(&count, 1) <= 2 {
return &http.Response{Request: r, StatusCode: 500}, nil
}
return &http.Response{Request: r, StatusCode: 200}, nil
})
resp, err := Retry(
client,
WithStatusRequired(newRequest(t), 200),
100*time.Millisecond,
100*time.Millisecond,
100*time.Millisecond,
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if resp.StatusCode != 200 {
t.Fatalf(`expected response status 200, got %d`, resp.StatusCode)
}
if count != 3 {
t.Fatalf(`expected 3 calls to be made, got %d`, count)
}
}
func Test_Retry_error(t *testing.T) {
client := newClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{Request: r, StatusCode: 500}, nil
})
resp, err := Retry(
client,
WithStatusRequired(newRequest(t), 200),
100*time.Millisecond,
100*time.Millisecond,
100*time.Millisecond,
)
if resp == nil {
t.Fatalf("response expected")
}
if err == nil {
t.Fatal("expected error")
}
want := "GET http://localhost/: expected response status [200], got 500"
if err.Error() != want {
t.Fatalf(`expected "%s" error, got "%s"`, want, err.Error())
}
}