mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-03-22 03:08:23 +00:00
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { runWithConcurrencyLimit } from "./concurrency";
|
|
|
|
describe("runWithConcurrencyLimit", () => {
|
|
it("should process items without exceeding the configured concurrency", async () => {
|
|
// Given
|
|
const items = Array.from({ length: 12 }, (_, index) => index + 1);
|
|
let activeTasks = 0;
|
|
let maxActiveTasks = 0;
|
|
|
|
// When
|
|
const results = await runWithConcurrencyLimit(items, 4, async (item) => {
|
|
activeTasks += 1;
|
|
maxActiveTasks = Math.max(maxActiveTasks, activeTasks);
|
|
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
activeTasks -= 1;
|
|
return item * 2;
|
|
});
|
|
|
|
// Then
|
|
expect(maxActiveTasks).toBeLessThanOrEqual(4);
|
|
expect(results).toEqual(items.map((item) => item * 2));
|
|
});
|
|
|
|
it("should reject when worker throws an uncaught error", async () => {
|
|
// Given
|
|
const items = [1, 2, 3];
|
|
|
|
// When / Then
|
|
await expect(
|
|
runWithConcurrencyLimit(items, 2, async (item) => {
|
|
if (item === 2) {
|
|
throw new Error("boom");
|
|
}
|
|
return item;
|
|
}),
|
|
).rejects.toThrow("boom");
|
|
});
|
|
});
|