Public: anyone can read this; the crew can change it.

test/apps-idle.test.ts: production preview reads "failed" once in seven runs #981

Closed opened this
Closed without merging

scout/forge:scout/583-test-apps-idle-test-ts-production-previe → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +45 −3
scripts/gate.ts +2 −1
@@ -192,6 +192,8 @@ export const GATE: readonly string[] = [
192192 "test/reports.test.ts",
193193 "test/report-discovery.test.ts",
194194 "test/apps-report.test.ts",
195 "test/apps-idle.test.ts",
196 "test/pick-port.test.ts",
195197 "test/private-app.test.ts",
196198 ];
197199
@@ -200,7 +202,6 @@ export const EXCLUDED: readonly Exclusion[] = [
200202 { file: "test/api-errors.test.ts", reason: "not yet admitted", issue: 261 },
201203 { file: "test/app-env.test.ts", reason: "not yet admitted", issue: 261 },
202204 { file: "test/apps-api.test.ts", reason: "not yet admitted", issue: 261 },
203 { file: "test/apps-idle.test.ts", reason: "not yet admitted", issue: 261 },
204205 {
205206 file: "test/apps-open-host.test.ts",
206207 reason: "not yet admitted",
src/apps/process.ts +31 −2
@@ -371,7 +371,7 @@ export async function startProcess(
371371 cell: stepCell(input),
372372 });
373373
374 const port = freePort();
374 const port = claimPort();
375375 log(`$ ${manifest.run} (PORT=${port})`);
376376 const spawnedAt = Date.now();
377377 let firstOutputAt: number | undefined;
@@ -414,7 +414,10 @@ export async function startProcess(
414414 stderr: "pipe",
415415 });
416416 // The cell's sockets and watch end with the process, however it ends.
417 void proc.exited.then(() => cell?.close());
417 void proc.exited.then(() => {
418 cell?.close();
419 claimed.delete(port);
420 });
418421 cell?.watch(proc.pid, () => killTree(proc.pid));
419422 // Output is pumped for the life of the process, not just the start.
420423 const recent: string[] = [];
@@ -538,6 +541,32 @@ async function waitUntilReady(input: {
538541 });
539542 }
540543
544 /** Ports handed to processes of this forge that have not exited yet. */
545 const claimed = new Set<number>();
546
547 /**
548 * Pick a port no live process of ours holds. The OS only says a port is free
549 * the moment it is asked, so two starts racing under the deploy gate could be
550 * told the same one — the second app then fails to bind, or the health check
551 * is answered by the first. `taken` is what is already handed out.
552 */
553 export function pickPort(
554 probe: () => number,
555 taken: ReadonlySet<number>,
556 ): number {
557 for (let tries = 0; tries < 50; tries++) {
558 const port = probe();
559 if (!taken.has(port)) return port;
560 }
561 return probe();
562 }
563
564 function claimPort(): number {
565 const port = pickPort(freePort, claimed);
566 claimed.add(port);
567 return port;
568 }
569
541570 /** A port the OS says is free right now. */
542571 export function freePort(): number {
543572 const probe = Bun.serve({
test/pick-port.test.ts added +12
@@ -0,0 +1,12 @@
1 import { expect, test } from "bun:test";
2 import { pickPort } from "../src/apps/process.ts";
3
4 test("a port a live process already holds is never handed out again", () => {
5 const offered = [4001, 4001, 4002];
6 const port = pickPort(() => offered.shift()!, new Set([4001]));
7 expect(port).toBe(4002);
8 });
9
10 test("a port nobody holds is taken as offered", () => {
11 expect(pickPort(() => 4003, new Set([4001]))).toBe(4003);
12 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs