forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

map-concurrent.test.ts34 lines, 1.0 KiBHistoryRawDownload
1import { mapConcurrent } from "@/lib/map-concurrent";
2
3describe("mapConcurrent", () => {
4 it("keeps input order", async () => {
5 const out = await mapConcurrent([30, 10, 20], 2, async (ms, index) => {
6 await new Promise((resolve) => setTimeout(resolve, ms));
7 return index;
8 });
9 expect(out).toEqual([0, 1, 2]);
10 });
11
12 it("never runs more than `limit` calls at once", async () => {
13 let active = 0;
14 let peak = 0;
15 await mapConcurrent(Array.from({ length: 20 }, (_, i) => i), 3, async () => {
16 active += 1;
17 peak = Math.max(peak, active);
18 await new Promise((resolve) => setTimeout(resolve, 1));
19 active -= 1;
20 });
21 expect(peak).toBe(3);
22 });
23
24 it("returns an empty array for no items", async () => {
25 expect(await mapConcurrent([], 4, async () => 1)).toEqual([]);
26 });
27
28 it("rejects when a call rejects", async () => {
29 await expect(mapConcurrent([1, 2], 2, async (n) => {
30 if (n === 2) throw new Error("boom");
31 return n;
32 })).rejects.toThrow("boom");
33 });
34});