-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.test.ts
More file actions
779 lines (667 loc) · 26.2 KB
/
index.test.ts
File metadata and controls
779 lines (667 loc) · 26.2 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import { redisTest } from "@internal/testcontainers";
import { describe, expect, vi } from "vitest";
import { BatchQueue } from "../index.js";
import type { GlobalRateLimiter } from "@trigger.dev/redis-worker";
import type { CompleteBatchResult, InitializeBatchOptions, BatchItem } from "../types.js";
vi.setConfig({ testTimeout: 60_000 });
describe("BatchQueue", () => {
function createBatchQueue(
redisContainer: { getHost: () => string; getPort: () => number },
options?: { startConsumers?: boolean }
) {
return new BatchQueue({
redis: {
host: redisContainer.getHost(),
port: redisContainer.getPort(),
keyPrefix: "test:",
},
drr: {
quantum: 5,
maxDeficit: 50,
},
consumerCount: 1,
consumerIntervalMs: 50,
startConsumers: options?.startConsumers ?? false, // Don't start by default in tests
});
}
function createInitOptions(
batchId: string,
envId: string,
runCount: number
): InitializeBatchOptions {
return {
batchId,
friendlyId: `friendly_${batchId}`,
environmentId: envId,
environmentType: "DEVELOPMENT",
organizationId: "org123",
projectId: "proj123",
runCount,
};
}
function createBatchItems(count: number): BatchItem[] {
return Array.from({ length: count }, (_, i) => ({
task: `task-${i}`,
payload: JSON.stringify({ index: i }),
payloadType: "application/json",
options: { tags: [`tag-${i}`] },
}));
}
async function enqueueItems(
queue: BatchQueue,
batchId: string,
envId: string,
items: BatchItem[]
): Promise<void> {
for (let i = 0; i < items.length; i++) {
await queue.enqueueBatchItem(batchId, envId, i, items[i]);
}
}
describe("initializeBatch + enqueueBatchItem (2-phase API)", () => {
redisTest("should initialize a batch successfully", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer);
try {
const options = createInitOptions("batch1", "env1", 5);
await queue.initializeBatch(options);
// Verify batch metadata was stored
const meta = await queue.getBatchMeta("batch1");
expect(meta).not.toBeNull();
expect(meta?.batchId).toBe("batch1");
expect(meta?.environmentId).toBe("env1");
expect(meta?.runCount).toBe(5);
} finally {
await queue.close();
}
});
redisTest("should enqueue items and track remaining count", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer);
try {
await queue.initializeBatch(createInitOptions("batch1", "env1", 10));
const items = createBatchItems(10);
await enqueueItems(queue, "batch1", "env1", items);
const count = await queue.getBatchRemainingCount("batch1");
expect(count).toBe(10);
} finally {
await queue.close();
}
});
redisTest("should enqueue multiple batches", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer);
try {
await queue.initializeBatch(createInitOptions("batch1", "env1", 5));
await queue.initializeBatch(createInitOptions("batch2", "env1", 3));
await queue.initializeBatch(createInitOptions("batch3", "env2", 7));
await enqueueItems(queue, "batch1", "env1", createBatchItems(5));
await enqueueItems(queue, "batch2", "env1", createBatchItems(3));
await enqueueItems(queue, "batch3", "env2", createBatchItems(7));
expect(await queue.getBatchRemainingCount("batch1")).toBe(5);
expect(await queue.getBatchRemainingCount("batch2")).toBe(3);
expect(await queue.getBatchRemainingCount("batch3")).toBe(7);
} finally {
await queue.close();
}
});
redisTest("should store batch metadata correctly", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer);
try {
const options: InitializeBatchOptions = {
batchId: "batch1",
friendlyId: "batch_abc123",
environmentId: "env1",
environmentType: "PRODUCTION",
organizationId: "org456",
projectId: "proj789",
runCount: 1,
parentRunId: "run_parent",
resumeParentOnCompletion: true,
triggerVersion: "1.0.0",
spanParentAsLink: true,
idempotencyKey: "idem123",
};
await queue.initializeBatch(options);
await queue.enqueueBatchItem("batch1", "env1", 0, {
task: "my-task",
payload: '{"data": true}',
});
const meta = await queue.getBatchMeta("batch1");
expect(meta).not.toBeNull();
expect(meta?.friendlyId).toBe("batch_abc123");
expect(meta?.environmentType).toBe("PRODUCTION");
expect(meta?.organizationId).toBe("org456");
expect(meta?.projectId).toBe("proj789");
expect(meta?.parentRunId).toBe("run_parent");
expect(meta?.resumeParentOnCompletion).toBe(true);
expect(meta?.triggerVersion).toBe("1.0.0");
expect(meta?.spanParentAsLink).toBe(true);
expect(meta?.idempotencyKey).toBe("idem123");
} finally {
await queue.close();
}
});
redisTest("should deduplicate items with same index", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer);
try {
await queue.initializeBatch(createInitOptions("batch1", "env1", 2));
const item: BatchItem = { task: "task-0", payload: '{"index": 0}' };
// First enqueue should succeed
const result1 = await queue.enqueueBatchItem("batch1", "env1", 0, item);
expect(result1.enqueued).toBe(true);
// Second enqueue with same index should be deduplicated
const result2 = await queue.enqueueBatchItem("batch1", "env1", 0, item);
expect(result2.enqueued).toBe(false);
// Different index should succeed
const result3 = await queue.enqueueBatchItem("batch1", "env1", 1, item);
expect(result3.enqueued).toBe(true);
} finally {
await queue.close();
}
});
});
describe("processing callbacks", () => {
redisTest("should call process callback for each item", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
const processedItems: Array<{ batchId: string; itemIndex: number; task: string }> = [];
let completionResult: CompleteBatchResult | null = null;
try {
// Set up callbacks
queue.onProcessItem(async ({ batchId, itemIndex, item }) => {
processedItems.push({ batchId, itemIndex, task: item.task });
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
// Initialize and enqueue a small batch
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
// Wait for processing
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Verify all items were processed
expect(processedItems).toHaveLength(3);
expect(processedItems.map((p) => p.itemIndex).sort()).toEqual([0, 1, 2]);
// Verify completion result
expect(completionResult!.batchId).toBe("batch1");
expect(completionResult!.successfulRunCount).toBe(3);
expect(completionResult!.failedRunCount).toBe(0);
expect(completionResult!.runIds).toEqual(["run_0", "run_1", "run_2"]);
} finally {
await queue.close();
}
});
redisTest("should handle processing failures", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionResult: CompleteBatchResult | null = null;
try {
// Set up callbacks - fail item 1
queue.onProcessItem(async ({ itemIndex }) => {
if (itemIndex === 1) {
return { success: false, error: "Task failed", errorCode: "TASK_ERROR" };
}
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Verify mixed results
expect(completionResult!.successfulRunCount).toBe(2);
expect(completionResult!.failedRunCount).toBe(1);
expect(completionResult!.failures).toHaveLength(1);
expect(completionResult!.failures[0].index).toBe(1);
expect(completionResult!.failures[0].error).toBe("Task failed");
expect(completionResult!.failures[0].errorCode).toBe("TASK_ERROR");
} finally {
await queue.close();
}
});
redisTest("should handle callback exceptions", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionResult: CompleteBatchResult | null = null;
try {
// Set up callbacks - throw exception on item 0
queue.onProcessItem(async ({ itemIndex }) => {
if (itemIndex === 0) {
throw new Error("Unexpected error");
}
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 2));
await enqueueItems(queue, "batch1", "env1", createBatchItems(2));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Exception should be recorded as failure
expect(completionResult!.failedRunCount).toBe(1);
expect(completionResult!.failures[0].error).toBe("Unexpected error");
expect(completionResult!.failures[0].errorCode).toBe("UNEXPECTED_ERROR");
} finally {
await queue.close();
}
});
});
describe("consumer lifecycle", () => {
redisTest("should start and stop consumers", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: false });
try {
// Start consumers
queue.start();
// Should be able to stop without error
await queue.stop();
// Should be able to start again
queue.start();
} finally {
await queue.close();
}
});
redisTest(
"should process items only when consumers are started",
async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: false });
const processedItems: number[] = [];
let completionCalled = false;
try {
queue.onProcessItem(async ({ itemIndex }) => {
processedItems.push(itemIndex);
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async () => {
completionCalled = true;
});
// Enqueue batch without starting consumers
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
// Wait a bit - nothing should be processed
await new Promise((resolve) => setTimeout(resolve, 200));
expect(processedItems).toHaveLength(0);
// Now start consumers
queue.start();
// Wait for processing
await vi.waitFor(
() => {
expect(completionCalled).toBe(true);
},
{ timeout: 5000 }
);
expect(processedItems).toHaveLength(3);
} finally {
await queue.close();
}
}
);
});
describe("fair scheduling (DRR)", () => {
redisTest(
"should process batches from multiple environments fairly",
async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
const processedByEnv: Record<string, number[]> = { env1: [], env2: [] };
const completedBatches: string[] = [];
try {
queue.onProcessItem(async ({ itemIndex, meta }) => {
processedByEnv[meta.environmentId].push(itemIndex);
return { success: true, runId: `run_${meta.environmentId}_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completedBatches.push(result.batchId);
});
// Initialize and enqueue batches for two environments
await queue.initializeBatch(createInitOptions("batch1", "env1", 20));
await queue.initializeBatch(createInitOptions("batch2", "env2", 20));
await enqueueItems(queue, "batch1", "env1", createBatchItems(20));
await enqueueItems(queue, "batch2", "env2", createBatchItems(20));
// Wait for both to complete
await vi.waitFor(
() => {
expect(completedBatches).toHaveLength(2);
},
{ timeout: 10000 }
);
// Both environments should have been processed
expect(processedByEnv.env1).toHaveLength(20);
expect(processedByEnv.env2).toHaveLength(20);
} finally {
await queue.close();
}
}
);
redisTest("should not let one environment monopolize", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
const processOrder: string[] = [];
try {
queue.onProcessItem(async ({ meta }) => {
processOrder.push(meta.environmentId);
// Small delay to simulate work
await new Promise((resolve) => setTimeout(resolve, 5));
return { success: true, runId: `run_${Date.now()}` };
});
// Initialize and enqueue env1 with many items first
await queue.initializeBatch(createInitOptions("batch1", "env1", 30));
await enqueueItems(queue, "batch1", "env1", createBatchItems(30));
// Small delay then enqueue env2
await new Promise((resolve) => setTimeout(resolve, 50));
await queue.initializeBatch(createInitOptions("batch2", "env2", 10));
await enqueueItems(queue, "batch2", "env2", createBatchItems(10));
// Wait for env2 batch to complete
await vi.waitFor(
() => {
const env2Count = processOrder.filter((e) => e === "env2").length;
expect(env2Count).toBe(10);
},
{ timeout: 10000 }
);
// Check that env2 items were interleaved, not all at the end
// Find first env2 item position
const firstEnv2Index = processOrder.indexOf("env2");
// Env2 should appear before all env1 items are processed
expect(firstEnv2Index).toBeLessThan(30);
} finally {
await queue.close();
}
});
});
describe("batch results", () => {
redisTest("should track successful runs in completion result", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 5));
await enqueueItems(queue, "batch1", "env1", createBatchItems(5));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Verify completion result contains all runs
// Note: After completion, batch data is cleaned up from Redis
expect(completionResult!.batchId).toBe("batch1");
expect(completionResult!.successfulRunCount).toBe(5);
expect(completionResult!.failedRunCount).toBe(0);
expect(completionResult!.runIds).toHaveLength(5);
expect(completionResult!.runIds).toContain("run_0");
expect(completionResult!.runIds).toContain("run_4");
} finally {
await queue.close();
}
});
redisTest(
"should track failures with details in completion result",
async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex, item }) => {
if (itemIndex % 2 === 0) {
return {
success: false,
error: `Error on ${item.task}`,
errorCode: "VALIDATION_ERROR",
};
}
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 4));
await enqueueItems(queue, "batch1", "env1", createBatchItems(4));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Verify completion result has failure details
// Note: After completion, batch data is cleaned up from Redis
expect(completionResult!.batchId).toBe("batch1");
expect(completionResult!.successfulRunCount).toBe(2); // Items 1 and 3 succeeded
expect(completionResult!.failedRunCount).toBe(2); // Items 0 and 2 failed
expect(completionResult!.failures).toHaveLength(2);
for (const failure of completionResult!.failures) {
expect(failure.errorCode).toBe("VALIDATION_ERROR");
expect(failure.taskIdentifier).toMatch(/^task-\d+$/);
expect(failure.error).toMatch(/^Error on task-\d+$/);
expect([0, 2]).toContain(failure.index); // Even indices failed
}
} finally {
await queue.close();
}
}
);
redisTest("should preserve order of successful runs", async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 10));
await enqueueItems(queue, "batch1", "env1", createBatchItems(10));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 5000 }
);
// Runs should be in order since items are processed sequentially
expect(completionResult!.runIds).toEqual([
"run_0",
"run_1",
"run_2",
"run_3",
"run_4",
"run_5",
"run_6",
"run_7",
"run_8",
"run_9",
]);
} finally {
await queue.close();
}
});
});
describe("completion callback error handling", () => {
redisTest(
"should preserve Redis data when completion callback throws an error",
async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let callbackCallCount = 0;
let lastCompletionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
callbackCallCount++;
lastCompletionResult = result;
// Simulate database failure on first attempt
if (callbackCallCount === 1) {
throw new Error("Database temporarily unavailable");
}
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
// Wait for completion callback to be called (and fail)
await vi.waitFor(
() => {
expect(callbackCallCount).toBeGreaterThanOrEqual(1);
},
{ timeout: 5000 }
);
// Redis data should still exist after callback failure
const meta = await queue.getBatchMeta("batch1");
expect(meta).not.toBeNull();
expect(meta?.batchId).toBe("batch1");
// Verify the completion result was correct
expect(lastCompletionResult).not.toBeNull();
expect(lastCompletionResult!.batchId).toBe("batch1");
expect(lastCompletionResult!.successfulRunCount).toBe(3);
expect(lastCompletionResult!.runIds).toHaveLength(3);
} finally {
await queue.close();
}
}
);
redisTest(
"should cleanup Redis data when completion callback succeeds",
async ({ redisContainer }) => {
const queue = createBatchQueue(redisContainer, { startConsumers: true });
let completionCalled = false;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async () => {
completionCalled = true;
// Callback succeeds - no error thrown
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
// Wait for completion
await vi.waitFor(
() => {
expect(completionCalled).toBe(true);
},
{ timeout: 5000 }
);
// Small delay to ensure cleanup has occurred
await new Promise((resolve) => setTimeout(resolve, 100));
// Redis data should be cleaned up after successful callback
const meta = await queue.getBatchMeta("batch1");
expect(meta).toBeNull();
} finally {
await queue.close();
}
}
);
});
describe("global rate limiter at worker queue consumer level", () => {
redisTest(
"should call rate limiter before each processing attempt",
async ({ redisContainer }) => {
let limitCallCount = 0;
const rateLimiter: GlobalRateLimiter = {
async limit() {
limitCallCount++;
return { allowed: true };
},
};
const queue = new BatchQueue({
redis: {
host: redisContainer.getHost(),
port: redisContainer.getPort(),
keyPrefix: "test:",
},
drr: { quantum: 5, maxDeficit: 50 },
consumerCount: 1,
consumerIntervalMs: 50,
startConsumers: true,
globalRateLimiter: rateLimiter,
});
let completionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
const itemCount = 5;
await queue.initializeBatch(createInitOptions("batch1", "env1", itemCount));
await enqueueItems(queue, "batch1", "env1", createBatchItems(itemCount));
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 10000 }
);
expect(completionResult!.successfulRunCount).toBe(itemCount);
// Rate limiter is called before each blockingPop, including iterations
// where no message is available, so count >= items processed
expect(limitCallCount).toBeGreaterThanOrEqual(itemCount);
} finally {
await queue.close();
}
}
);
redisTest(
"should delay processing when rate limited",
async ({ redisContainer }) => {
let limitCallCount = 0;
const rateLimiter: GlobalRateLimiter = {
async limit() {
limitCallCount++;
// Rate limit the first 3 calls, then allow
if (limitCallCount <= 3) {
return { allowed: false, resetAt: Date.now() + 100 };
}
return { allowed: true };
},
};
const queue = new BatchQueue({
redis: {
host: redisContainer.getHost(),
port: redisContainer.getPort(),
keyPrefix: "test:",
},
drr: { quantum: 5, maxDeficit: 50 },
consumerCount: 1,
consumerIntervalMs: 50,
startConsumers: true,
globalRateLimiter: rateLimiter,
});
let completionResult: CompleteBatchResult | null = null;
try {
queue.onProcessItem(async ({ itemIndex }) => {
return { success: true, runId: `run_${itemIndex}` };
});
queue.onBatchComplete(async (result) => {
completionResult = result;
});
await queue.initializeBatch(createInitOptions("batch1", "env1", 3));
await enqueueItems(queue, "batch1", "env1", createBatchItems(3));
// Should still complete despite initial rate limiting
await vi.waitFor(
() => {
expect(completionResult).not.toBeNull();
},
{ timeout: 10000 }
);
expect(completionResult!.successfulRunCount).toBe(3);
// Rate limiter was called more times than items due to initial rejections
expect(limitCallCount).toBeGreaterThan(3);
} finally {
await queue.close();
}
}
);
});
});