forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

create-ack-waiter.test.ts131 lines, 5.0 KiBHistoryRawDownload
1import { describe, expect, it, vi } from "vitest";
2import { createAckWaiter } from "./create-ack-waiter";
3
4describe("createAckWaiter", () => {
5 it("resolves ok only when the ack for that client_seq arrives", async () => {
6 const w = createAckWaiter(1000);
7 const p = w.wait(1, "q1");
8 w.ack(2); // another send's ack must not confirm this one
9 expect(w.size).toBe(1);
10 w.ack(1);
11 await expect(p).resolves.toBe("ok");
12 expect(w.size).toBe(0);
13 });
14
15 it("rejects when no ack ever comes back", async () => {
16 vi.useFakeTimers();
17 const w = createAckWaiter(8000);
18 const p = w.wait(1, "q1");
19 const assertion = expect(p).rejects.toThrow("ack timeout");
20 await vi.advanceTimersByTimeAsync(8000);
21 await assertion;
22 expect(w.size).toBe(0);
23 vi.useRealTimers();
24 });
25
26 it("rejects every pending wait on failAll (unattributable frame / socket drop)", async () => {
27 const w = createAckWaiter(1000);
28 const a = w.wait(1, "q1");
29 const b = w.wait(2, "q2");
30 w.failAll(new Error("disconnected"));
31 await expect(a).rejects.toThrow("disconnected");
32 await expect(b).rejects.toThrow("disconnected");
33 expect(w.size).toBe(0);
34 });
35
36 it("fails only the named send when the error frame echoes its client_seq", async () => {
37 const w = createAckWaiter(1000);
38 const a = w.wait(1, "q1");
39 const b = w.wait(2, "q2");
40 w.fail(1, new Error("a choice question takes selected"));
41 await expect(a).rejects.toThrow("a choice question takes selected");
42 expect(w.size).toBe(1); // q2 is still waiting for its own answer
43 w.ack(2);
44 await expect(b).resolves.toBe("ok");
45 });
46
47 it("ignores a fail for a client_seq with nothing in flight", () => {
48 const w = createAckWaiter(1000);
49 void w.wait(1, "q1").catch(() => {});
50 w.fail(2, new Error("boom")); // late/unknown blame must not touch send #1
51 expect(w.size).toBe(1);
52 w.failAll(new Error("cleanup"));
53 });
54
55 it("marks an overtaken save superseded and lets its own ack settle nobody", async () => {
56 const w = createAckWaiter(1000);
57 const first = w.wait(1, "q1");
58 const second = w.wait(2, "q1");
59 await expect(first).resolves.toBe("superseded");
60 expect(w.size).toBe(1);
61 w.ack(1); // the first send's ack — it owns nobody now
62 expect(w.size).toBe(1);
63 w.ack(2);
64 await expect(second).resolves.toBe("ok");
65 expect(w.size).toBe(0);
66 });
67
68 // Regression: a `saved` frame for a send that already timed out must never
69 // confirm the retry that replaced it — that told students "Saved" for an
70 // answer the server had refused. With exact correlation the late frame simply
71 // names a send nobody waits on, and it no longer poisons the retry either.
72 it("does not let a timed-out send's late ack confirm the retry", async () => {
73 vi.useFakeTimers();
74 const w = createAckWaiter(8000);
75 const first = w.wait(1, "q1"); // student saves "A"
76 const firstAssertion = expect(first).rejects.toThrow("ack timeout");
77 await vi.advanceTimersByTimeAsync(8000);
78 await firstAssertion;
79
80 const retry = w.wait(2, "q1"); // student presses save again with "B"
81 let outcome: string | undefined;
82 void retry.then(
83 (r) => { outcome = `resolved:${r}`; },
84 (e: Error) => { outcome = `rejected:${e.message}`; },
85 );
86
87 w.ack(1); // the FIRST send's late `saved` frame finally lands
88 await vi.advanceTimersByTimeAsync(0);
89 expect(outcome).toBeUndefined(); // must NOT claim the retry was saved
90
91 w.ack(2); // the retry's own frame — no debt swallowed it
92 await vi.advanceTimersByTimeAsync(0);
93 expect(outcome).toBe("resolved:ok");
94 vi.useRealTimers();
95 });
96
97 // The unattributable-error path keeps the socket open, so the frames owed to
98 // the sends it rejected are still on their way. They name their own client_seq
99 // and settle nobody; the retry is confirmed by its own frame, not by theirs.
100 it("does not let an in-flight ack confirm the retry after an unattributable error frame", async () => {
101 const w = createAckWaiter(1000);
102 const first = w.wait(1, "q1");
103 const firstAssertion = expect(first).rejects.toThrow("state unavailable");
104 w.failAll(new Error("state unavailable")); // socket stays open
105 await firstAssertion;
106
107 const retry = w.wait(2, "q1");
108 let outcome: string | undefined;
109 void retry.then(
110 (r) => { outcome = `resolved:${r}`; },
111 (e: Error) => { outcome = `rejected:${e.message}`; },
112 );
113 w.ack(1); // the FIRST send's `saved` frame, still in flight
114 await Promise.resolve();
115 expect(outcome).toBeUndefined(); // must NOT claim the retry was saved
116
117 w.ack(2);
118 await expect(retry).resolves.toBe("ok");
119 });
120
121 it("does not confirm after a timeout has already fired", async () => {
122 vi.useFakeTimers();
123 const w = createAckWaiter(100);
124 const p = w.wait(1, "q1");
125 const assertion = expect(p).rejects.toThrow("ack timeout");
126 await vi.advanceTimersByTimeAsync(100);
127 w.ack(1); // late ack: nothing to settle, must not throw
128 await assertion;
129 vi.useRealTimers();
130 });
131});