33 lines
979 B
TypeScript
33 lines
979 B
TypeScript
import { test } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { dedupeInFlight } from './inFlight';
|
||
|
|
|
||
|
|
test('dedupeInFlight: one request per key while pending, a new one after (#318)', async () => {
|
||
|
|
const once = dedupeInFlight<number>();
|
||
|
|
let calls = 0;
|
||
|
|
let release!: () => void;
|
||
|
|
const gate = new Promise<void>((r) => {
|
||
|
|
release = r;
|
||
|
|
});
|
||
|
|
const run = async () => {
|
||
|
|
calls += 1;
|
||
|
|
await gate;
|
||
|
|
return calls;
|
||
|
|
};
|
||
|
|
const a = once('d1', run);
|
||
|
|
const b = once('d1', run);
|
||
|
|
const c = once('d2', run);
|
||
|
|
assert.equal(a, b);
|
||
|
|
release();
|
||
|
|
assert.deepEqual(await Promise.all([a, b, c]), [2, 2, 2]);
|
||
|
|
assert.equal(calls, 2);
|
||
|
|
await once('d1', run);
|
||
|
|
assert.equal(calls, 3, 'settled requests are not reused');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('dedupeInFlight: a rejection clears the key', async () => {
|
||
|
|
const once = dedupeInFlight<void>();
|
||
|
|
await assert.rejects(once('k', () => Promise.reject(new Error('boom'))));
|
||
|
|
await once('k', () => Promise.resolve());
|
||
|
|
});
|