|
| 1 | +import { EventEmitter } from "node:events"; |
| 2 | +import type { Server as HttpServer } from "node:http"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { GatewayLockError } from "../../infra/gateway-lock.js"; |
| 5 | +import { listenGatewayHttpServer } from "./http-listen.js"; |
| 6 | + |
| 7 | +const sleepMock = vi.hoisted(() => vi.fn(async (_ms: number) => {})); |
| 8 | + |
| 9 | +vi.mock("../../utils.js", () => ({ |
| 10 | + sleep: (ms: number) => sleepMock(ms), |
| 11 | +})); |
| 12 | + |
| 13 | +type ListenOutcome = { kind: "error"; code: string } | { kind: "listening" }; |
| 14 | + |
| 15 | +function createFakeHttpServer(outcomes: ListenOutcome[]) { |
| 16 | + class FakeHttpServer extends EventEmitter { |
| 17 | + public closeCalls = 0; |
| 18 | + private attempt = 0; |
| 19 | + |
| 20 | + listen(_port: number, _host: string) { |
| 21 | + const outcome = outcomes[this.attempt] ?? { kind: "listening" }; |
| 22 | + this.attempt += 1; |
| 23 | + setImmediate(() => { |
| 24 | + if (outcome.kind === "error") { |
| 25 | + const err = Object.assign(new Error(outcome.code), { code: outcome.code }); |
| 26 | + this.emit("error", err); |
| 27 | + } else { |
| 28 | + this.emit("listening"); |
| 29 | + } |
| 30 | + }); |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + close(cb?: () => void) { |
| 35 | + this.closeCalls += 1; |
| 36 | + setImmediate(() => cb?.()); |
| 37 | + return this; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return new FakeHttpServer(); |
| 42 | +} |
| 43 | + |
| 44 | +describe("listenGatewayHttpServer", () => { |
| 45 | + it("retries EADDRINUSE and closes server handle before retry", async () => { |
| 46 | + sleepMock.mockClear(); |
| 47 | + const fake = createFakeHttpServer([ |
| 48 | + { kind: "error", code: "EADDRINUSE" }, |
| 49 | + { kind: "listening" }, |
| 50 | + ]); |
| 51 | + |
| 52 | + await expect( |
| 53 | + listenGatewayHttpServer({ |
| 54 | + httpServer: fake as unknown as HttpServer, |
| 55 | + bindHost: "127.0.0.1", |
| 56 | + port: 18789, |
| 57 | + }), |
| 58 | + ).resolves.toBeUndefined(); |
| 59 | + |
| 60 | + expect(fake.closeCalls).toBe(1); |
| 61 | + expect(sleepMock).toHaveBeenCalledTimes(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it("throws GatewayLockError after EADDRINUSE retries are exhausted", async () => { |
| 65 | + sleepMock.mockClear(); |
| 66 | + const fake = createFakeHttpServer([ |
| 67 | + { kind: "error", code: "EADDRINUSE" }, |
| 68 | + { kind: "error", code: "EADDRINUSE" }, |
| 69 | + { kind: "error", code: "EADDRINUSE" }, |
| 70 | + { kind: "error", code: "EADDRINUSE" }, |
| 71 | + { kind: "error", code: "EADDRINUSE" }, |
| 72 | + { kind: "error", code: "EADDRINUSE" }, |
| 73 | + ]); |
| 74 | + |
| 75 | + await expect( |
| 76 | + listenGatewayHttpServer({ |
| 77 | + httpServer: fake as unknown as HttpServer, |
| 78 | + bindHost: "127.0.0.1", |
| 79 | + port: 18789, |
| 80 | + }), |
| 81 | + ).rejects.toBeInstanceOf(GatewayLockError); |
| 82 | + |
| 83 | + expect(fake.closeCalls).toBe(4); |
| 84 | + }); |
| 85 | + |
| 86 | + it("wraps non-EADDRINUSE errors as GatewayLockError", async () => { |
| 87 | + sleepMock.mockClear(); |
| 88 | + const fake = createFakeHttpServer([{ kind: "error", code: "EACCES" }]); |
| 89 | + |
| 90 | + await expect( |
| 91 | + listenGatewayHttpServer({ |
| 92 | + httpServer: fake as unknown as HttpServer, |
| 93 | + bindHost: "127.0.0.1", |
| 94 | + port: 18789, |
| 95 | + }), |
| 96 | + ).rejects.toBeInstanceOf(GatewayLockError); |
| 97 | + |
| 98 | + expect(fake.closeCalls).toBe(0); |
| 99 | + }); |
| 100 | +}); |
0 commit comments