diff --git a/src/utils/run-with-concurrency.test.ts b/src/utils/run-with-concurrency.test.ts index 91725b686e..5cb9f4bf3b 100644 --- a/src/utils/run-with-concurrency.test.ts +++ b/src/utils/run-with-concurrency.test.ts @@ -5,15 +5,34 @@ describe("runTasksWithConcurrency", () => { it("preserves task order with bounded worker count", async () => { let running = 0; let peak = 0; - const tasks = [25, 10, 5, 15].map((delayMs, index) => async (): Promise => { + const resolvers: Array<(() => void) | undefined> = []; + const tasks = [0, 1, 2, 3].map((index) => async (): Promise => { running += 1; peak = Math.max(peak, running); - await new Promise((resolve) => setTimeout(resolve, delayMs)); + await new Promise((resolve) => { + resolvers[index] = resolve; + }); running -= 1; return index + 1; }); - const result = await runTasksWithConcurrency({ tasks, limit: 2 }); + const resultPromise = runTasksWithConcurrency({ tasks, limit: 2 }); + await vi.waitFor(() => { + expect(typeof resolvers[0]).toBe("function"); + expect(typeof resolvers[1]).toBe("function"); + }); + resolvers[1]?.(); + await vi.waitFor(() => { + expect(typeof resolvers[2]).toBe("function"); + }); + resolvers[0]?.(); + await vi.waitFor(() => { + expect(typeof resolvers[3]).toBe("function"); + }); + resolvers[2]?.(); + resolvers[3]?.(); + + const result = await resultPromise; expect(result.hasError).toBe(false); expect(result.firstError).toBeUndefined(); expect(result.results).toEqual([1, 2, 3, 4]);