Open History to see the last commit
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.each([WS_METHODS.serverGetUsageSummary, WS_METHODS.serverReportClientActivity])(
56 "ignores background request %s",
57 (method) => {
58 trackRpcRequestSent("1", method);
59 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
60
61 expect(getSlowRpcAckRequests()).toEqual([]);
62 },
63 );
64
65 it.each(Object.values(WS_METHODS).filter((method) => method.startsWith("pullRequests.")))(
66 "ignores pull request workspace request %s",
67 (method) => {
68 trackRpcRequestSent("1", method);
69 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
70
71 expect(getSlowRpcAckRequests()).toEqual([]);
72 },
73 );
74
75 it("keeps ignoring untracked methods when a display tag is supplied", () => {
76 trackRpcRequestSent(
77 "1",
78 WS_METHODS.serverGetUsageSummary,
79 `${WS_METHODS.serverGetUsageSummary} · env-1`,
80 );
81 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
82
83 expect(getSlowRpcAckRequests()).toEqual([]);
84 });
85
86 it.each([WS_METHODS.serverUpdateProvider, WS_METHODS.vcsRefreshStatus])(
87 "gives %s a longer threshold before warning",
88 (method) => {
89 trackRpcRequestSent("1", method, `${method} · env-1`);
90 vi.advanceTimersByTime(LONG_RUNNING_RPC_ACK_THRESHOLD_MS - 1);
91 expect(getSlowRpcAckRequests()).toEqual([]);
92
93 vi.advanceTimersByTime(1);
94 expect(getSlowRpcAckRequests()).toMatchObject([
95 {
96 requestId: "1",
97 tag: `${method} · env-1`,
98 thresholdMs: LONG_RUNNING_RPC_ACK_THRESHOLD_MS,
99 },
100 ]);
101 },
102 );
103
104 it("evicts the oldest pending requests once the tracker reaches capacity", () => {
105 for (let index = 0; index < MAX_TRACKED_RPC_ACK_REQUESTS + 1; index += 1) {
106 trackRpcRequestSent(String(index), "server.getConfig");
107 }
108
109 vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS);
110
111 const slowRequests = getSlowRpcAckRequests();
112 expect(slowRequests).toHaveLength(MAX_TRACKED_RPC_ACK_REQUESTS);
113 expect(slowRequests[0]?.requestId).toBe("1");
114 expect(slowRequests.at(-1)?.requestId).toBe(String(MAX_TRACKED_RPC_ACK_REQUESTS));
115 });
116});