fix(test): improve capture reliability and runner cleanup (#609)
* test: make capture fixtures readiness-aware and strengthen assertions * fix(test): escalate child termination and report cleanup failures * perf(test): use runner concurrency and lock shared build outputs
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { access } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
it.runIf(process.platform !== "win32")(
|
||||
"escalates cancellation when an owned command ignores SIGTERM",
|
||||
async () => {
|
||||
const lockName = `runner-cancel-${String(process.pid)}`;
|
||||
const lockPath = join(".cache", "rea-command-locks", `${lockName}.lock`);
|
||||
const runner = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"scripts/run-exclusive.mjs",
|
||||
lockName,
|
||||
process.execPath,
|
||||
"-e",
|
||||
'process.on("SIGTERM", () => {}); process.stdout.write("ready"); setInterval(() => {}, 1000);',
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "pipe"] },
|
||||
);
|
||||
let output = "";
|
||||
const ready = new Promise<void>((resolveReady) => {
|
||||
runner.stdout.on("data", (chunk) => {
|
||||
output += chunk.toString();
|
||||
if (output.includes("ready")) resolveReady();
|
||||
});
|
||||
});
|
||||
const closed = new Promise<{
|
||||
code: number | null;
|
||||
signal: NodeJS.Signals | null;
|
||||
}>((resolveClosed) => {
|
||||
runner.once("close", (code, signal) => resolveClosed({ code, signal }));
|
||||
});
|
||||
|
||||
try {
|
||||
await withTimeout(ready, "runner child did not start");
|
||||
runner.kill("SIGTERM");
|
||||
const result = await withTimeout(
|
||||
closed,
|
||||
"runner did not finish cancellation",
|
||||
);
|
||||
|
||||
expect(result).toEqual({ code: 143, signal: null });
|
||||
} finally {
|
||||
if (runner.exitCode === null) runner.kill("SIGKILL");
|
||||
await withTimeout(closed, "runner cleanup did not finish");
|
||||
}
|
||||
|
||||
expect(output).toContain("ready");
|
||||
await expect(access(lockPath)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
},
|
||||
);
|
||||
|
||||
function withTimeout<T>(promise: Promise<T>, message: string): Promise<T> {
|
||||
let timeout: NodeJS.Timeout | undefined;
|
||||
return Promise.race([
|
||||
promise,
|
||||
new Promise<never>((_, reject) => {
|
||||
timeout = setTimeout(() => reject(new Error(message)), 5_000);
|
||||
}),
|
||||
]).finally(() => clearTimeout(timeout));
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from "../../scripts/lib/verifier-run.mjs";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const CHILD_SHUTDOWN_GRACE_MS = 1_000;
|
||||
|
||||
describe.sequential("verifier run identity", () => {
|
||||
it("allocates, propagates, and reuses one process-local identity", async () => {
|
||||
@@ -102,6 +103,26 @@ describe.sequential("verifier run identity", () => {
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.runIf(process.platform !== "win32")(
|
||||
"bounds cleanup when a verifier child ignores SIGTERM",
|
||||
async () => {
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"-e",
|
||||
"process.on('SIGTERM', () => {}); process.stdout.write('ready'); setInterval(() => {}, 1000)",
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "ignore"] },
|
||||
);
|
||||
try {
|
||||
await childReady(child);
|
||||
} finally {
|
||||
await stopChild(child);
|
||||
}
|
||||
expect(child.signalCode).toBe("SIGKILL");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
const spawnReadyChild = (env: NodeJS.ProcessEnv): ChildProcess =>
|
||||
@@ -123,13 +144,37 @@ const childReady = async (child: ChildProcess): Promise<void> => {
|
||||
|
||||
const stopChild = async (child: ChildProcess): Promise<void> => {
|
||||
if (child.exitCode !== null || child.signalCode !== null) return;
|
||||
const exited = new Promise<void>((resolve) =>
|
||||
child.once("exit", () => resolve()),
|
||||
);
|
||||
child.kill("SIGTERM");
|
||||
await exited;
|
||||
if (await waitForExit(child, CHILD_SHUTDOWN_GRACE_MS)) return;
|
||||
child.kill("SIGKILL");
|
||||
if (!(await waitForExit(child, CHILD_SHUTDOWN_GRACE_MS))) {
|
||||
throw new Error(
|
||||
`verifier child ${String(child.pid)} did not exit after SIGKILL`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const waitForExit = (
|
||||
child: ChildProcess,
|
||||
timeoutMs: number,
|
||||
): Promise<boolean> =>
|
||||
new Promise((resolve) => {
|
||||
if (child.exitCode !== null || child.signalCode !== null) {
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(() => {
|
||||
child.removeListener("exit", onExit);
|
||||
resolve(false);
|
||||
}, timeoutMs);
|
||||
timer.unref();
|
||||
const onExit = (): void => {
|
||||
clearTimeout(timer);
|
||||
resolve(true);
|
||||
};
|
||||
child.once("exit", onExit);
|
||||
});
|
||||
|
||||
const restoreRunId = (runId: string | undefined): void => {
|
||||
if (runId === undefined) delete process.env.REA_PROCESS_RUN_ID;
|
||||
else process.env.REA_PROCESS_RUN_ID = runId;
|
||||
|
||||
Reference in New Issue
Block a user