diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-18 12:37:11 +0000 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-18 12:37:11 +0000 |
| commit | 358cf43172f0028b079c49fa16f376e400d6cc25 (patch) | |
| tree | bfcd8a9ca5f2bff967a01e756794c9c318e10a90 /castle-search/test | |
| parent | ea10a2f1c5f7a9aae2fea99894df21b76d126d27 (diff) | |
castle-search: X reads 30 s ± a random 0-25 s apart, not a fixed 10 s
An even 10 s beat allowed 30 page loads in five minutes, which reads as
a machine. Drawn afresh for each read.
Diffstat (limited to 'castle-search/test')
| -rw-r--r-- | castle-search/test/x-limits.test.ts | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/castle-search/test/x-limits.test.ts b/castle-search/test/x-limits.test.ts index b239f5b..6be5f7d 100644 --- a/castle-search/test/x-limits.test.ts +++ b/castle-search/test/x-limits.test.ts @@ -7,14 +7,16 @@ import { type Clock, XLimiter, XRateLimitError } from "../src/x-limits.ts"; const fakeClock = () => { let now = 1_000_000; const waits: number[] = []; + let draw = 0.5; const clock: Clock = { + random: () => draw, now: () => now, sleep: async (ms) => { waits.push(ms); now += ms; }, }; - return { clock, waits, advance: (ms: number) => (now += ms) }; + return { clock, waits, advance: (ms: number) => (now += ms), setDraw: (r: number) => (draw = r) }; }; const signal = new AbortController().signal; @@ -22,7 +24,7 @@ const ok = async () => "ok"; test("a search sooner than the gap waits out the rest of it", async () => { const { clock, waits, advance } = fakeClock(); - const l = new XLimiter(clock, { gap: 10_000, hour: 30, day: 150 }); + const l = new XLimiter(clock, { gap: 10_000, jitter: 0, hour: 30, day: 150 }); await l.run(signal, ok); advance(3_000); await l.run(signal, ok); @@ -31,7 +33,7 @@ test("a search sooner than the gap waits out the rest of it", async () => { test("over the hourly cap a search fails at once, naming when it can run", async () => { const { clock, advance } = fakeClock(); - const l = new XLimiter(clock, { gap: 0, hour: 2, day: 150 }); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 2, day: 150 }); await l.run(signal, ok); await l.run(signal, ok); await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /last hour/.test(e.message)); @@ -41,7 +43,7 @@ test("over the hourly cap a search fails at once, naming when it can run", async test("the daily cap holds across hours", async () => { const { clock, advance } = fakeClock(); - const l = new XLimiter(clock, { gap: 0, hour: 100, day: 3 }); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 100, day: 3 }); for (let i = 0; i < 3; i++) { await l.run(signal, ok); advance(3_600_001); @@ -51,7 +53,7 @@ test("the daily cap holds across hours", async () => { test("one at a time: the second starts only after the first finishes", async () => { const { clock } = fakeClock(); - const l = new XLimiter(clock, { gap: 0, hour: 30, day: 150 }); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); const order: string[] = []; let release!: () => void; const first = l.run(signal, async () => { @@ -70,9 +72,27 @@ test("one at a time: the second starts only after the first finishes", async () test("a failed search does not block the queue", async () => { const { clock } = fakeClock(); - const l = new XLimiter(clock, { gap: 0, hour: 30, day: 150 }); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); await assert.rejects(l.run(signal, async () => { throw new Error("boom"); })); assert.equal(await l.run(signal, ok), "ok"); }); + +test("the gap is drawn afresh each time, within gap ± jitter", async () => { + const { clock, waits, setDraw } = fakeClock(); + const l = new XLimiter(clock, { gap: 30_000, jitter: 25_000, hour: 30, day: 150 }); + await l.run(signal, ok); + setDraw(0); // the shortest: 30 - 25 s + await l.run(signal, ok); + setDraw(0.999999); // close to the longest: 30 + 25 s + await l.run(signal, ok); + assert.equal(waits[0], 5_000); + assert.ok(waits[1]! > 54_999 && waits[1]! <= 55_000); +}); + +test("the defaults are 30 s ± 25 s", async () => { + const { GAP_MS, JITTER_MS } = await import("../src/x-limits.ts"); + assert.equal(GAP_MS, 30_000); + assert.equal(JITTER_MS, 25_000); +}); |
