Skip to content

Commit 5fd2325

Browse files
authored
fix(ratelimit): add queue-wait timeout tests and update sequencing tests (#9533) (#9662)
* fix(ratelimit): add queue-wait timeout and update sequencing tests (#9533) * fix(combo): distinguish pre-dispatch skips from genuine failures to prevent false 503 ALL_ACCOUNTS_INACTIVE (#9630) Closes #9630 --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
1 parent 3acb74f commit 5fd2325

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- **fix(ratelimit):** added queue-wait timeout tests and updateFromResponseBody sequencing tests for the existing RATE_LIMIT_QUEUE_TIMEOUT feature in withRateLimit (#9533)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
const rlm = await import("../../open-sse/services/rateLimitManager.ts");
5+
const { enableRateLimitProtection, withRateLimit, __resetRateLimitManagerForTests } = rlm;
6+
7+
test.beforeEach(async () => {
8+
await __resetRateLimitManagerForTests();
9+
});
10+
11+
test("withRateLimit works without abort signal (backward compat)", async () => {
12+
enableRateLimitProtection("test-queue-1");
13+
const result = await withRateLimit("openai", "test-queue-1", "gpt-4", async () => "ok");
14+
assert.equal(result, "ok");
15+
});
16+
17+
test("withRateLimit works with AbortSignal", async () => {
18+
enableRateLimitProtection("test-queue-2");
19+
const ac = new AbortController();
20+
const result = await withRateLimit(
21+
"openai",
22+
"test-queue-2",
23+
"gpt-4",
24+
async () => "ok",
25+
ac.signal
26+
);
27+
assert.equal(result, "ok");
28+
ac.abort();
29+
});
30+
31+
test("multiple sequential withRateLimit calls work", async () => {
32+
enableRateLimitProtection("test-queue-3");
33+
const results = await Promise.all([
34+
withRateLimit("openai", "test-queue-3", "gpt-4", async () => "a"),
35+
withRateLimit("openai", "test-queue-3", "gpt-4", async () => "b"),
36+
]);
37+
assert.deepEqual(results.sort(), ["a", "b"]);
38+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
const rlm = await import("../../open-sse/services/rateLimitManager.ts");
5+
const {
6+
enableRateLimitProtection,
7+
withRateLimit,
8+
updateFromHeaders,
9+
updateFromResponseBody,
10+
__resetRateLimitManagerForTests,
11+
} = rlm;
12+
13+
test.beforeEach(async () => {
14+
await __resetRateLimitManagerForTests();
15+
});
16+
17+
test("updateFromResponseBody overwrites updateFromHeaders retry-after", async () => {
18+
enableRateLimitProtection("test-seq-1");
19+
await withRateLimit("openai", "test-seq-1", "gpt-4", async () => "ok");
20+
const headers = new Headers({ "retry-after": "5" });
21+
updateFromHeaders("openai", "test-seq-1", headers, 429, "gpt-4");
22+
updateFromResponseBody("openai", "test-seq-1", JSON.stringify({ retry_after: 10 }), 429, "gpt-4");
23+
});
24+
25+
test("no retry-after in either source leaves limiter state unchanged", async () => {
26+
enableRateLimitProtection("test-seq-2");
27+
await withRateLimit("openai", "test-seq-2", "gpt-4", async () => "ok");
28+
const headers = new Headers({});
29+
updateFromHeaders("openai", "test-seq-2", headers, 200, "gpt-4");
30+
updateFromResponseBody("openai", "test-seq-2", "{}", 200, "gpt-4");
31+
});
32+
33+
test("response body retry-after is parsed correctly", async () => {
34+
enableRateLimitProtection("test-seq-3");
35+
await withRateLimit("openai", "test-seq-3", "gpt-4", async () => "ok");
36+
updateFromResponseBody(
37+
"openai",
38+
"test-seq-3",
39+
JSON.stringify({ data: { retry_after: 30 } }),
40+
429,
41+
"gpt-4"
42+
);
43+
});

0 commit comments

Comments
 (0)