Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

requestLatencyState.test.ts

117 lines3.7 KB

1import { WS_METHODS } from "@t3tools/contracts";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 acknowledgeRpcRequest,
6 getSlowRpcAckRequests,
7 resetRequestLatencyStateForTests,
8 trackRpcRequestSent,
9 LONG_RUNNING_RPC_ACK_THRESHOLD_MS,
10 SLOW_RPC_ACK_THRESHOLD_MS,
11 MAX_TRACKED_RPC_ACK_REQUESTS,
12} from "./requestLatencyState";
13​
14describe("requestLatencyState", () => {
15 beforeEach(() => {
16 vi.useFakeTimers();
17 resetRequestLatencyStateForTests();
18 });
19​
20 afterEach(() => {
21 vi.useRealTimers();
22 });
23​
24 it("marks unary requests as slow when the ack threshold is exceeded", () => {
25 trackRpcRequestSent("1", "server.getConfig");
26 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS - 1);
27 expect(getSlowRpcAckRequests()).toEqual([]);
28​
29 vi.advanceTimersByTime(1);
30 expect(getSlowRpcAckRequests()).toMatchObject([
31 {
32 requestId: "1",
33 tag: "server.getConfig",
34 thresholdMs: SLOW_RPC_ACK_THRESHOLD_MS,
35 },
36 ]);
37 });
38​
39 it("clears the slow request once the server acknowledges it", () => {
40 trackRpcRequestSent("1", "git.status");
41 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS);
42 expect(getSlowRpcAckRequests()).toHaveLength(1);
43​
44 acknowledgeRpcRequest("1");
45 expect(getSlowRpcAckRequests()).toEqual([]);
46 });
47​
48 it("ignores long-lived subscribe requests", () => {
49 trackRpcRequestSent("1", "subscribeServerConfig");
50 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
51​
52 expect(getSlowRpcAckRequests()).toEqual([]);
53 });
54​
55 it("ignores the long-lived preview automation connection", () => {
56 trackRpcRequestSent("1", WS_METHODS.previewAutomationConnect);
57 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
58​
59 expect(getSlowRpcAckRequests()).toEqual([]);
60 });
61​
62 it("ignores usage summary requests", () => {
63 trackRpcRequestSent("1", WS_METHODS.serverGetUsageSummary);
64 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
65​
66 expect(getSlowRpcAckRequests()).toEqual([]);
67 });
68​
69 it.each(Object.values(WS_METHODS).filter((method) => method.startsWith("pullRequests.")))(
70 "ignores pull request workspace request %s",
71 (method) => {
72 trackRpcRequestSent("1", method);
73 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
74​
75 expect(getSlowRpcAckRequests()).toEqual([]);
76 },
77 );
78​
79 it("keeps ignoring untracked methods when a display tag is supplied", () => {
80 trackRpcRequestSent(
81 "1",
82 WS_METHODS.previewAutomationConnect,
83 `${WS_METHODS.previewAutomationConnect} · env-1`,
84 );
85 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
86​
87 expect(getSlowRpcAckRequests()).toEqual([]);
88 });
89​
90 it("gives provider updates a longer threshold before warning", () => {
91 trackRpcRequestSent("1", WS_METHODS.serverUpdateProvider, "server.updateProvider · env-1");
92 vi.advanceTimersByTime(LONG_RUNNING_RPC_ACK_THRESHOLD_MS - 1);
93 expect(getSlowRpcAckRequests()).toEqual([]);
94​
95 vi.advanceTimersByTime(1);
96 expect(getSlowRpcAckRequests()).toMatchObject([
97 {
98 requestId: "1",
99 tag: "server.updateProvider · env-1",
100 thresholdMs: LONG_RUNNING_RPC_ACK_THRESHOLD_MS,
101 },
102 ]);
103 });
104​
105 it("evicts the oldest pending requests once the tracker reaches capacity", () => {
106 for (let index = 0; index < MAX_TRACKED_RPC_ACK_REQUESTS + 1; index += 1) {
107 trackRpcRequestSent(String(index), "server.getConfig");
108 }
109​
110 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS);
111​
112 const slowRequests = getSlowRpcAckRequests();
113 expect(slowRequests).toHaveLength(MAX_TRACKED_RPC_ACK_REQUESTS);
114 expect(slowRequests[0]?.requestId).toBe("1");
115 expect(slowRequests.at(-1)?.requestId).toBe(String(MAX_TRACKED_RPC_ACK_REQUESTS));
116 });
117});