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

PreviewFrameWindow.test.ts

162 lines5.8 KB

1import { expect, it } from "@effect/vitest";
2​
3import { makePreviewFrameWindow } from "./PreviewFrameWindow.ts";
4​
5const FRAME_INTERVAL_MS = 33;
6​
7interface Link {
8 readonly latencyMs: number;
9 readonly transferMs: number;
10 /** Every nth ack arrives this much later. */
11 readonly lateAck?: { readonly every: number; readonly ms: number };
13​
14// Offers a frame every 33 ms to a link that takes `transferMs` to carry each one and
15// `latencyMs` to return its ack, the way the stream route does: send when the window allows,
16// with an empty probe ahead on an idle link, otherwise keep only the newest frame.
17const makeLink = (link: Link) => {
18 const window = makePreviewFrameWindow();
19 const acks: Array<{ readonly at: number; readonly image: boolean }> = [];
20 let linkFreeAt = 0;
21 let waiting = false;
22 let now = 0;
23 let sent = 0;
24 let acked = 0;
25 const write = (probe: boolean) => {
26 window.sent(now, probe);
27 linkFreeAt = Math.max(linkFreeAt, now) + (probe ? 0 : link.transferMs);
28 acked += 1;
29 const late = link.lateAck && acked % link.lateAck.every === 0 ? link.lateAck.ms : 0;
30 acks.push({
31 at: Math.max(acks.at(-1)?.at ?? 0, linkFreeAt + link.latencyMs + late),
32 image: !probe,
33 });
34 };
35 const send = () => {
36 sent += 1;
37 if (window.idle()) write(true);
38 write(false);
39 };
40 const deliverAcks = () => {
41 while (acks.length > 0 && acks[0]!.at <= now) {
42 acks.shift();
43 window.acked(now);
44 }
45 };
46 return {
47 // `replyBacklog` is the most images a reply written at any moment would have waited
48 // behind; one frame at a time, as before pipelining, makes it 1.
49 stream: (ms: number) => {
50 const until = now + ms;
51 const sentBefore = sent;
52 let mostInFlight = 0;
53 let replyBacklog = 0;
54 for (; now < until; now += 1) {
55 deliverAcks();
56 if (now % FRAME_INTERVAL_MS === 0) waiting = true;
57 if (waiting && window.canSend(now)) {
58 waiting = false;
59 send();
60 }
61 mostInFlight = Math.max(mostInFlight, acks.filter((ack) => ack.image).length);
62 const serializedAt = linkFreeAt;
63 replyBacklog = Math.max(
64 replyBacklog,
65 serializedAt > now ? Math.ceil((serializedAt - now) / link.transferMs) : 0,
66 );
67 }
68 return { sent: sent - sentBefore, mostInFlight, replyBacklog };
69 },
70 idle: (ms: number) => {
71 waiting = false;
72 const until = now + ms;
73 for (; now < until || now % FRAME_INTERVAL_MS !== 0; now += 1) deliverAcks();
74 },
75 change: (next: Link) => {
76 link = next;
77 },
78 };
79};
80​
81it("a fast link with a long round trip carries every frame instead of one per round trip", () => {
82 const link = makeLink({ latencyMs: 200, transferMs: 2 });
83 link.stream(1_000);
84 // One frame per 202 ms round trip would be 50 frames; the page offers 303.
85 expect(link.stream(10_000).sent).toBeGreaterThan(290);
86});
87​
88it("a slow link carries one frame per round trip, as it did before", () => {
89 const link = makeLink({ latencyMs: 10, transferMs: 320 });
90 const streamed = link.stream(180_000);
91 expect(streamed.mostInFlight).toBe(1);
92 expect(streamed.replyBacklog).toBe(1);
93 // One every 330 ms round trip from the first millisecond.
94 expect(streamed.sent).toBe(546);
95 link.idle(10_000);
96 expect(link.stream(60_000).mostInFlight).toBe(1);
97});
98​
99it("a link too slow for every frame gets one at a time, however long its round trip", () => {
100 const link = makeLink({ latencyMs: 80, transferMs: 40 });
101 expect(link.stream(60_000).mostInFlight).toBe(1);
102});
103​
104it("a link that gets slower goes back to one frame at a time once its acks say so", () => {
105 const link = makeLink({ latencyMs: 80, transferMs: 2 });
106 expect(link.stream(3_000).mostInFlight).toBeGreaterThan(2);
107 link.change({ latencyMs: 80, transferMs: 320 });
108 // Frames already sent queue; their late acks end it.
109 link.stream(3_000);
110 expect(link.stream(120_000).mostInFlight).toBe(1);
111});
112​
113it("a stalled fast link drains and stays at one frame in flight on a slow link", () => {
114 const link = makeLink({ latencyMs: 80, transferMs: 2 });
115 expect(link.stream(3_000).mostInFlight).toBeGreaterThan(2);
116 link.change({ latencyMs: 2_300, transferMs: 500 });
117 link.stream(8_000);
118 const streamed = link.stream(20_000);
119 expect(streamed.mostInFlight).toBe(1);
120 expect(streamed.sent).toBeGreaterThan(0);
121});
122​
123it("a link that gets faster carries every frame again", () => {
124 const link = makeLink({ latencyMs: 80, transferMs: 320 });
125 link.stream(10_000);
126 link.change({ latencyMs: 80, transferMs: 2 });
127 link.stream(3_000);
128 expect(link.stream(10_000).sent).toBeGreaterThan(290);
129});
130​
131it("an idle link is measured again before it carries more than one image", () => {
132 const link = makeLink({ latencyMs: 80, transferMs: 2 });
133 expect(link.stream(3_000).mostInFlight).toBeGreaterThan(2);
134 link.idle(10_000);
135 link.change({ latencyMs: 10, transferMs: 320 });
136 // Six frames are offered before the first ack returns after 330 ms.
137 expect(link.stream(6 * FRAME_INTERVAL_MS).mostInFlight).toBe(1);
138 expect(link.stream(60_000).mostInFlight).toBe(1);
139});
140​
141it("late acks cost a jittery link a few frames, not its window", () => {
142 const link = makeLink({ latencyMs: 80, transferMs: 2, lateAck: { every: 30, ms: 60 } });
143 let sent = 0;
144 for (let cycle = 0; cycle < 12; cycle += 1) {
145 sent += link.stream(5_000).sent;
146 link.idle(5_000);
147 }
148 // 12 bursts of 152 frames are offered; one frame per round trip would be 61 each.
149 expect(sent).toBeGreaterThan(12 * 152 * 0.9);
150});
151​
152it("an empty message acked only with its image does not make a slow link fast", () => {
153 const window = makePreviewFrameWindow();
154 // A busy viewer answers both at 330 ms, so the frame seems to take no time beyond latency.
155 window.sent(0, true);
156 window.sent(0, false);
157 window.acked(330);
158 window.acked(330);
159 window.sent(330, true);
160 window.sent(330, false);
161 expect(window.canSend(363)).toBe(false);
162});