
























@@ -79,6 +79,18 @@ describe("gateway control-plane write rate limit", () => {
7979return respond;
8080}
818182+function respondCall(respond: ReturnType<typeof vi.fn>) {
83+const call = respond.mock.calls.at(0);
84+if (!call) {
85+throw new Error("Expected response call");
86+}
87+return call as [
88+boolean,
89+unknown,
90+{ code?: string; details?: unknown; retryAfterMs?: number; retryable?: boolean }?,
91+];
92+}
93+8294it("allows 3 control-plane writes and blocks the 4th in the same minute", async () => {
8395const handlerCalls = vi.fn();
8496const handler: GatewayRequestHandler = (opts) => {
@@ -95,9 +107,10 @@ describe("gateway control-plane write rate limit", () => {
95107const blocked = await runRequest({ method: "config.patch", context, client, handler });
9610897109expect(handlerCalls).toHaveBeenCalledTimes(3);
98-const error = blocked.mock.calls[0]?.[2] as { code?: string; retryable?: boolean } | undefined;
99-expect(blocked.mock.calls[0]?.[0]).toBe(false);
100-expect(blocked.mock.calls[0]?.[1]).toBeUndefined();
110+const blockedCall = respondCall(blocked);
111+const error = blockedCall[2];
112+expect(blockedCall[0]).toBe(false);
113+expect(blockedCall[1]).toBeUndefined();
101114expect(error?.code).toBe("UNAVAILABLE");
102115expect(error?.retryable).toBe(true);
103116expect(logWarn).toHaveBeenCalledTimes(1);
@@ -117,9 +130,10 @@ describe("gateway control-plane write rate limit", () => {
117130await runRequest({ method: "update.run", context, client, handler });
118131119132const blocked = await runRequest({ method: "update.run", context, client, handler });
120-expect(blocked.mock.calls[0]?.[0]).toBe(false);
121-expect(blocked.mock.calls[0]?.[1]).toBeUndefined();
122-expect(blocked.mock.calls[0]?.[2]?.code).toBe("UNAVAILABLE");
133+const blockedCall = respondCall(blocked);
134+expect(blockedCall[0]).toBe(false);
135+expect(blockedCall[1]).toBeUndefined();
136+expect(blockedCall[2]?.code).toBe("UNAVAILABLE");
123137124138vi.advanceTimersByTime(60_001);
125139@@ -145,9 +159,10 @@ describe("gateway control-plane write rate limit", () => {
145159const blocked = await runRequest({ method, context, client, handler });
146160147161expect(handlerCalls).not.toHaveBeenCalled();
148-const error = blocked.mock.calls[0]?.[2];
149-expect(blocked.mock.calls[0]?.[0]).toBe(false);
150-expect(blocked.mock.calls[0]?.[1]).toBeUndefined();
162+const blockedCall = respondCall(blocked);
163+const error = blockedCall[2];
164+expect(blockedCall[0]).toBe(false);
165+expect(blockedCall[1]).toBeUndefined();
151166expect(error?.code).toBe("UNAVAILABLE");
152167expect(error?.retryable).toBe(true);
153168expect(error?.retryAfterMs).toBe(500);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。