|
| 1 | +import fs from "node:fs"; |
| 2 | +import net from "node:net"; |
| 3 | +import path from "node:path"; |
| 4 | +import test from "node:test"; |
| 5 | +import assert from "node:assert/strict"; |
| 6 | +import { once } from "node:events"; |
| 7 | +import { spawn } from "node:child_process"; |
| 8 | +import { fileURLToPath } from "node:url"; |
| 9 | + |
| 10 | +import { buildEnv, installFakeCodex } from "./fake-codex-fixture.mjs"; |
| 11 | +import { makeTempDir } from "./helpers.mjs"; |
| 12 | +import { sendBrokerShutdown, waitForBrokerEndpoint } from "../plugins/codex/scripts/lib/broker-lifecycle.mjs"; |
| 13 | + |
| 14 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 15 | +const BROKER_SCRIPT = path.join(ROOT, "plugins", "codex", "scripts", "app-server-broker.mjs"); |
| 16 | +const IDLE_TIMEOUT_ENV = "CODEX_COMPANION_BROKER_IDLE_TIMEOUT_MS"; |
| 17 | + |
| 18 | +function spawnTestBroker({ idleTimeoutMs }) { |
| 19 | + const workspace = makeTempDir(); |
| 20 | + const binDir = makeTempDir(); |
| 21 | + const sessionDir = makeTempDir("codex-plugin-broker-"); |
| 22 | + const socketPath = path.join(sessionDir, "broker.sock"); |
| 23 | + const endpoint = `unix:${socketPath}`; |
| 24 | + const pidFile = path.join(sessionDir, "broker.pid"); |
| 25 | + installFakeCodex(binDir); |
| 26 | + |
| 27 | + const child = spawn( |
| 28 | + process.execPath, |
| 29 | + [BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", workspace, "--pid-file", pidFile], |
| 30 | + { |
| 31 | + cwd: workspace, |
| 32 | + env: { |
| 33 | + ...buildEnv(binDir), |
| 34 | + [IDLE_TIMEOUT_ENV]: String(idleTimeoutMs) |
| 35 | + }, |
| 36 | + stdio: ["ignore", "pipe", "pipe"] |
| 37 | + } |
| 38 | + ); |
| 39 | + |
| 40 | + return { child, endpoint, pidFile, socketPath }; |
| 41 | +} |
| 42 | + |
| 43 | +async function waitForExit(child, timeoutMs = 3000) { |
| 44 | + if (child.exitCode != null || child.signalCode != null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + await Promise.race([ |
| 48 | + once(child, "exit"), |
| 49 | + new Promise((_, reject) => setTimeout(() => reject(new Error("Timed out waiting for broker to exit.")), timeoutMs)) |
| 50 | + ]); |
| 51 | +} |
| 52 | + |
| 53 | +function terminateBroker(child) { |
| 54 | + if (child.exitCode == null && child.signalCode == null) { |
| 55 | + child.kill("SIGTERM"); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +test("broker exits and removes runtime files after its last client stays disconnected", async (t) => { |
| 60 | + const broker = spawnTestBroker({ idleTimeoutMs: 150 }); |
| 61 | + t.after(() => terminateBroker(broker.child)); |
| 62 | + |
| 63 | + assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); |
| 64 | + await waitForExit(broker.child); |
| 65 | + |
| 66 | + assert.equal(broker.child.exitCode, 0); |
| 67 | + assert.equal(fs.existsSync(broker.socketPath), false); |
| 68 | + assert.equal(fs.existsSync(broker.pidFile), false); |
| 69 | +}); |
| 70 | + |
| 71 | +test("broker idle shutdown waits until a connected client disconnects", async (t) => { |
| 72 | + const broker = spawnTestBroker({ idleTimeoutMs: 150 }); |
| 73 | + t.after(() => terminateBroker(broker.child)); |
| 74 | + |
| 75 | + assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); |
| 76 | + const socket = net.createConnection({ path: broker.socketPath }); |
| 77 | + await once(socket, "connect"); |
| 78 | + |
| 79 | + await new Promise((resolve) => setTimeout(resolve, 350)); |
| 80 | + assert.equal(broker.child.exitCode, null); |
| 81 | + assert.equal(broker.child.signalCode, null); |
| 82 | + |
| 83 | + socket.end(); |
| 84 | + await once(socket, "close"); |
| 85 | + await waitForExit(broker.child); |
| 86 | + assert.equal(broker.child.exitCode, 0); |
| 87 | +}); |
| 88 | + |
| 89 | +test("broker idle timeout can be disabled", async (t) => { |
| 90 | + const broker = spawnTestBroker({ idleTimeoutMs: 0 }); |
| 91 | + t.after(() => terminateBroker(broker.child)); |
| 92 | + |
| 93 | + assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 350)); |
| 95 | + assert.equal(broker.child.exitCode, null); |
| 96 | + assert.equal(broker.child.signalCode, null); |
| 97 | + |
| 98 | + terminateBroker(broker.child); |
| 99 | + await waitForExit(broker.child); |
| 100 | +}); |
| 101 | + |
| 102 | +test("broker shuts down cleanly while new clients race to connect", async (t) => { |
| 103 | + const broker = spawnTestBroker({ idleTimeoutMs: 0 }); |
| 104 | + t.after(() => terminateBroker(broker.child)); |
| 105 | + |
| 106 | + assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); |
| 107 | + const reconnects = Array.from( |
| 108 | + { length: 50 }, |
| 109 | + () => |
| 110 | + new Promise((resolve) => { |
| 111 | + const socket = net.createConnection({ path: broker.socketPath }); |
| 112 | + const timer = setTimeout(() => socket.destroy(), 1000); |
| 113 | + const finish = () => { |
| 114 | + clearTimeout(timer); |
| 115 | + resolve(); |
| 116 | + }; |
| 117 | + socket.on("connect", () => socket.end()); |
| 118 | + socket.on("error", finish); |
| 119 | + socket.on("close", finish); |
| 120 | + }) |
| 121 | + ); |
| 122 | + |
| 123 | + await Promise.all([sendBrokerShutdown(broker.endpoint), ...reconnects]); |
| 124 | + await waitForExit(broker.child); |
| 125 | + assert.equal(broker.child.exitCode, 0); |
| 126 | +}); |
0 commit comments