|
| 1 | +import http2 from "node:http2"; |
| 2 | +import net from "node:net"; |
| 3 | +import type { AddressInfo } from "node:net"; |
1 | 4 | import { describe, expect, it } from "vitest"; |
2 | 5 |
|
3 | 6 | import { ChatChunkTooLargeError, isChatChunkTooLargeError } from "../errors.js"; |
4 | | -import { encodeChunkOrError } from "./streamsWriterV2.js"; |
| 7 | +import { StreamsWriterV2, encodeChunkOrError } from "./streamsWriterV2.js"; |
5 | 8 |
|
6 | 9 | // The size cap and discriminant extraction are the only S2-independent bits |
7 | 10 | // of `StreamsWriterV2` that benefit from unit coverage. Both live in the |
@@ -75,3 +78,113 @@ describe("isChatChunkTooLargeError", () => { |
75 | 78 | expect(isChatChunkTooLargeError(undefined)).toBe(false); |
76 | 79 | }); |
77 | 80 | }); |
| 81 | + |
| 82 | +// A session `.out` stream that S2 reports as not found (the head-start drain |
| 83 | +// can append before the stream is visible). The writer is detached — nothing |
| 84 | +// awaits it until the handover flush — so a rejecting append used to escape |
| 85 | +// as an unhandled rejection and take the whole process down. |
| 86 | +describe("StreamsWriterV2 against a stream S2 does not have", () => { |
| 87 | + it("keeps the append failure on `wait()` instead of emitting an unhandled rejection", async () => { |
| 88 | + const server = http2.createServer((_req, res) => { |
| 89 | + res.writeHead(404, { "content-type": "application/json" }); |
| 90 | + res.end( |
| 91 | + JSON.stringify({ message: "stream sessions/chat_x/out not found", code: "not_found" }) |
| 92 | + ); |
| 93 | + }); |
| 94 | + const sessions = new Set<http2.ServerHttp2Session>(); |
| 95 | + server.on("session", (session) => { |
| 96 | + sessions.add(session); |
| 97 | + session.on("close", () => sessions.delete(session)); |
| 98 | + }); |
| 99 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", () => resolve())); |
| 100 | + const { port } = server.address() as AddressInfo; |
| 101 | + |
| 102 | + const rejections: unknown[] = []; |
| 103 | + const onUnhandledRejection = (reason: unknown) => rejections.push(reason); |
| 104 | + process.on("unhandledRejection", onUnhandledRejection); |
| 105 | + |
| 106 | + try { |
| 107 | + const writer = new StreamsWriterV2<string>({ |
| 108 | + basin: "trigger-local", |
| 109 | + stream: "sessions/chat_x/out", |
| 110 | + accessToken: "token", |
| 111 | + endpoint: `http://127.0.0.1:${port}/v1`, |
| 112 | + source: new ReadableStream<string>({ |
| 113 | + start(controller) { |
| 114 | + controller.enqueue("hello"); |
| 115 | + controller.close(); |
| 116 | + }, |
| 117 | + }), |
| 118 | + flushIntervalMs: 5, |
| 119 | + }); |
| 120 | + |
| 121 | + // Long enough for the append to fail and for Node to flag an unhandled |
| 122 | + // rejection, without anything having awaited the writer yet. |
| 123 | + await new Promise((resolve) => setTimeout(resolve, 1_000)); |
| 124 | + |
| 125 | + expect(rejections).toEqual([]); |
| 126 | + await expect(writer.wait()).rejects.toThrow(/not found/); |
| 127 | + } finally { |
| 128 | + process.off("unhandledRejection", onUnhandledRejection); |
| 129 | + for (const session of sessions) session.destroy(); |
| 130 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 131 | + } |
| 132 | + }, 20_000); |
| 133 | + |
| 134 | + // Same escape route when the SDK's own append retries run out. Reached via |
| 135 | + // the retry loop's session-creation branch, not the ack-timeout branch. |
| 136 | + it("keeps a retry-exhausted append on `wait()`", async () => { |
| 137 | + // Holds the port and refuses every connection, so the retry budget is |
| 138 | + // spent on connection failures with no window for another listener. |
| 139 | + let connectionAttempts = 0; |
| 140 | + const server = net.createServer((socket) => { |
| 141 | + connectionAttempts++; |
| 142 | + socket.destroy(); |
| 143 | + }); |
| 144 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", () => resolve())); |
| 145 | + const { port } = server.address() as AddressInfo; |
| 146 | + |
| 147 | + const rejections: unknown[] = []; |
| 148 | + const onUnhandledRejection = (reason: unknown) => rejections.push(reason); |
| 149 | + process.on("unhandledRejection", onUnhandledRejection); |
| 150 | + |
| 151 | + try { |
| 152 | + const writer = new StreamsWriterV2<string>({ |
| 153 | + basin: "trigger-local", |
| 154 | + stream: "sessions/chat_x/out", |
| 155 | + accessToken: "token", |
| 156 | + endpoint: `http://127.0.0.1:${port}/v1`, |
| 157 | + source: new ReadableStream<string>({ |
| 158 | + start(controller) { |
| 159 | + controller.enqueue("hello"); |
| 160 | + controller.close(); |
| 161 | + }, |
| 162 | + }), |
| 163 | + flushIntervalMs: 5, |
| 164 | + }); |
| 165 | + |
| 166 | + // Side channel: wait for the budget (3 attempts) to be spent without |
| 167 | + // touching the writer, then let the abort propagate. |
| 168 | + const deadline = Date.now() + 5_000; |
| 169 | + while (connectionAttempts < 3 && Date.now() < deadline) { |
| 170 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 171 | + } |
| 172 | + expect(connectionAttempts).toBeGreaterThanOrEqual(3); |
| 173 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 174 | + |
| 175 | + expect(rejections).toEqual([]); |
| 176 | + // Rejects in a microtask if it already failed; the 0ms timer wins if the |
| 177 | + // writer is somehow still pending, so a slow runner fails instead of |
| 178 | + // passing vacuously. |
| 179 | + await expect( |
| 180 | + Promise.race([ |
| 181 | + writer.wait(), |
| 182 | + new Promise((_, reject) => setTimeout(() => reject(new Error("still pending")), 0)), |
| 183 | + ]) |
| 184 | + ).rejects.toThrow(/Max attempts \(3\) exhausted/); |
| 185 | + } finally { |
| 186 | + process.off("unhandledRejection", onUnhandledRejection); |
| 187 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 188 | + } |
| 189 | + }, 20_000); |
| 190 | +}); |
0 commit comments