From 2002f1f61426abced4e97a64b9e3d38fa1df2ff6 Mon Sep 17 00:00:00 2001 From: "homeboy-ci[bot]" <266378653+homeboy-ci[bot]@users.noreply.github.com> Date: Mon, 24 Aug 2026 22:18:45 +0000 Subject: [PATCH] test: make timeout smoke deterministic --- scripts/playground-command-timeout-smoke.ts | 90 ++++++++++++++++----- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/scripts/playground-command-timeout-smoke.ts b/scripts/playground-command-timeout-smoke.ts index 276d3ba38..b628bdcf0 100644 --- a/scripts/playground-command-timeout-smoke.ts +++ b/scripts/playground-command-timeout-smoke.ts @@ -2,19 +2,45 @@ import assert from "node:assert/strict" import { createRuntime } from "../packages/runtime-core/src/index.js" import { createPlaygroundRuntimeBackend, type PlaygroundCliModule } from "../packages/runtime-playground/src/index.js" -let runCalled = false +const commandTimeoutMs = 25 +let runCalls = 0 +let runtimeDisposed = false +let timedRunCompleted = false +let resolveRunEntered!: () => void +let resolveTimedRun!: () => void +let resolveTimedRunSettled!: () => void +const runEntered = new Promise((resolve) => { + resolveRunEntered = resolve +}) +const timedRun = new Promise<{ text: string; exitCode: number }>((resolve) => { + resolveTimedRun = () => resolve({ text: "", exitCode: 0 }) +}) +const timedRunSettled = new Promise((resolve) => { + resolveTimedRunSettled = resolve +}) const fakeCliModule: PlaygroundCliModule = { runCLI: async () => ({ serverUrl: "http://127.0.0.1:9400", playground: { run: async () => { - runCalled = true - return await new Promise(() => undefined) + runCalls += 1 + if (runCalls === 1) { + return { text: "warm", exitCode: 0 } + } + + resolveRunEntered() + try { + return await timedRun + } finally { + timedRunCompleted = true + resolveTimedRunSettled() + } }, }, async [Symbol.asyncDispose]() { - return undefined + runtimeDisposed = true + resolveTimedRun() }, }), } @@ -31,26 +57,50 @@ const runtime = await createRuntime({ }, }, createPlaygroundRuntimeBackend({ cliModule: fakeCliModule })) -await assert.rejects( - () => runtime.execute({ +try { + // Keep runtime startup outside the deliberately tiny in-flight command budget. + await runtime.execute({ + command: "wordpress.run-php", + args: ["code=echo 'warm';"], + }) + + const startedAt = Date.now() + const execution = runtime.execute({ command: "wordpress.run-php", args: ["code=echo 'never';"], - timeoutMs: 25, - }), - (error) => { - assert.ok(error instanceof Error) - assert.match(error.message, /Runtime command wordpress\.run-php exceeded timeoutMs=25/) - return true - }, -) + timeoutMs: commandTimeoutMs, + }) -assert.equal(runCalled, true) + const enteredBeforeSettlement = await Promise.race([ + runEntered.then(() => true), + execution.then(() => false, () => false), + ]) + assert.equal(enteredBeforeSettlement, true, "the timed command must enter Playground run() before settling") -const observation = await runtime.observe({ type: "command-result" }) -const commandResult = observation.data as { exitCode?: number; stderr?: string } -assert.equal(commandResult.exitCode, 1) -assert.match(commandResult.stderr ?? "", /timeoutMs=25/) + await assert.rejects( + () => execution, + (error) => { + assert.ok(error instanceof Error) + assert.match(error.message, /Runtime command wordpress\.run-php exceeded timeoutMs=25/) + return true + }, + ) + + const elapsedMs = Date.now() - startedAt + assert.ok(elapsedMs >= commandTimeoutMs, `timeout fired early after ${elapsedMs}ms`) + assert.ok(elapsedMs < 1_000, `timeout was not bounded: ${elapsedMs}ms`) + assert.equal(timedRunCompleted, false, "timeout must cancel execution without waiting for the backend run to settle") + + const observation = await runtime.observe({ type: "command-result" }) + const commandResult = observation.data as { exitCode?: number; stderr?: string } + assert.equal(commandResult.exitCode, 1) + assert.match(commandResult.stderr ?? "", /timeoutMs=25/) +} finally { + await runtime.destroy() +} -await runtime.destroy() +await timedRunSettled +assert.equal(timedRunCompleted, true) +assert.equal(runtimeDisposed, true, "runtime teardown must terminate the in-flight fake backend run") console.log("playground command timeout smoke passed")