|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import type { Subprocess } from "bun"; |
| 3 | +import { createServer } from "node:net"; |
| 4 | + |
| 5 | +const repoRoot = process.cwd(); |
| 6 | +const spawned: Subprocess[] = []; |
| 7 | + |
| 8 | +async function reserveLoopbackPort() { |
| 9 | + const listener = createServer(() => undefined); |
| 10 | + await new Promise<void>((resolve, reject) => { |
| 11 | + listener.once("error", reject); |
| 12 | + listener.listen(0, "127.0.0.1", () => resolve()); |
| 13 | + }); |
| 14 | + |
| 15 | + const address = listener.address(); |
| 16 | + const port = typeof address === "object" && address ? address.port : 0; |
| 17 | + await new Promise<void>((resolve) => listener.close(() => resolve())); |
| 18 | + return port; |
| 19 | +} |
| 20 | + |
| 21 | +async function waitUntil<T>( |
| 22 | + label: string, |
| 23 | + fn: () => Promise<T | null> | T | null, |
| 24 | + timeoutMs = 1_500, |
| 25 | + intervalMs = 20, |
| 26 | +) { |
| 27 | + const deadline = Date.now() + timeoutMs; |
| 28 | + |
| 29 | + for (;;) { |
| 30 | + const value = await fn(); |
| 31 | + if (value !== null) { |
| 32 | + return value; |
| 33 | + } |
| 34 | + |
| 35 | + if (Date.now() >= deadline) { |
| 36 | + throw new Error(`Timed out waiting for ${label}.`); |
| 37 | + } |
| 38 | + |
| 39 | + await Bun.sleep(intervalMs); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async function readHealth(port: number) { |
| 44 | + try { |
| 45 | + const response = await fetch(`http://127.0.0.1:${port}/health`); |
| 46 | + if (!response.ok) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return (await response.json()) as { ok: boolean; pid: number }; |
| 51 | + } catch { |
| 52 | + return null; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +afterEach(async () => { |
| 57 | + await Promise.allSettled( |
| 58 | + spawned.splice(0).map(async (proc) => { |
| 59 | + try { |
| 60 | + proc.kill(); |
| 61 | + } catch { |
| 62 | + // Ignore processes that already exited. |
| 63 | + } |
| 64 | + |
| 65 | + await proc.exited.catch(() => undefined); |
| 66 | + }), |
| 67 | + ); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("mcp serve process lifecycle", () => { |
| 71 | + test("exits cleanly after SIGTERM instead of hot-looping after server shutdown", async () => { |
| 72 | + const port = await reserveLoopbackPort(); |
| 73 | + const proc = Bun.spawn(["bun", "run", "src/main.tsx", "mcp", "serve"], { |
| 74 | + cwd: repoRoot, |
| 75 | + stdin: "ignore", |
| 76 | + stdout: "pipe", |
| 77 | + stderr: "pipe", |
| 78 | + env: { |
| 79 | + ...process.env, |
| 80 | + HUNK_MCP_PORT: String(port), |
| 81 | + }, |
| 82 | + }); |
| 83 | + spawned.push(proc); |
| 84 | + |
| 85 | + const health = await waitUntil("daemon health", () => readHealth(port), 3_000, 50); |
| 86 | + expect(health).toMatchObject({ ok: true, pid: proc.pid }); |
| 87 | + |
| 88 | + let exited = false; |
| 89 | + void proc.exited.then(() => { |
| 90 | + exited = true; |
| 91 | + }); |
| 92 | + |
| 93 | + process.kill(proc.pid, "SIGTERM"); |
| 94 | + |
| 95 | + await waitUntil("mcp serve process exit", () => (exited ? true : null), 1_500, 25); |
| 96 | + await waitUntil("daemon port close", async () => |
| 97 | + (await readHealth(port)) === null ? true : null, |
| 98 | + ); |
| 99 | + }, 10_000); |
| 100 | +}); |
0 commit comments