Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271/** Tests for FIFO queue semantics of scheduled calls, exercised through the public `pLimit` entry point (the queue itself is package-internal). @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { pLimit, } from '../dist/final/neutral/index.mjs';
import { createGate, type Gate, yieldTurn,} from './test-support.ts';
await describe({ name: 'task queue behavior', children: [ it({ name: 'admits queued calls in FIFO order one slot at a time', fn: async () => { const limit = pLimit(1,); /** Start order recorded by every call as its function runs. */ const started: string[] = []; /** Gates keyed by call id, released in a deliberate order. */ const gates = new Map<string, Gate>(); for (const id of [ 'first', 'second', 'third', ]) { /** Gate for this call, opened by the test below. */ const gate = createGate(); gates.set(id, gate); void limit({ fn: async function gatedCall(): Promise<string> { started.push(id,); await gate.open; return id; }, args: [], },); }
await yieldTurn(); expect(started,).toEqual([ 'first', ],);
gates.get('first',) ?.release(); await yieldTurn(); expect(started,).toEqual([ 'first', 'second', ],);
gates.get('second',) ?.release(); gates.get('third',) ?.release(); await yieldTurn(); expect(started,).toEqual([ 'first', 'second', 'third', ],); }, },),
it({ name: 'admits a whole freed batch in FIFO order', fn: async () => { const limit = pLimit(1,); /** Start order recorded by every call as its function runs. */ const started: string[] = []; /** Gates for the three blocked calls, released at the end. */ const gates: Gate[] = []; for (const id of [ 'a', 'b', 'c', ]) { /** Gate for this call, opened by the test below. */ const gate = createGate(); gates.push(gate,); void limit({ fn: async function gatedCall(): Promise<string> { started.push(id,); await gate.open; return id; }, args: [], },); }
await yieldTurn(); expect(started,).toEqual([ 'a', ],); expect(limit.pendingCount,).toBe(2,);
limit.concurrency = 3; await yieldTurn(); expect(started,).toEqual([ 'a', 'b', 'c', ],); expect(limit.pendingCount,).toBe(0,); for (const gate of gates) gate.release(); await yieldTurn(); expect(limit.activeCount,).toBe(0,); }, },),
it({ name: 'reports queued calls through pendingCount and empties the queue in order', fn: async () => { const limit = pLimit(2,); /** Gates for the two calls that occupy both slots immediately. */ const gates = [ createGate(), createGate(), ]; for (const gate of gates) void limit({ fn: async function blockedCall(): Promise<void> { await gate.open; }, args: [], },);
void limit({ fn: function queuedCall(): number { return 1; }, args: [], },); void limit({ fn: function queuedCallTwo(): number { return 2; }, args: [], },); await yieldTurn(); expect(limit.pendingCount,).toBe(2,); expect(limit.activeCount,).toBe(2,);
for (const gate of gates) gate.release(); await yieldTurn(); expect(limit.pendingCount,).toBe(0,); expect(limit.activeCount,).toBe(0,); }, },), it({ name: 'reports queued calls through pendingCount while a consumed prefix remains', fn: async () => { const limit = pLimit(1,); /** Gates for the five gated calls, released one at a time. */ const gates: Gate[] = Array.from( { length: 5, }, function makeGate(): Gate { return createGate(); }, ); for (const gate of gates) void limit({ fn: async function gatedCall(): Promise<void> { await gate.open; }, args: [], },);
await yieldTurn(); expect(limit.pendingCount,).toBe(4,);
gates[0]?.release(); await yieldTurn(); expect(limit.pendingCount,).toBe(3,);
for (const gate of gates) gate.release(); await yieldTurn(); expect(limit.pendingCount,).toBe(0,); }, },),
it({ name: 'clearQueue drops only queued calls and leaves the running call to finish', fn: async () => { const limit = pLimit({ concurrency: 1, rejectOnClear: true, },); /** Gate held by the single running call. */ const gate = createGate(); const running = limit({ fn: async function blockedRunning(): Promise<string> { await gate.open; return 'running'; }, args: [], },); /** Promises of the two queued calls, enqueued in order. */ const dropped = [ limit({ fn: function firstDropped(): string { return 'first'; }, args: [], },), limit({ fn: function secondDropped(): string { return 'second'; }, args: [], },), ]; await yieldTurn(); expect(limit.pendingCount,).toBe(2,);
limit.clearQueue(); /** Outcomes of the two dropped calls, which must both reject. */ const outcomes = await Promise.allSettled(dropped,); expect(outcomes.map(function toStatus(outcome: PromiseSettledResult<string>,): string { return outcome.status; },),).toEqual([ 'rejected', 'rejected', ],);
gate.release(); expect(await running,).toBe('running',); }, },), ],},);