test: replaced blind sleeps with shared fixtures and condition polling
- Shared immutable model registries and auth storage via beforeAll/afterAll. - Swapped fixed-delay settle sleeps for predicate polling and signals. - Stubbed network/timers to drop wall-clock waits in registry and history tests. - Added resetDisplay invalidation tests and startup-timing breakdown lines.
This commit is contained in:
@@ -14,12 +14,27 @@ import * as piNatives from "@oh-my-pi/pi-natives";
|
||||
const ARTIFACT_HEAD_BYTES_DEFAULT = 20 * 1024;
|
||||
const BACKGROUND_COMPLETION_RACE_MS = 750;
|
||||
const KILL_MARKER_DELAY_SECONDS = "0.4";
|
||||
const KILL_MARKER_ASSERTION_WAIT_MS = 900;
|
||||
const KILL_MARKER_DELAY_MS = 400;
|
||||
// We prove a killed process never wrote its marker by observing until the
|
||||
// wall-clock instant the marker WOULD have appeared (spawn + delay) plus a
|
||||
// margin. Anchoring the deadline to a pre-spawn timestamp — instead of blindly
|
||||
// sleeping a fixed amount after executeBash returns — keeps the wait bounded
|
||||
// without shrinking the kill-propagation margin: the timeout/abort fires at
|
||||
// ~100ms, well before the 400ms marker write, so the margin between kill and
|
||||
// write is unchanged; only the redundant observation tail goes away.
|
||||
const KILL_MARKER_OBSERVE_MARGIN_MS = 300;
|
||||
|
||||
function makeTempDir(): string {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), "omp-bash-exec-"));
|
||||
}
|
||||
|
||||
/** Spin-wait until the wall-clock deadline, polling rather than blind-sleeping. */
|
||||
async function waitUntil(deadlineMs: number): Promise<void> {
|
||||
while (Date.now() < deadlineMs) {
|
||||
await Bun.sleep(20);
|
||||
}
|
||||
}
|
||||
|
||||
describe("executeBash", () => {
|
||||
let tempDir: string;
|
||||
|
||||
@@ -532,6 +547,7 @@ describe("executeBash", () => {
|
||||
const markerEscaped = marker.replace(/'/g, "'\\''");
|
||||
|
||||
// Command creates marker after a short delay, but we timeout before then.
|
||||
const start = Date.now();
|
||||
const result = await executeBash(`sleep ${KILL_MARKER_DELAY_SECONDS} && echo done > '${markerEscaped}'`, {
|
||||
cwd: tempDir,
|
||||
timeout: 100,
|
||||
@@ -539,10 +555,9 @@ describe("executeBash", () => {
|
||||
|
||||
expect(result.cancelled).toBe(true);
|
||||
|
||||
// Wait longer than the command would have needed to create the marker.
|
||||
await Bun.sleep(KILL_MARKER_ASSERTION_WAIT_MS);
|
||||
|
||||
// If process was killed (not orphaned), marker should NOT exist
|
||||
// Observe past the instant the marker would have been written had the
|
||||
// process survived. If it was killed (not orphaned), it never appears.
|
||||
await waitUntil(start + KILL_MARKER_DELAY_MS + KILL_MARKER_OBSERVE_MARGIN_MS);
|
||||
expect(fs.existsSync(marker)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -552,6 +567,7 @@ describe("executeBash", () => {
|
||||
const marker = path.join(tempDir, "marker-bg.txt");
|
||||
const markerEscaped = marker.replace(/'/g, "'\\''");
|
||||
|
||||
const start = Date.now();
|
||||
const result = await executeBash(
|
||||
`{ sleep ${KILL_MARKER_DELAY_SECONDS}; echo done > '${markerEscaped}'; } & sleep 10`,
|
||||
{
|
||||
@@ -562,7 +578,7 @@ describe("executeBash", () => {
|
||||
|
||||
expect(result.cancelled).toBe(true);
|
||||
|
||||
await Bun.sleep(KILL_MARKER_ASSERTION_WAIT_MS);
|
||||
await waitUntil(start + KILL_MARKER_DELAY_MS + KILL_MARKER_OBSERVE_MARGIN_MS);
|
||||
expect(fs.existsSync(marker)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -597,9 +613,13 @@ describe("executeBash", () => {
|
||||
expect(result.cancelled).toBe(true);
|
||||
expect(result.output).toContain("Command cancelled");
|
||||
|
||||
await Bun.sleep(KILL_MARKER_ASSERTION_WAIT_MS);
|
||||
// The backgrounded subshell only writes its marker once `release` exists.
|
||||
// If abort failed to kill the process group, the orphan is still polling
|
||||
// for `release` every 50ms — touching it makes a survivor react within one
|
||||
// poll. A short settle first lets the kill signal propagate before we probe.
|
||||
await Bun.sleep(100);
|
||||
fs.writeFileSync(release, "");
|
||||
await Bun.sleep(150);
|
||||
await Bun.sleep(200);
|
||||
expect(fs.existsSync(marker)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -611,6 +631,7 @@ describe("executeBash", () => {
|
||||
const controller = new AbortController();
|
||||
|
||||
// Command creates marker after a short delay.
|
||||
const start = Date.now();
|
||||
const promise = executeBash(`sleep ${KILL_MARKER_DELAY_SECONDS} && echo done > '${markerEscaped}'`, {
|
||||
cwd: tempDir,
|
||||
timeout: 10000,
|
||||
@@ -625,10 +646,9 @@ describe("executeBash", () => {
|
||||
expect(result.cancelled).toBe(true);
|
||||
expect(result.output).toContain("Command cancelled");
|
||||
|
||||
// Wait longer than the command would have needed to create the marker.
|
||||
await Bun.sleep(KILL_MARKER_ASSERTION_WAIT_MS);
|
||||
|
||||
// If process was killed (not orphaned), marker should NOT exist
|
||||
// Observe past the instant the marker would have been written had the
|
||||
// process survived. If it was killed (not orphaned), it never appears.
|
||||
await waitUntil(start + KILL_MARKER_DELAY_MS + KILL_MARKER_OBSERVE_MARGIN_MS);
|
||||
expect(fs.existsSync(marker)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user