Repository navigation
Expand file tree
/
Copy pathrequest_queue_batch_test.go
More file actions
359 lines (332 loc) · 13.4 KB
/
Copy pathrequest_queue_batch_test.go
File metadata and controls
359 lines (332 loc) · 13.4 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
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
package apify
import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
)
// batchBackend answers batch-add requests like the API: it adds every request except the ones
// that reject says to leave unprocessed.
type batchBackend struct {
mu sync.Mutex
calls int
bodies [][]RequestQueueRequest
inflight atomic.Int64
peak atomic.Int64
// reject returns the unique keys that the call with the given index leaves unprocessed.
reject func(call int, requests []RequestQueueRequest) map[string]bool
// fail returns the error of the call with the given index, or nil.
fail func(call int) error
// delay is how long each call takes.
delay time.Duration
}
func (b *batchBackend) Do(req *http.Request) (*http.Response, error) {
data := decodeBody(req)
var requests []RequestQueueRequest
if err := json.Unmarshal(data, &requests); err != nil {
return nil, err
}
current := b.inflight.Add(1)
defer b.inflight.Add(-1)
for peak := b.peak.Load(); current > peak && !b.peak.CompareAndSwap(peak, current); peak = b.peak.Load() {
}
if b.delay > 0 {
time.Sleep(b.delay)
}
b.mu.Lock()
call := b.calls
b.calls++
b.bodies = append(b.bodies, requests)
b.mu.Unlock()
if b.fail != nil {
if err := b.fail(call); err != nil {
return nil, err
}
}
var rejected map[string]bool
if b.reject != nil {
rejected = b.reject(call, requests)
}
processed, unprocessed := []map[string]any{}, []map[string]any{}
for _, r := range requests {
if rejected[r.UniqueKey] {
unprocessed = append(unprocessed, map[string]any{"uniqueKey": r.UniqueKey, "url": r.URL, "method": r.Method})
} else {
processed = append(processed, map[string]any{"requestId": "id-" + r.UniqueKey, "uniqueKey": r.UniqueKey})
}
}
body, _ := json.Marshal(map[string]any{"data": map[string]any{"processedRequests": processed, "unprocessedRequests": unprocessed}})
return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(string(body)))}, nil
}
// decodeBody reads the body of req, which the client may have compressed.
func decodeBody(req *http.Request) []byte {
var reader io.Reader = req.Body
switch req.Header.Get("Content-Encoding") {
case "br":
reader = brotli.NewReader(req.Body)
case "gzip":
gz, err := gzip.NewReader(req.Body)
if err != nil {
return nil
}
reader = gz
case "zstd":
zr, err := zstd.NewReader(req.Body)
if err != nil {
return nil
}
defer zr.Close()
reader = zr
}
data, err := io.ReadAll(reader)
if err != nil {
return nil
}
return data
}
func uniqueRequests(n int) []RequestQueueRequest {
requests := make([]RequestQueueRequest, n)
for i := range requests {
key := "key-" + string(rune('a'+i/26)) + string(rune('a'+i%26))
requests[i] = RequestQueueRequest{URL: "https://example.com/" + key, UniqueKey: key, Method: "GET"}
}
return requests
}
// fastBatchOptions retries without a noticeable delay.
func fastBatchOptions() BatchAddRequestsOptions {
return BatchAddRequestsOptions{MinDelayBetweenUnprocessedRequestsRetries: Ptr(time.Millisecond)}
}
func TestBatchAddRequestsSplitsInputIntoBatchesOfTheAPILimit(t *testing.T) {
backend := &batchBackend{}
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(context.Background(), uniqueRequests(60), false)
if err != nil {
t.Fatal(err)
}
if backend.calls != 3 || len(result.ProcessedRequests) != 60 || len(result.UnprocessedRequests) != 0 {
t.Fatalf("calls %d, processed %d, unprocessed %d", backend.calls, len(result.ProcessedRequests), len(result.UnprocessedRequests))
}
}
func TestBatchAddRequestsRetriesTheUnprocessedRequestsOnly(t *testing.T) {
backend := &batchBackend{reject: func(call int, requests []RequestQueueRequest) map[string]bool {
if call == 0 {
return map[string]bool{"key-aa": true, "key-ab": true}
}
return nil
}}
options := fastBatchOptions()
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(5), options)
if err != nil {
t.Fatal(err)
}
if backend.calls != 2 {
t.Fatalf("expected one retry, got %d calls", backend.calls)
}
if retried := backend.bodies[1]; len(retried) != 2 || retried[0].UniqueKey != "key-aa" || retried[1].UniqueKey != "key-ab" {
t.Fatalf("the retry must send the unprocessed requests only, got %+v", retried)
}
if len(result.ProcessedRequests) != 5 || len(result.UnprocessedRequests) != 0 {
t.Fatalf("processed %d, unprocessed %d", len(result.ProcessedRequests), len(result.UnprocessedRequests))
}
}
func TestBatchAddRequestsReportsWhatIsStillUnprocessedWhenTheRetriesRunOut(t *testing.T) {
backend := &batchBackend{reject: func(call int, requests []RequestQueueRequest) map[string]bool {
return map[string]bool{"key-aa": true}
}}
options := fastBatchOptions()
options.MaxUnprocessedRequestsRetries = Ptr(int64(2))
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(3), options)
if err != nil {
t.Fatal(err)
}
if backend.calls != 3 {
t.Fatalf("expected the first attempt and 2 retries, got %d calls", backend.calls)
}
if len(result.ProcessedRequests) != 2 || len(result.UnprocessedRequests) != 1 || result.UnprocessedRequests[0].UniqueKey != "key-aa" {
t.Fatalf("result: %+v", result)
}
}
func TestBatchAddRequestsDoesNotRetryWithZeroRetries(t *testing.T) {
backend := &batchBackend{reject: func(int, []RequestQueueRequest) map[string]bool { return map[string]bool{"key-aa": true} }}
options := fastBatchOptions()
options.MaxUnprocessedRequestsRetries = Ptr(int64(0))
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(3), options)
if err != nil || backend.calls != 1 || len(result.UnprocessedRequests) != 1 {
t.Fatalf("calls %d, unprocessed %d, err %v", backend.calls, len(result.UnprocessedRequests), err)
}
}
func TestBatchAddRequestsSendsAtMostMaxParallelBatchesAtOnce(t *testing.T) {
backend := &batchBackend{delay: 20 * time.Millisecond}
options := BatchAddRequestsOptions{MaxParallel: Ptr(int64(2))}
if _, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(125), options); err != nil {
t.Fatal(err)
}
if backend.calls != 5 || backend.peak.Load() != 2 {
t.Fatalf("calls %d, peak parallelism %d", backend.calls, backend.peak.Load())
}
}
func TestBatchAddRequestsLeavesTheRequestsOfAFailedBatchUnprocessed(t *testing.T) {
backend := &batchBackend{fail: func(call int) error {
if call == 0 {
return errors.New("connection refused")
}
return nil
}}
options := BatchAddRequestsOptions{MaxParallel: Ptr(int64(1))}
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(30), options)
if err != nil {
t.Fatalf("a failed batch is not an error of the call: %v", err)
}
if len(result.UnprocessedRequests) != 25 || len(result.ProcessedRequests) != 5 {
t.Fatalf("processed %d, unprocessed %d", len(result.ProcessedRequests), len(result.UnprocessedRequests))
}
if first := result.UnprocessedRequests[0]; first.UniqueKey != "key-aa" || first.URL == "" || first.Method != "GET" {
t.Fatalf("an unprocessed request keeps its method, unique key and URL: %+v", first)
}
}
func TestBatchAddRequestsKeepsWhatWasAddedWhenARetryFails(t *testing.T) {
backend := &batchBackend{
reject: func(call int, requests []RequestQueueRequest) map[string]bool {
if call == 0 {
return map[string]bool{"key-ab": true, "key-ac": true}
}
return nil
},
fail: func(call int) error {
if call == 1 {
return errors.New("connection reset")
}
return nil
},
}
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(4), fastBatchOptions())
if err != nil {
t.Fatal(err)
}
if len(result.ProcessedRequests) != 2 || len(result.UnprocessedRequests) != 2 {
t.Fatalf("processed %d, unprocessed %d", len(result.ProcessedRequests), len(result.UnprocessedRequests))
}
for _, r := range result.UnprocessedRequests {
if r.UniqueKey != "key-ab" && r.UniqueKey != "key-ac" {
t.Fatalf("an added request is reported as unprocessed: %+v", r)
}
}
}
func TestBatchAddRequestsReturnsTheErrorWhenTheContextEndsDuringARequest(t *testing.T) {
backend := &batchBackend{delay: 100 * time.Millisecond, fail: func(int) error { return context.DeadlineExceeded }}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
_, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(ctx, uniqueRequests(2), false)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("err = %v", err)
}
}
func TestBatchAddRequestsReturnsAnErrorForAnUndecodableResponse(t *testing.T) {
backend := &mockBackend{responses: constant(200, `{"data":`)}
_, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(context.Background(), uniqueRequests(1), false)
if err == nil {
t.Fatal("expected an error")
}
}
func TestUnprocessedRetryDelayStaysWithinTheCap(t *testing.T) {
client := testClient(&batchBackend{}, 0).RequestQueue("q1")
timeoutMax := client.ctx.http.retry.timeoutMax
for _, minDelay := range []time.Duration{500 * time.Millisecond, 2 * timeoutMax} {
for _, attempt := range []int{0, 1, 5, 40, 200} {
delay := client.unprocessedRetryDelay(minDelay, attempt)
if delay < minDuration(minDelay, timeoutMax) || delay >= 2*timeoutMax {
t.Fatalf("minDelay %v, attempt %d: delay %v", minDelay, attempt, delay)
}
}
}
}
func TestBatchAddRequestsReturnsTheErrorOfAnInvalidQueueID(t *testing.T) {
backend := &batchBackend{}
_, err := testClient(backend, 0).RequestQueue("").BatchAddRequests(context.Background(), uniqueRequests(2), false)
if err == nil || backend.calls != 0 {
t.Fatalf("err = %v, calls = %d", err, backend.calls)
}
}
func TestBatchAddRequestsStopsRetryingWhenTheContextEnds(t *testing.T) {
backend := &batchBackend{reject: func(int, []RequestQueueRequest) map[string]bool { return map[string]bool{"key-aa": true} }}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
options := BatchAddRequestsOptions{MinDelayBetweenUnprocessedRequestsRetries: Ptr(time.Hour)}
result, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(ctx, uniqueRequests(2), options)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("err = %v", err)
}
if len(result.ProcessedRequests) != 1 || len(result.UnprocessedRequests) != 1 {
t.Fatalf("processed %d, unprocessed %d", len(result.ProcessedRequests), len(result.UnprocessedRequests))
}
}
func TestBatchAddRequestsRejectsInvalidOptionsBeforeSendingAnything(t *testing.T) {
cases := map[string]BatchAddRequestsOptions{
"negative retries": {MaxUnprocessedRequestsRetries: Ptr(int64(-1))},
"zero parallelism": {MaxParallel: Ptr(int64(0))},
"negative delay": {MinDelayBetweenUnprocessedRequestsRetries: Ptr(-time.Second)},
}
for name, options := range cases {
t.Run(name, func(t *testing.T) {
backend := &batchBackend{}
if _, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(1), options); err == nil || backend.calls != 0 {
t.Fatalf("err = %v, calls = %d", err, backend.calls)
}
})
}
}
func TestBatchAddRequestsSendsForefront(t *testing.T) {
backend := &mockBackend{responses: constant(200, `{"data":{"processedRequests":[],"unprocessedRequests":[]}}`)}
options := BatchAddRequestsOptions{Forefront: true, MaxUnprocessedRequestsRetries: Ptr(int64(0))}
if _, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequestsWithOptions(context.Background(), uniqueRequests(1), options); err != nil {
t.Fatal(err)
}
if !strings.Contains(backend.lastURL, "forefront=1") {
t.Fatalf("URL: %s", backend.lastURL)
}
}
func TestBatchAddRequestsRejectsAnOversizedRequestBeforeSendingAnything(t *testing.T) {
backend := &batchBackend{}
requests := uniqueRequests(30)
// A URL this long exceeds payloadSizeLimitBytes, so the request cannot fit a batch of its own.
requests[29].URL = "https://example.com/" + strings.Repeat("a", payloadSizeLimitBytes)
_, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(context.Background(), requests, false)
if err == nil || !strings.Contains(err.Error(), "index 29") {
t.Fatalf("expected an error naming index 29, got %v", err)
}
if backend.calls != 0 {
t.Fatalf("expected no batch to be sent, got %d calls", backend.calls)
}
}
func TestBatchAddRequestsSplitsABatchThatExceedsTheByteLimit(t *testing.T) {
backend := &batchBackend{}
big := strings.Repeat("a", payloadSizeLimitBytes/2)
requests := []RequestQueueRequest{
{URL: "https://example.com/" + big, UniqueKey: "one"},
{URL: "https://example.com/" + big, UniqueKey: "two"},
}
if _, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(context.Background(), requests, false); err != nil {
t.Fatal(err)
}
if backend.calls != 2 {
t.Fatalf("expected 2 batches, got %d calls", backend.calls)
}
}
func TestBatchAddRequestsSendsEachBatchAsOneJSONArray(t *testing.T) {
backend := &batchBackend{}
requests := uniqueRequests(2)
if _, err := testClient(backend, 0).RequestQueue("q1").BatchAddRequests(context.Background(), requests, false); err != nil {
t.Fatal(err)
}
if got := backend.bodies[0]; len(got) != 2 || got[0].URL != requests[0].URL || got[1].URL != requests[1].URL {
t.Fatalf("batch body: %+v", got)
}
}