|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import { setTimeout } from "node:timers/promises"; |
| 3 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 4 | +import { createS2Container, type StartedS2Container } from "@internal/testcontainers/webapp"; |
| 5 | +import { S2RealtimeStreams } from "~/services/realtime/s2realtimeStreams.server"; |
| 6 | +import { S2FaultProxy } from "./helpers/s2FaultProxy"; |
| 7 | + |
| 8 | +describe("S2 subscription handshake", () => { |
| 9 | + let s2: StartedS2Container; |
| 10 | + let streams: S2RealtimeStreams; |
| 11 | + const proxy = new S2FaultProxy(); |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + s2 = await createS2Container(); |
| 15 | + const endpoint = await proxy.start(); |
| 16 | + proxy.setTarget(s2.endpoint); |
| 17 | + streams = new S2RealtimeStreams({ |
| 18 | + endpoint, |
| 19 | + basin: s2.basin, |
| 20 | + accessToken: "unused", |
| 21 | + s2WaitSeconds: 0, |
| 22 | + }); |
| 23 | + }, 60_000); |
| 24 | + |
| 25 | + afterAll(async () => { |
| 26 | + await proxy.stop(); |
| 27 | + await s2?.container.stop(); |
| 28 | + }); |
| 29 | + |
| 30 | + function subscribe(session: string, signal = AbortSignal.timeout(5_000), lastEventId?: string) { |
| 31 | + return streams.streamResponseFromSessionStream( |
| 32 | + new Request("http://localhost/session"), |
| 33 | + session, |
| 34 | + "out", |
| 35 | + signal, |
| 36 | + { lastEventId } |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + it("retries creation conflicts without losing the requested resume position", async () => { |
| 41 | + const session = randomUUID(); |
| 42 | + await streams.appendPartToSessionStream("first", "p0", session, "out"); |
| 43 | + await streams.appendPartToSessionStream("second", "p1", session, "out"); |
| 44 | + proxy.rejectNextRead(session, 409, 2); |
| 45 | + |
| 46 | + const response = await subscribe(session, undefined, "0"); |
| 47 | + const body = await response.text(); |
| 48 | + expect(response.status).toBe(200); |
| 49 | + expect(response.headers.get("X-Stream-Version")).toBe("v2"); |
| 50 | + expect(body).toContain("second"); |
| 51 | + expect(body).not.toContain("first"); |
| 52 | + expect(proxy.injected.filter((path) => path.includes(session))).toHaveLength(2); |
| 53 | + }); |
| 54 | + |
| 55 | + it("fails after four attempts if the conflict persists", async () => { |
| 56 | + const session = randomUUID(); |
| 57 | + proxy.rejectNextRead(session, 409, 10); |
| 58 | + await expect(subscribe(session)).rejects.toThrow("S2 stream failed: 409"); |
| 59 | + expect(proxy.injected.filter((path) => path.includes(session))).toHaveLength(4); |
| 60 | + }); |
| 61 | + |
| 62 | + it("retries non-blocking read conflicts without losing the requested resume position", async () => { |
| 63 | + const session = randomUUID(); |
| 64 | + await streams.appendPartToSessionStream("first", "p0", session, "out"); |
| 65 | + await streams.appendPartToSessionStream("second", "p1", session, "out"); |
| 66 | + proxy.rejectNextRead(session, 409, 2); |
| 67 | + |
| 68 | + const records = await streams.readSessionStreamRecords(session, "out", 0); |
| 69 | + |
| 70 | + expect(records.map(({ data, id, seqNum }) => ({ data, id, seqNum }))).toEqual([ |
| 71 | + { data: "second", id: "p1", seqNum: 1 }, |
| 72 | + ]); |
| 73 | + expect(proxy.injected.filter((path) => path.includes(session))).toHaveLength(2); |
| 74 | + }); |
| 75 | + |
| 76 | + it.each([ |
| 77 | + { status: 409, attempts: 3 }, |
| 78 | + { status: 403, attempts: 1 }, |
| 79 | + ])( |
| 80 | + "bounds non-blocking reads to $attempts attempts for HTTP $status", |
| 81 | + async ({ status, attempts }) => { |
| 82 | + const run = randomUUID(); |
| 83 | + proxy.rejectNextRead(run, status, 10); |
| 84 | + |
| 85 | + await expect(streams.readRecords(run, "input")).rejects.toThrow( |
| 86 | + `S2 readRecords failed: ${status}` |
| 87 | + ); |
| 88 | + expect(proxy.injected.filter((path) => path.includes(run))).toHaveLength(attempts); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + it("opens concurrent subscriptions to previously unwritten streams", async () => { |
| 93 | + for (let round = 0; round < 8; round++) { |
| 94 | + const session = randomUUID(); |
| 95 | + const statuses = await Promise.all( |
| 96 | + Array.from({ length: 8 }, async () => { |
| 97 | + const response = await subscribe(session); |
| 98 | + await response.text(); |
| 99 | + return response.status; |
| 100 | + }) |
| 101 | + ); |
| 102 | + expect(statuses).toEqual(Array(8).fill(200)); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + it("does not retry other HTTP errors", async () => { |
| 107 | + const session = randomUUID(); |
| 108 | + proxy.rejectNextRead(session, 403, 10); |
| 109 | + await expect(subscribe(session)).rejects.toThrow("S2 stream failed: 403"); |
| 110 | + expect(proxy.injected.filter((path) => path.includes(session))).toHaveLength(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it("stops retrying when the subscriber disconnects during backoff", async () => { |
| 114 | + const session = randomUUID(); |
| 115 | + const abort = new AbortController(); |
| 116 | + proxy.rejectNextRead(session, 409, 10); |
| 117 | + const result = subscribe(session, abort.signal).catch((error: unknown) => error); |
| 118 | + await expect.poll(() => proxy.injected.some((path) => path.includes(session))).toBe(true); |
| 119 | + abort.abort(); |
| 120 | + expect(await result).toMatchObject({ name: "AbortError" }); |
| 121 | + const attempts = proxy.injected.filter((path) => path.includes(session)).length; |
| 122 | + // Prove no new request is sent after cancellation, across the first retry delay. |
| 123 | + await setTimeout(150); |
| 124 | + expect(proxy.injected.filter((path) => path.includes(session))).toHaveLength(attempts); |
| 125 | + }); |
| 126 | +}); |
0 commit comments