SPB Git

Repository Public

pool.test.ts 2.0 KB 57 lines typescript

1// Essais de l'exécution bornée (mapLimit) : plafond de concurrence, ordre des résultats, échecs isolés.2import assert from "node:assert/strict"3import { test } from "node:test"4import { firstRejection, mapLimit } from "./pool"56const tick = (ms: number) => new Promise((r) => setTimeout(r, ms))78test("mapLimit : jamais plus de `limit` appels en vol, tous exécutés", async () => {9  let flying = 010  let peak = 011  const seen: number[] = []12  const out = await mapLimit(Array.from({ length: 23 }, (_, i) => i), 4, async (n) => {13    flying++14    peak = Math.max(peak, flying)15    await tick(2 + (n % 3))16    flying--17    seen.push(n)18    return n * 219  })20  assert.equal(peak, 4)21  assert.equal(seen.length, 23)22  assert.deepEqual(out.map((r) => (r.status === "fulfilled" ? r.value : null)), Array.from({ length: 23 }, (_, i) => i * 2))23})2425test("mapLimit : l'ordre des résultats est celui des éléments, pas celui des fins", async () => {26  const out = await mapLimit([30, 5, 15], 3, async (ms) => {27    await tick(ms)28    return ms29  })30  assert.deepEqual(out, [30, 5, 15].map((value) => ({ status: "fulfilled", value })))31})3233test("mapLimit : un échec n'arrête pas les autres ; firstRejection rend le premier", async () => {34  const boom = new Error("boom")35  const out = await mapLimit([1, 2, 3, 4, 5], 2, async (n) => {36    if (n === 2 || n === 4) throw n === 2 ? boom : new Error("other")37    return n38  })39  assert.deepEqual(out.map((r) => r.status), ["fulfilled", "rejected", "fulfilled", "rejected", "fulfilled"])40  assert.equal(firstRejection(out), boom)41  assert.equal(firstRejection(out.filter((r) => r.status === "fulfilled")), undefined)42})4344test("mapLimit : liste vide, limite nulle ou plus grande que la liste", async () => {45  assert.deepEqual(await mapLimit([], 4, async () => 1), [])46  let flying = 047  let peak = 048  await mapLimit([1, 2, 3], 0, async () => {49    flying++50    peak = Math.max(peak, flying)51    await tick(1)52    flying--53  })54  assert.equal(peak, 1)55  assert.equal((await mapLimit([1, 2], 10, async (n) => n)).length, 2)56})57
Files main
Menu

Theme