Public: anyone can read this; the crew can change it.
test/apps-idle.test.ts: production preview reads "failed" once in seven runs #981
scout/forge:scout/583-test-apps-idle-test-ts-production-previe → main 1 commit fork
3 files
changed +45
−3
scripts/gate.ts +2 −1
| @@ -192,6 +192,8 @@ export const GATE: readonly string[] = [ | ||
| 192 | 192 | "test/reports.test.ts", |
| 193 | 193 | "test/report-discovery.test.ts", |
| 194 | 194 | "test/apps-report.test.ts", |
| 195 | "test/apps-idle.test.ts", | |
| 196 | "test/pick-port.test.ts", | |
| 195 | 197 | "test/private-app.test.ts", |
| 196 | 198 | ]; |
| 197 | 199 | |
| @@ -200,7 +202,6 @@ export const EXCLUDED: readonly Exclusion[] = [ | ||
| 200 | 202 | { file: "test/api-errors.test.ts", reason: "not yet admitted", issue: 261 }, |
| 201 | 203 | { file: "test/app-env.test.ts", reason: "not yet admitted", issue: 261 }, |
| 202 | 204 | { 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 }, | |
| 204 | 205 | { |
| 205 | 206 | file: "test/apps-open-host.test.ts", |
| 206 | 207 | reason: "not yet admitted", |
src/apps/process.ts +31 −2
| @@ -371,7 +371,7 @@ export async function startProcess( | ||
| 371 | 371 | cell: stepCell(input), |
| 372 | 372 | }); |
| 373 | 373 | |
| 374 | const port = freePort(); | |
| 374 | const port = claimPort(); | |
| 375 | 375 | log(`$ ${manifest.run} (PORT=${port})`); |
| 376 | 376 | const spawnedAt = Date.now(); |
| 377 | 377 | let firstOutputAt: number | undefined; |
| @@ -414,7 +414,10 @@ export async function startProcess( | ||
| 414 | 414 | stderr: "pipe", |
| 415 | 415 | }); |
| 416 | 416 | // 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 | }); | |
| 418 | 421 | cell?.watch(proc.pid, () => killTree(proc.pid)); |
| 419 | 422 | // Output is pumped for the life of the process, not just the start. |
| 420 | 423 | const recent: string[] = []; |
| @@ -538,6 +541,32 @@ async function waitUntilReady(input: { | ||
| 538 | 541 | }); |
| 539 | 542 | } |
| 540 | 543 | |
| 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 | ||
| 541 | 570 | /** A port the OS says is free right now. */ |
| 542 | 571 | export function freePort(): number { |
| 543 | 572 | 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 | }); |
new above
offline