|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { BoundedSerialQueue } from "./bounded-serial-queue.js"; |
| 3 | + |
| 4 | +function deferred<T = void>(): { |
| 5 | + promise: Promise<T>; |
| 6 | + resolve: (value: T) => void; |
| 7 | + reject: (reason: unknown) => void; |
| 8 | +} { |
| 9 | + let resolve!: (value: T) => void; |
| 10 | + let reject!: (reason: unknown) => void; |
| 11 | + const promise = new Promise<T>((accept, fail) => { |
| 12 | + resolve = accept; |
| 13 | + reject = fail; |
| 14 | + }); |
| 15 | + return { promise, resolve, reject }; |
| 16 | +} |
| 17 | + |
| 18 | +describe("BoundedSerialQueue", () => { |
| 19 | + it("runs one task at a time in FIFO order and continues after failures", async () => { |
| 20 | + const first = deferred(); |
| 21 | + const order: string[] = []; |
| 22 | + const queue = new BoundedSerialQueue({ maxPendingCount: 4, maxPendingWeight: 4 }); |
| 23 | + |
| 24 | + const one = queue.enqueue(async () => { |
| 25 | + order.push("one:start"); |
| 26 | + await first.promise; |
| 27 | + order.push("one:end"); |
| 28 | + }); |
| 29 | + const failure = new Error("second failed"); |
| 30 | + const two = queue.enqueue(async () => { |
| 31 | + order.push("two"); |
| 32 | + throw failure; |
| 33 | + }); |
| 34 | + const three = queue.enqueue(async () => { |
| 35 | + order.push("three"); |
| 36 | + return 3; |
| 37 | + }); |
| 38 | + |
| 39 | + expect(one.accepted && two.accepted && three.accepted).toBe(true); |
| 40 | + expect(order).toEqual(["one:start"]); |
| 41 | + const oneCompletion = expect( |
| 42 | + one.accepted ? one.completion : Promise.reject(new Error("first task was not admitted")), |
| 43 | + ).resolves.toBeUndefined(); |
| 44 | + const twoCompletion = expect(two.accepted ? two.completion : Promise.resolve()).rejects.toBe( |
| 45 | + failure, |
| 46 | + ); |
| 47 | + const threeCompletion = expect( |
| 48 | + three.accepted ? three.completion : Promise.reject(new Error("third task was not admitted")), |
| 49 | + ).resolves.toBe(3); |
| 50 | + first.resolve(); |
| 51 | + |
| 52 | + await Promise.all([oneCompletion, twoCompletion, threeCompletion]); |
| 53 | + expect(order).toEqual(["one:start", "one:end", "two", "three"]); |
| 54 | + expect(queue.isIdle).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it("bounds waiting count and weight, then seals on the first overflow", () => { |
| 58 | + const first = deferred(); |
| 59 | + const queue = new BoundedSerialQueue({ maxPendingCount: 2, maxPendingWeight: 5 }); |
| 60 | + const active = queue.enqueue(async () => await first.promise, { weight: 100 }); |
| 61 | + const waiting = queue.enqueue(async () => undefined, { weight: 3 }); |
| 62 | + const overflow = queue.enqueue(async () => undefined, { weight: 3 }); |
| 63 | + const late = queue.enqueue(async () => undefined); |
| 64 | + |
| 65 | + expect(active.accepted).toBe(true); |
| 66 | + expect(waiting.accepted).toBe(true); |
| 67 | + expect(overflow).toEqual({ accepted: false, reason: "overflow" }); |
| 68 | + expect(late).toEqual({ accepted: false, reason: "sealed" }); |
| 69 | + expect(queue.didOverflow).toBe(true); |
| 70 | + first.resolve(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("can reject at capacity without sealing admission", async () => { |
| 74 | + const first = deferred(); |
| 75 | + const queue = new BoundedSerialQueue({ maxPendingCount: 1, maxPendingWeight: 1 }); |
| 76 | + const active = queue.enqueue(async () => await first.promise); |
| 77 | + const waiting = queue.enqueue(async () => undefined); |
| 78 | + |
| 79 | + expect( |
| 80 | + queue.enqueue(async () => undefined, { |
| 81 | + sealOnOverflow: false, |
| 82 | + }), |
| 83 | + ).toEqual({ accepted: false, reason: "capacity" }); |
| 84 | + expect(queue.didOverflow).toBe(false); |
| 85 | + |
| 86 | + first.resolve(); |
| 87 | + await queue.flush(); |
| 88 | + const afterDrain = queue.enqueue(async () => "accepted"); |
| 89 | + expect(afterDrain.accepted).toBe(true); |
| 90 | + if (afterDrain.accepted) { |
| 91 | + await expect(afterDrain.completion).resolves.toBe("accepted"); |
| 92 | + } |
| 93 | + if (active.accepted) { |
| 94 | + await active.completion; |
| 95 | + } |
| 96 | + if (waiting.accepted) { |
| 97 | + await waiting.completion; |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it("flushes only the accepted prefix visible at call time", async () => { |
| 102 | + const first = deferred(); |
| 103 | + const second = deferred(); |
| 104 | + const queue = new BoundedSerialQueue({ maxPendingCount: 2, maxPendingWeight: 2 }); |
| 105 | + const active = queue.enqueue(async () => await first.promise); |
| 106 | + const flush = queue.flush(); |
| 107 | + const repeatedFlushes = Array.from({ length: 10_000 }, () => queue.flush()); |
| 108 | + expect(new Set([flush, ...repeatedFlushes]).size).toBe(1); |
| 109 | + const late = queue.enqueue(async () => await second.promise); |
| 110 | + const flushed = vi.fn(); |
| 111 | + void flush.then(flushed); |
| 112 | + |
| 113 | + first.resolve(); |
| 114 | + await flush; |
| 115 | + |
| 116 | + expect(flushed).toHaveBeenCalledOnce(); |
| 117 | + expect(late.accepted && queue.isIdle).toBe(false); |
| 118 | + second.resolve(); |
| 119 | + await queue.flush(); |
| 120 | + expect(queue.isIdle).toBe(true); |
| 121 | + if (active.accepted) { |
| 122 | + await active.completion; |
| 123 | + } |
| 124 | + if (late.accepted) { |
| 125 | + await late.completion; |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it("seals idempotently while preserving accepted work", async () => { |
| 130 | + const first = deferred(); |
| 131 | + const queue = new BoundedSerialQueue({ maxPendingCount: 1, maxPendingWeight: 1 }); |
| 132 | + const active = queue.enqueue(async () => await first.promise); |
| 133 | + const waiting = queue.enqueue(async () => "done"); |
| 134 | + |
| 135 | + queue.seal(); |
| 136 | + queue.seal(); |
| 137 | + expect(queue.enqueue(async () => undefined)).toEqual({ accepted: false, reason: "sealed" }); |
| 138 | + first.resolve(); |
| 139 | + await queue.flush(); |
| 140 | + |
| 141 | + expect(active.accepted && waiting.accepted).toBe(true); |
| 142 | + if (waiting.accepted) { |
| 143 | + await expect(waiting.completion).resolves.toBe("done"); |
| 144 | + } |
| 145 | + }); |
| 146 | +}); |
0 commit comments