Repository navigation
Expand file tree
/
Copy pathsequin_test.go
More file actions
468 lines (369 loc) · 11.8 KB
/
Copy pathsequin_test.go
File metadata and controls
468 lines (369 loc) · 11.8 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package sequin
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProcessor(t *testing.T) {
t.Run("configuration", func(t *testing.T) {
t.Run("validates options", func(t *testing.T) {
tests := []struct {
name string
opts ProcessorOptions
want error
}{
{
name: "negative MaxBatchSize",
opts: ProcessorOptions{MaxBatchSize: -1},
want: errors.New("MaxBatchSize must be >= 0"),
},
{
name: "negative MaxConcurrent",
opts: ProcessorOptions{MaxConcurrent: -1},
want: errors.New("MaxConcurrent must be >= 0"),
},
{
name: "invalid prefetching",
opts: ProcessorOptions{
Prefetching: &PrefetchingOptions{BufferSize: 0},
},
want: errors.New("BufferSize must be > 0"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := newMockClient()
handler := func(context.Context, []Message) error { return nil }
_, err := NewProcessor(client, "test-group", handler, tt.opts)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.want.Error())
})
}
})
t.Run("applies defaults", func(t *testing.T) {
client := newMockClient()
handler := func(context.Context, []Message) error { return nil }
p, err := NewProcessor(client, "test-group", handler, ProcessorOptions{})
require.NoError(t, err)
assert.Equal(t, 1, p.opts.MaxBatchSize)
assert.Equal(t, 1, p.opts.MaxConcurrent)
assert.Equal(t, 1, p.opts.FetchBatchSize)
assert.Nil(t, p.opts.Prefetching)
})
})
t.Run("basic processing", func(t *testing.T) {
t.Run("processes single message", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
msgs := generateTestMessages(1)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 1,
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Create a channel to signal when processing is complete
done := make(chan error, 1)
go func() {
done <- p.Run(ctx)
}()
// Wait for either processing to complete or timeout
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(500 * time.Millisecond):
t.Fatal("processor did not complete in time")
}
// Verify the message was processed
processed := processor.processedMessages()
require.Len(t, processed, 1)
require.Equal(t, msgs, processed[0])
// Verify the message was acknowledged
acked := client.acknowledgedMessages()
require.Len(t, acked, 1)
require.Equal(t, msgs[0].AckID, acked[0])
})
t.Run("processes batches", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
msgs := generateTestMessages(25) // More than MaxBatchSize
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 10,
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
// Wait for processing
time.Sleep(50 * time.Millisecond)
cancel()
require.NoError(t, <-errCh)
processed := processor.processedMessages()
// Verify batch sizes
var totalProcessed int
for _, batch := range processed {
assert.LessOrEqual(t, len(batch), 10)
totalProcessed += len(batch)
}
assert.Equal(t, 25, totalProcessed)
// Verify all messages were acknowledged
acked := client.acknowledgedMessages()
assert.Len(t, acked, 25)
})
})
t.Run("concurrent processing", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
processor.processDelay = 10 * time.Millisecond
msgs := generateTestMessages(50)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 5,
MaxConcurrent: 3,
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
start := time.Now()
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
time.Sleep(50 * time.Millisecond)
cancel()
require.NoError(t, <-errCh)
duration := time.Since(start)
// With 50 messages, batch size 5, and 3 concurrent processors,
// it should take ~20ms (2 rounds of processing)
assert.Less(t, duration, 60*time.Millisecond)
processed := processor.processedMessages()
var totalProcessed int
for _, batch := range processed {
totalProcessed += len(batch)
}
assert.Equal(t, 50, totalProcessed)
})
t.Run("prefetching", func(t *testing.T) {
t.Run("buffers messages", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
processor.processDelay = 10 * time.Millisecond
msgs := generateTestMessages(100)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 10,
MaxConcurrent: 2,
Prefetching: &PrefetchingOptions{
BufferSize: 30,
},
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
// Wait for initial prefetch
time.Sleep(20 * time.Millisecond)
// Check received batch sizes
sizes := client.receivedBatchSizes()
assert.NotEmpty(t, sizes)
// Should have made multiple receive calls to fill buffer
assert.True(t, len(sizes) > 1)
cancel()
require.NoError(t, <-errCh)
})
})
t.Run("error handling", func(t *testing.T) {
t.Run("handles processor errors", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
processor.err = errors.New("processing failed")
var errorHandlerCalled bool
errorHandler := func(_ context.Context, msgs []Message, err error) {
errorHandlerCalled = true
assert.EqualError(t, err, "handler failed: processing failed")
}
msgs := generateTestMessages(1)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 1,
ErrorHandler: errorHandler,
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
time.Sleep(50 * time.Millisecond)
cancel()
require.NoError(t, <-errCh)
assert.True(t, errorHandlerCalled)
// Message should not have been acknowledged
acked := client.acknowledgedMessages()
assert.Empty(t, acked)
})
t.Run("handles client errors", func(t *testing.T) {
client := newMockClient()
client.receiveErr = errors.New("receive failed")
processor := newTestProcessorFunc()
var errorHandlerCalled bool
errorHandler := func(_ context.Context, msgs []Message, err error) {
errorHandlerCalled = true
assert.Contains(t, err.Error(), "receive failed")
}
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 1,
ErrorHandler: errorHandler,
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
time.Sleep(50 * time.Millisecond)
cancel()
require.NoError(t, <-errCh)
assert.True(t, errorHandlerCalled)
})
})
t.Run("shutdown", func(t *testing.T) {
t.Run("completes in-flight messages", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
processor.processDelay = 50 * time.Millisecond
msgs := generateTestMessages(10)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 5,
MaxConcurrent: 2,
})
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
// Wait for processing to start
time.Sleep(20 * time.Millisecond)
// Cancel context and wait for shutdown
cancel()
// Should complete without error
require.NoError(t, <-errCh)
// Check that messages that were in-flight were completed
processed := processor.processedMessages()
acked := client.acknowledgedMessages()
assert.NotEmpty(t, processed)
assert.NotEmpty(t, acked)
})
t.Run("drains prefetch buffer", func(t *testing.T) {
client := newMockClient()
processor := newTestProcessorFunc()
msgs := generateTestMessages(20)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 5,
MaxConcurrent: 2,
Prefetching: &PrefetchingOptions{
BufferSize: 10,
},
})
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
// Wait for buffer to fill
time.Sleep(50 * time.Millisecond)
// Cancel context
cancel()
require.NoError(t, <-errCh)
// Verify messages in buffer were processed
processed := processor.processedMessages()
var totalProcessed int
for _, batch := range processed {
totalProcessed += len(batch)
}
assert.True(t, totalProcessed >= 10, "Should process at least buffered messages")
})
t.Run("stops receiving after shutdown", func(t *testing.T) {
client := newMockClient()
client.receiveDelay = 10 * time.Millisecond
processor := newTestProcessorFunc()
msgs := generateTestMessages(100)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 5,
MaxConcurrent: 2,
})
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
errCh := make(chan error, 1)
go func() {
errCh <- p.Run(ctx)
}()
// Let it process some messages
time.Sleep(30 * time.Millisecond)
// Get current receive count
initialReceiveCount := client.receiveCount
// Cancel and wait for shutdown
cancel()
require.NoError(t, <-errCh)
// Wait a bit to ensure no more receives
time.Sleep(20 * time.Millisecond)
// Should have minimal additional receives during shutdown
finalReceiveCount := client.receiveCount
assert.Less(t, finalReceiveCount-initialReceiveCount, 3,
"Should not make many new receives during shutdown")
})
})
t.Run("stress test", func(t *testing.T) {
if testing.Short() {
t.Skip("Skipping stress test in short mode")
}
client := newMockClient()
processor := newTestProcessorFunc()
processor.processDelay = 1 * time.Millisecond
msgs := generateTestMessages(1000)
client.setMessages(msgs)
p, err := NewProcessor(client, "test-group", processor.handler, ProcessorOptions{
MaxBatchSize: 20,
MaxConcurrent: 5,
Prefetching: &PrefetchingOptions{
BufferSize: 100,
},
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
start := time.Now()
err = p.Run(ctx)
duration := time.Since(start)
require.ErrorIs(t, err, context.DeadlineExceeded)
processed := processor.processedMessages()
var totalProcessed int
for _, batch := range processed {
totalProcessed += len(batch)
assert.LessOrEqual(t, len(batch), 20, "Batch size limit respected")
}
t.Logf("Processed %d messages in %v", totalProcessed, duration)
assert.GreaterOrEqual(t, totalProcessed, 500, "Should process many messages")
// Verify acknowledgments
acked := client.acknowledgedMessages()
assert.Equal(t, len(acked), totalProcessed, "All processed messages should be acknowledged")
})
}