Spaces:
Running
Running
File size: 920 Bytes
82b44e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import assert from "node:assert/strict";
import { test } from "node:test";
import { readBatches } from "../src/read-batches.js";
test("caps concurrent reads, keeps result order and includes the final partial batch", async () => {
let active = 0;
let peak = 0;
const input = Array.from({ length: 23 }, (_, index) => index);
const output = await readBatches(input, async (batch) => {
peak = Math.max(peak, ++active);
await new Promise((resolve) => setTimeout(resolve, batch[0] % 2 ? 1 : 5));
active--;
return batch;
}, 5, 3);
assert.equal(peak, 3);
assert.deepEqual(output, input);
});
test("does not query empty input or return incomplete results after failure", async () => {
assert.deepEqual(await readBatches([], () => { throw new Error("should not run"); }), []);
await assert.rejects(readBatches([1, 2], async () => { throw new Error("DB unavailable"); }), /DB unavailable/);
});
|