|
| 1 | +// `listenOrExit` is what stops an HTTP transport from reporting a port it does |
| 2 | +// not hold (#4923: SSE printed "Server is running" on a port another process |
| 3 | +// had, and stayed up listening on nothing). These tests bind real sockets, |
| 4 | +// because the defect lived in how Express hands a real listen error to the |
| 5 | +// success callback; a mocked `listen` would not have reproduced it. |
| 6 | +// `process.exit` is replaced so the failure path can be observed without ending |
| 7 | +// the test run, and `console.error` is silenced because that path prints. |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 10 | +import { createServer, type Server } from "node:http"; |
| 11 | +import { once } from "node:events"; |
| 12 | +import express from "express"; |
| 13 | +import { listenOrExit } from "../transports/listen.js"; |
| 14 | + |
| 15 | +const LISTENING = "listening line"; |
| 16 | + |
| 17 | +describe("listenOrExit", () => { |
| 18 | + let servers: Server[]; |
| 19 | + let errorSpy: ReturnType<typeof vi.spyOn>; |
| 20 | + let exited: Promise<number | undefined>; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + servers = []; |
| 24 | + errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 25 | + exited = new Promise((resolve) => { |
| 26 | + vi.spyOn(process, "exit").mockImplementation((code) => { |
| 27 | + resolve(typeof code === "number" ? code : undefined); |
| 28 | + return undefined as never; |
| 29 | + }); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + vi.restoreAllMocks(); |
| 35 | + await Promise.all( |
| 36 | + servers |
| 37 | + .filter((server) => server.listening) |
| 38 | + .map( |
| 39 | + (server) => |
| 40 | + new Promise<void>((resolve) => server.close(() => resolve())), |
| 41 | + ), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + /** A server holding a port, and the port it holds. */ |
| 46 | + async function holdPort(): Promise<number> { |
| 47 | + const holder = createServer(); |
| 48 | + servers.push(holder); |
| 49 | + // No host, as the transports bind: a holder on 127.0.0.1 alone would not |
| 50 | + // conflict with their wildcard bind on every platform. |
| 51 | + holder.listen(0); |
| 52 | + await once(holder, "listening"); |
| 53 | + const address = holder.address(); |
| 54 | + if (address === null || typeof address === "string") { |
| 55 | + throw new Error("expected a TCP address"); |
| 56 | + } |
| 57 | + return address.port; |
| 58 | + } |
| 59 | + |
| 60 | + it("prints the listening line once the port is bound, and does not exit", async () => { |
| 61 | + const server = listenOrExit(express(), 0, LISTENING); |
| 62 | + servers.push(server); |
| 63 | + await once(server, "listening"); |
| 64 | + |
| 65 | + expect(server.listening).toBe(true); |
| 66 | + expect(errorSpy).toHaveBeenCalledWith(LISTENING); |
| 67 | + expect(process.exit).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("reports a port already in use and exits 1 without claiming to listen", async () => { |
| 71 | + const port = await holdPort(); |
| 72 | + const server = listenOrExit(express(), port, LISTENING); |
| 73 | + servers.push(server); |
| 74 | + |
| 75 | + expect(await exited).toBe(1); |
| 76 | + expect(server.listening).toBe(false); |
| 77 | + expect(errorSpy).toHaveBeenCalledWith( |
| 78 | + `Failed to start: Port ${port} is already in use. Set PORT to a free port or stop the conflicting process.`, |
| 79 | + ); |
| 80 | + expect(errorSpy).not.toHaveBeenCalledWith(LISTENING); |
| 81 | + }); |
| 82 | + |
| 83 | + it("reports any other server error and exits 1", async () => { |
| 84 | + const server = listenOrExit(express(), 0, LISTENING); |
| 85 | + servers.push(server); |
| 86 | + await once(server, "listening"); |
| 87 | + |
| 88 | + const failure = Object.assign(new Error("permission denied"), { |
| 89 | + code: "EACCES", |
| 90 | + }); |
| 91 | + server.emit("error", failure); |
| 92 | + |
| 93 | + expect(await exited).toBe(1); |
| 94 | + expect(errorSpy).toHaveBeenCalledWith( |
| 95 | + "HTTP server encountered an error while starting:", |
| 96 | + failure, |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
0 commit comments