|
| 1 | +// Real-behavior proof (real sockets, real undici fetch, real timers): a live HTTP |
| 2 | +// endpoint that sends headers and then stalls or slow-drips its body must be bounded |
| 3 | +// by the request deadline, not only by the per-chunk idle guard. This exercises the |
| 4 | +// production containerRpcRequest -> containerRestRequest -> readSignalRestText path |
| 5 | +// without mocking fetch, unlike the fake-timer unit tests. |
| 6 | +import http from "node:http"; |
| 7 | +import type { AddressInfo } from "node:net"; |
| 8 | +import { afterEach, describe, expect, it } from "vitest"; |
| 9 | +import { containerRpcRequest } from "./client-container.js"; |
| 10 | + |
| 11 | +type StartedServer = { baseUrl: string; close: () => Promise<void> }; |
| 12 | + |
| 13 | +const running: StartedServer[] = []; |
| 14 | + |
| 15 | +afterEach(async () => { |
| 16 | + while (running.length > 0) { |
| 17 | + await running.pop()?.close(); |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +async function startServer(handler: http.RequestListener): Promise<StartedServer> { |
| 22 | + const server = http.createServer(handler); |
| 23 | + server.on("clientError", () => {}); |
| 24 | + await new Promise<void>((resolve) => { |
| 25 | + server.listen(0, "127.0.0.1", resolve); |
| 26 | + }); |
| 27 | + const { port } = server.address() as AddressInfo; |
| 28 | + const started: StartedServer = { |
| 29 | + baseUrl: `http://127.0.0.1:${port}`, |
| 30 | + close: () => |
| 31 | + new Promise<void>((resolve) => { |
| 32 | + server.closeAllConnections(); |
| 33 | + server.close(() => resolve()); |
| 34 | + }), |
| 35 | + }; |
| 36 | + running.push(started); |
| 37 | + return started; |
| 38 | +} |
| 39 | + |
| 40 | +describe("signal REST real-server deadline", () => { |
| 41 | + it("aborts a slow-drip body that never idles, at the request deadline", async () => { |
| 42 | + // Drip a byte every 50ms: below the 300ms idle guard, so only the total request |
| 43 | + // deadline can stop it. This is the exact slow-drip case the fix bounds. |
| 44 | + let dripCount = 0; |
| 45 | + const server = await startServer((_req, res) => { |
| 46 | + res.writeHead(200, { "content-type": "application/json" }); |
| 47 | + res.write("{"); |
| 48 | + const drip = setInterval(() => { |
| 49 | + try { |
| 50 | + dripCount += 1; |
| 51 | + res.write(" "); |
| 52 | + } catch { |
| 53 | + clearInterval(drip); |
| 54 | + } |
| 55 | + }, 50); |
| 56 | + res.on("close", () => clearInterval(drip)); |
| 57 | + }); |
| 58 | + |
| 59 | + const startedAt = Date.now(); |
| 60 | + await expect( |
| 61 | + containerRpcRequest("version", undefined, { baseUrl: server.baseUrl, timeoutMs: 300 }), |
| 62 | + ).rejects.toThrow(/Signal REST request timed out|stalled/); |
| 63 | + const elapsedMs = Date.now() - startedAt; |
| 64 | + |
| 65 | + // Multiple chunks arrived below the idle threshold, yet the absolute deadline |
| 66 | + // still bounded the call. Without it, this response would continue indefinitely. |
| 67 | + expect(dripCount).toBeGreaterThan(1); |
| 68 | + expect(elapsedMs).toBeLessThan(2_000); |
| 69 | + }); |
| 70 | + |
| 71 | + it("aborts a response whose body stalls immediately after headers", async () => { |
| 72 | + const server = await startServer((_req, res) => { |
| 73 | + res.writeHead(200, { "content-type": "application/json" }); |
| 74 | + res.write("{"); |
| 75 | + // Never ends the body. |
| 76 | + }); |
| 77 | + |
| 78 | + const startedAt = Date.now(); |
| 79 | + await expect( |
| 80 | + containerRpcRequest("version", undefined, { baseUrl: server.baseUrl, timeoutMs: 300 }), |
| 81 | + ).rejects.toThrow(/Signal REST (request timed out|response body stalled)/); |
| 82 | + expect(Date.now() - startedAt).toBeLessThan(2_000); |
| 83 | + }); |
| 84 | + |
| 85 | + it("returns the parsed body when it completes within the deadline", async () => { |
| 86 | + const server = await startServer((_req, res) => { |
| 87 | + res.writeHead(200, { "content-type": "application/json" }); |
| 88 | + res.end(JSON.stringify({ versions: ["v1"], build: 2 })); |
| 89 | + }); |
| 90 | + |
| 91 | + const result = await containerRpcRequest<{ versions?: string[]; build?: number }>( |
| 92 | + "version", |
| 93 | + undefined, |
| 94 | + { baseUrl: server.baseUrl, timeoutMs: 1_000 }, |
| 95 | + ); |
| 96 | + expect(result).toEqual({ versions: ["v1"], build: 2 }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments