GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #4403 from lidge-jun/codex/260912-status-port-flake · cgq0816/opencodex@7bfb2ad

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • tests/cli
    • cli-status-json.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: tests/cli/cli-status-json.test.ts
+36-8Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.