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

clock.test.ts

201 lines6.8 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3import {
4 deadlineStore,
5 observeServerTime,
6 recordServerClock,
7 secondTick,
8 toServerClock,
9} from "./clock.ts";
10​
11describe("deadlineStore", () => {
12 afterEach(() => {
13 vi.useRealTimers();
14 });
15​
16 it("flips once at the deadline and notifies every subscriber from one timer", () => {
17 vi.useFakeTimers({ now: 10_000 });
18 const store = deadlineStore(10_500);
19 const first = vi.fn();
20 const second = vi.fn();
21 const unsubscribeFirst = store.subscribe(first);
22 const unsubscribeSecond = deadlineStore(10_500).subscribe(second);
23 expect(vi.getTimerCount()).toBe(1);
24 expect(store.getSnapshot()).toBe(false);
25​
26 vi.advanceTimersByTime(499);
27 expect(store.getSnapshot()).toBe(false);
28 vi.advanceTimersByTime(1);
29 expect(store.getSnapshot()).toBe(true);
30 expect(first).toHaveBeenCalledTimes(1);
31 expect(second).toHaveBeenCalledTimes(1);
32 expect(vi.getTimerCount()).toBe(0);
33 unsubscribeFirst();
34 unsubscribeSecond();
35 });
36​
37 it("shares one timer between callers that obtain the deadline before subscribing", () => {
38 vi.useFakeTimers({ now: 0 });
39 const first = deadlineStore(2_000);
40 const second = deadlineStore(2_000);
41 const firstListener = vi.fn();
42 const secondListener = vi.fn();
43 const unsubscribeFirst = first.subscribe(firstListener);
44 const unsubscribeSecond = second.subscribe(secondListener);
45 expect(vi.getTimerCount()).toBe(1);
46​
47 // A click observing the passed deadline must update every subscriber.
48 vi.setSystemTime(5_000);
49 first.refresh();
50 expect(second.getSnapshot()).toBe(true);
51 expect(secondListener).toHaveBeenCalledTimes(1);
52 expect(firstListener).toHaveBeenCalledTimes(1);
53 unsubscribeFirst();
54 unsubscribeSecond();
55 });
56​
57 it("re-arms deadlines beyond the 32-bit timer range instead of firing early", () => {
58 vi.useFakeTimers({ now: 0 });
59 const deadline = 2_147_483_647 + 1_000;
60 const store = deadlineStore(deadline);
61 const listener = vi.fn();
62 const unsubscribe = store.subscribe(listener);
63 vi.advanceTimersByTime(2_147_483_647);
64 expect(store.getSnapshot()).toBe(false);
65 expect(listener).not.toHaveBeenCalled();
66 vi.advanceTimersByTime(1_000);
67 expect(store.getSnapshot()).toBe(true);
68 expect(listener).toHaveBeenCalledTimes(1);
69 unsubscribe();
70 });
71​
72 it("refresh catches up when the timer runs late", () => {
73 vi.useFakeTimers({ now: 0 });
74 const store = deadlineStore(2_000);
75 const listener = vi.fn();
76 const unsubscribe = store.subscribe(listener);
77 store.refresh();
78 expect(listener).not.toHaveBeenCalled();
79 // Wall clock jumps past the deadline without the timer firing (sleep, throttling).
80 vi.setSystemTime(5_000);
81 expect(store.getSnapshot()).toBe(false);
82 store.refresh();
83 expect(store.getSnapshot()).toBe(true);
84 expect(listener).toHaveBeenCalledTimes(1);
85 expect(vi.getTimerCount()).toBe(0);
86 unsubscribe();
87 });
88​
89 it("stops its timer when the last subscriber leaves", () => {
90 vi.useFakeTimers({ now: 0 });
91 const store = deadlineStore(1_000);
92 const unsubscribe = store.subscribe(vi.fn());
93 unsubscribe();
94 expect(vi.getTimerCount()).toBe(0);
95 vi.advanceTimersByTime(1_000);
96 expect(store.getSnapshot()).toBe(true);
97 });
98​
99 it("treats a missing or unparseable deadline as never passing", () => {
100 vi.useFakeTimers({ now: 0 });
101 expect(deadlineStore(null).getSnapshot()).toBe(false);
102 const store = deadlineStore(Number.NaN);
103 store.subscribe(vi.fn());
104 expect(vi.getTimerCount()).toBe(0);
105 expect(store.getSnapshot()).toBe(false);
106 });
107});
108​
109describe("secondTick", () => {
110 afterEach(() => {
111 vi.useRealTimers();
112 });
113​
114 it("advances on wall-clock second boundaries", () => {
115 vi.useFakeTimers({ now: 5_250 });
116 const listener = vi.fn();
117 const unsubscribe = secondTick.subscribe(listener);
118 expect(secondTick.getSnapshot()).toBe(5_000);
119 vi.advanceTimersByTime(749);
120 expect(listener).not.toHaveBeenCalled();
121 vi.advanceTimersByTime(1);
122 expect(secondTick.getSnapshot()).toBe(6_000);
123 expect(listener).toHaveBeenCalledTimes(1);
124 unsubscribe();
125 expect(vi.getTimerCount()).toBe(0);
126 });
127​
128 it("stops ticking when the last subscriber leaves during a tick", () => {
129 vi.useFakeTimers({ now: 0 });
130 const unsubscribe = secondTick.subscribe(() => unsubscribe());
131 vi.advanceTimersByTime(1_000);
132 expect(vi.getTimerCount()).toBe(0);
133 });
134​
135 it("keeps one timer when a subscriber replaces itself during a tick", () => {
136 vi.useFakeTimers({ now: 0 });
137 let unsubscribeNext = () => {};
138 const unsubscribe = secondTick.subscribe(() => {
139 unsubscribe();
140 unsubscribeNext = secondTick.subscribe(() => {});
141 });
142 vi.advanceTimersByTime(1_000);
143 expect(vi.getTimerCount()).toBe(1);
144 unsubscribeNext();
145 expect(vi.getTimerCount()).toBe(0);
146 });
147});
148​
149describe("server clock", () => {
150 const HOUR = 3_600_000;
151​
152 it("counts from a host clock running ahead, minus only the reply's latency", () => {
153 const environmentId = EnvironmentId.make("ahead");
154 recordServerClock(environmentId, {
155 serverTimeMs: HOUR + 1_000,
156 requestedAtMs: 900,
157 receivedAtMs: 1_050,
158 });
159 expect(toServerClock(environmentId, 2_050)).toBe(HOUR + 2_000);
160 });
161​
162 it("counts from a host clock running behind, minus only the request's latency", () => {
163 const environmentId = EnvironmentId.make("behind");
164 recordServerClock(environmentId, {
165 serverTimeMs: 1_000,
166 requestedAtMs: HOUR + 900,
167 receivedAtMs: HOUR + 1_050,
168 });
169 expect(toServerClock(environmentId, HOUR + 1_900)).toBe(2_000);
170 });
171​
172 it("leaves clocks that agree within the round trip unshifted", () => {
173 const environmentId = EnvironmentId.make("synced");
174 recordServerClock(environmentId, {
175 serverTimeMs: 5_000,
176 requestedAtMs: 1_000,
177 receivedAtMs: 9_000,
178 });
179 expect(toServerClock(environmentId, 12_345)).toBe(12_345);
180 });
181​
182 it("raises a stale negative offset when a host timestamp arrives ahead of it", () => {
183 const environmentId = EnvironmentId.make("caught-up");
184 recordServerClock(environmentId, {
185 serverTimeMs: 1_000,
186 requestedAtMs: 2 * HOUR + 900,
187 receivedAtMs: 2 * HOUR + 1_050,
188 });
189 expect(toServerClock(environmentId, 2 * HOUR + 10_050)).toBe(10_150);
190​
191 observeServerTime(environmentId, 2 * HOUR + 10_000, 2 * HOUR + 10_050);
192 expect(toServerClock(environmentId, 2 * HOUR + 20_050)).toBe(2 * HOUR + 20_000);
193​
194 observeServerTime(environmentId, 5_000, 2 * HOUR + 20_100);
195 expect(toServerClock(environmentId, 2 * HOUR + 20_050)).toBe(2 * HOUR + 20_000);
196​
197 const unrecorded = EnvironmentId.make("unrecorded");
198 observeServerTime(unrecorded, HOUR, 1_000);
199 expect(toServerClock(unrecorded, 1_000)).toBe(1_000);
200 });
201});