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

resyncRequests.test.ts

151 lines4.3 KB

1import { ResyncRequests } from "@t3tools/client-runtime/connection";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 RESYNC_MIN_INTERVAL_MS,
6 type ResyncScheduler,
7 requestResync,
8 resetResyncThrottleForTesting,
9} from "./resyncRequests";
10​
11interface Harness {
12 readonly scheduler: ResyncScheduler;
13 /** Request a reconcile as if the clock read `nowMs`. */
14 readonly requestAt: (nowMs: number) => void;
15 /** Run every timer whose deadline has passed, advancing the clock to it. */
16 readonly runDueTimers: () => void;
17 readonly fired: () => number;
19​
20function harness(): { harness: Harness; dispose: () => void } {
21 resetResyncThrottleForTesting();
22 let fired = 0;
23 const unsubscribe = ResyncRequests.onResyncRequested(() => {
24 fired += 1;
25 });
26 let clock = 0;
27 const timers: Array<{ at: number; run: () => void }> = [];
28 const scheduler: ResyncScheduler = {
29 now: () => clock,
30 setTimer: (run, delayMs) => {
31 timers.push({ at: clock + delayMs, run });
32 },
33 };
34 return {
35 harness: {
36 scheduler,
37 requestAt: (nowMs) => {
38 clock = nowMs;
39 requestResync(scheduler);
40 },
41 runDueTimers: () => {
42 const due = timers.splice(0, timers.length);
43 for (const timer of due) {
44 clock = timer.at;
45 timer.run();
46 }
47 },
48 fired: () => fired,
49 },
50 dispose: () => {
51 unsubscribe();
52 resetResyncThrottleForTesting();
53 },
54 };
56​
57function withHarness(run: (h: Harness) => void): number {
58 const { harness: h, dispose } = harness();
59 try {
60 run(h);
61 return h.fired();
62 } finally {
63 dispose();
64 }
66​
67describe("requestResync", () => {
68 it("passes the first request straight through", () => {
69 expect(withHarness((h) => h.requestAt(0))).toBe(1);
70 });
71​
72 it("coalesces a burst into one immediate reconcile", () => {
73 // Settling many threads at once must not ask for the same snapshot fetch
74 // and resubscribe once per thread.
75 const fired = withHarness((h) => {
76 for (let i = 0; i < 20; i += 1) {
77 h.requestAt(i);
78 }
79 });
80 expect(fired).toBe(1);
81 });
82​
83 it("still reconciles for a request suppressed inside the window", () => {
84 // The whole point of a request is that someone observed the client
85 // disagreeing with the server. Dropping it would leave that standing.
86 const fired = withHarness((h) => {
87 h.requestAt(0);
88 h.requestAt(100);
89 expect(h.fired()).toBe(1);
90 h.runDueTimers();
91 });
92 expect(fired).toBe(2);
93 });
94​
95 it("collapses many suppressed requests into a single trailing reconcile", () => {
96 const fired = withHarness((h) => {
97 h.requestAt(0);
98 for (let i = 1; i < 20; i += 1) {
99 h.requestAt(i * 10);
100 }
101 h.runDueTimers();
102 });
103 expect(fired).toBe(2);
104 });
105​
106 it("fires immediately once the window has passed", () => {
107 const fired = withHarness((h) => {
108 h.requestAt(0);
109 h.requestAt(RESYNC_MIN_INTERVAL_MS);
110 });
111 expect(fired).toBe(2);
112 });
113​
114 it("does not fire a trailing reconcile that an immediate one already covered", () => {
115 // A backward clock jump fires immediately while a trailing reconcile is
116 // still scheduled. That pending timer is now obsolete: letting it run
117 // would produce two reconciles for one logical request.
118 const fired = withHarness((h) => {
119 h.requestAt(1_000_000);
120 h.requestAt(1_000_100);
121 h.requestAt(0);
122 expect(h.fired()).toBe(2);
123 h.runDueTimers();
124 });
125 expect(fired).toBe(2);
126 });
127​
128 it("still reconciles for a request that arrives after an immediate fire", () => {
129 // The obsolete-timer check must not swallow a request raised *after* the
130 // reconcile that superseded it.
131 const fired = withHarness((h) => {
132 h.requestAt(0);
133 h.requestAt(100);
134 h.requestAt(RESYNC_MIN_INTERVAL_MS);
135 expect(h.fired()).toBe(2);
136 h.requestAt(RESYNC_MIN_INTERVAL_MS + 100);
137 h.runDueTimers();
138 });
139 expect(fired).toBe(3);
140 });
141​
142 it("recovers from a backward clock jump instead of wedging shut", () => {
143 // An NTP correction or manual clock change must not suppress every
144 // reconcile until wall time catches back up to the old timestamp.
145 const fired = withHarness((h) => {
146 h.requestAt(1_000_000);
147 h.requestAt(0);
148 });
149 expect(fired).toBe(2);
150 });
151});