1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Clock, XLimiter, XRateLimitError } from "../src/x-limits.ts";
/** A clock that only moves when told to, recording the waits asked of it. */
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), setDraw: (r: number) => (draw = r) };
};
const signal = new AbortController().signal;
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, jitter: 0, hour: 30, day: 150 });
await l.run(signal, ok);
advance(3_000);
await l.run(signal, ok);
assert.deepEqual(waits, [7_000]);
});
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, 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));
advance(3_600_001);
assert.equal(await l.run(signal, ok), "ok");
});
test("the daily cap holds across hours", async () => {
const { clock, advance } = fakeClock();
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);
}
await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /24 hours/.test(e.message));
});
test("one at a time: the second starts only after the first finishes", async () => {
const { clock } = fakeClock();
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 () => {
order.push("first start");
await new Promise<void>((r) => (release = r));
order.push("first end");
});
const second = l.run(signal, async () => {
order.push("second start");
});
await new Promise((r) => setTimeout(r, 10));
release();
await Promise.all([first, second]);
assert.deepEqual(order, ["first start", "first end", "second start"]);
});
test("a failed search does not block the queue", async () => {
const { clock } = fakeClock();
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);
});
|