Sporalize-api / tests /read-batches.test.ts
Shoraky's picture
Improve backend request performance
82b44e7
Raw History Blame Contribute Delete
920 Bytes
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/);
});