Skip to content

Commit 94b609a

Browse files
committed
test(cli): stop judging a stale record on a port another test can take
The fallback-port fixture records a port from allocateFreePort, which reports the port it has already released. On a four-shard runner every other test binding an ephemeral port is a candidate to take it, and when that happens status finds a listener on the recorded port, reports the record as live, and the assertion fails against something the test never set up. It failed exactly that way on the preview promotion run while the same commit passed on dev. Confirm the recorded port refuses immediately before and immediately after the status probe, and re-allocate when something took it in between. The assertion is unchanged and no weaker: a run only counts when the endpoint demonstrably refused across the whole probe, and exhausting the attempts fails with that reason rather than silently passing.
1 parent db7062c commit 94b609a

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

‎tests/cli/cli-status-json.test.ts‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, spyOn, test } from "bun:test";
22
import { createHash } from "node:crypto";
33
import { spawnSync } from "node:child_process";
44
import { existsSync, mkdtempSync, readdirSync, readFileSync, writeFileSync, mkdirSync, unlinkSync } from "node:fs";
5-
import { createServer } from "node:net";
5+
import { createConnection, createServer } from "node:net";
66
import type { AddressInfo } from "node:net";
77
import { tmpdir } from "node:os";
88
import { dirname, join } from "node:path";
@@ -879,6 +879,24 @@ describe("status reports stale process records end to end", () => {
879879
await new Promise<void>(resolve => { probe.close(() => resolve()); });
880880
return port;
881881
}
882+
883+
/**
884+
* Prove the endpoint refuses right now.
885+
*
886+
* `allocateFreePort` releases the port it reports, and on a sharded runner every other
887+
* test binding an ephemeral port is a candidate to take it. A fixture that assumes a
888+
* released port is still refusing asserts against whatever happened to bind it.
889+
*/
890+
async function refusesConnection(port: number): Promise<boolean> {
891+
return await new Promise<boolean>(resolve => {
892+
const socket = createConnection({ port, host: "127.0.0.1" });
893+
const settle = (refused: boolean): void => { socket.destroy(); resolve(refused); };
894+
socket.setTimeout(1_000);
895+
socket.once("connect", () => settle(false));
896+
socket.once("timeout", () => settle(false));
897+
socket.once("error", () => settle(true));
898+
});
899+
}
882900
let freePort: number;
883901
beforeEach(async () => { freePort = await allocateFreePort(); });
884902

@@ -950,17 +968,27 @@ describe("status reports stale process records end to end", () => {
950968
await new Promise<void>(resolve => { occupied.listen(0, "127.0.0.1", () => resolve()); });
951969
const occupiedPort = (occupied.address() as AddressInfo).port;
952970
try {
953-
// Allocate after the listener is bound: it can reuse the port released by
954-
// beforeEach, so that earlier number no longer proves a refused endpoint.
955-
const recordedPort = await allocateFreePort();
956-
expect(recordedPort).not.toBe(occupiedPort);
957971
const pid = findDeadPid();
958972
writeFileSync(join(home, "config.json"), JSON.stringify({ port: occupiedPort, codexAutoStart: false }), "utf8");
959973
writeFileSync(join(home, "ocx.pid"), String(pid), "utf8");
960-
writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: recordedPort, hostname: "127.0.0.1" }), "utf8");
961974

962-
const parsed = JSON.parse(runStatusJson(home).stdout) as { proxy?: { staleProcessState?: unknown } };
963-
expect(parsed.proxy?.staleProcessState).toBe(true);
975+
// The recorded port has to refuse for this to discriminate, and `allocateFreePort`
976+
// hands back a port it has already released. Confirm refusal immediately before and
977+
// immediately after the probe, and re-allocate when something took it in between, so
978+
// a stolen port retries instead of failing an assertion it never exercised.
979+
let parsed: { proxy?: { staleProcessState?: unknown } } | undefined;
980+
for (let attempt = 0; attempt < 5 && parsed === undefined; attempt++) {
981+
const recordedPort = await allocateFreePort();
982+
if (recordedPort === occupiedPort) continue;
983+
if (!await refusesConnection(recordedPort)) continue;
984+
writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: recordedPort, hostname: "127.0.0.1" }), "utf8");
985+
const observed = JSON.parse(runStatusJson(home).stdout) as { proxy?: { staleProcessState?: unknown } };
986+
if (!await refusesConnection(recordedPort)) continue;
987+
parsed = observed;
988+
}
989+
990+
expect(parsed, "no allocated port stayed refused across the status probe").toBeDefined();
991+
expect(parsed?.proxy?.staleProcessState).toBe(true);
964992
} finally {
965993
await new Promise<void>(resolve => { occupied.close(() => resolve()); });
966994
removeTreeWithRetry(home);

0 commit comments

Comments
 (0)