Skip to content

Commit bf7060e

Browse files
committed
test(qa): always stop log follow child
1 parent dc6678a commit bf7060e

2 files changed

Lines changed: 82 additions & 25 deletions

File tree

‎test/e2e/qa-lab/runtime/remote-log-tailing-runtime.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { spawn } from "node:child_process";
2+
import { once } from "node:events";
13
import { readFileSync } from "node:fs";
24
import path from "node:path";
35
import { describe, expect, it } from "vitest";
6+
import { withOwnedFollowChild } from "./remote-log-tailing-runtime.js";
47

58
describe("remote log tailing scenario", () => {
69
it("declares packaged CLI, RPC bounds, cursor, follow, and owned SIGINT proof", () => {
@@ -12,7 +15,22 @@ describe("remote log tailing scenario", () => {
1215
expect(source).toContain("first.cursor");
1316
expect(source).toContain('"--max-bytes"');
1417
expect(source).toContain('"--follow"');
15-
expect(source).toContain('child.kill("SIGINT")');
18+
expect(source).toContain("withOwnedFollowChild(child");
1619
expect(source).toContain('path.join(repoRoot, "dist", "index.js")');
1720
});
21+
22+
it("stops and awaits the follow child when the owned operation fails", async () => {
23+
const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], {
24+
stdio: "ignore",
25+
});
26+
await once(child, "spawn");
27+
28+
await expect(
29+
withOwnedFollowChild(child, async () => {
30+
throw new Error("forced follow failure");
31+
}),
32+
).rejects.toThrow("forced follow failure");
33+
34+
expect(child.signalCode).not.toBeNull();
35+
});
1836
});

‎test/e2e/qa-lab/runtime/remote-log-tailing-runtime.ts‎

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn } from "node:child_process";
1+
import { spawn, type ChildProcess } from "node:child_process";
22
import { appendFile, mkdir } from "node:fs/promises";
33
import path from "node:path";
44
import { startQaGatewayChild } from "../../../../extensions/qa-lab/api.js";
@@ -24,6 +24,54 @@ function logLine(message: string): string {
2424
})}\n`;
2525
}
2626

27+
function hasExited(child: ChildProcess): boolean {
28+
return child.exitCode !== null || child.signalCode !== null;
29+
}
30+
31+
function waitForClose(child: ChildProcess, timeoutMs: number): Promise<void> {
32+
if (hasExited(child)) {
33+
return Promise.resolve();
34+
}
35+
return new Promise((resolve, reject) => {
36+
const onClose = () => {
37+
clearTimeout(timer);
38+
resolve();
39+
};
40+
const timer = setTimeout(() => {
41+
child.off("close", onClose);
42+
reject(new Error("follow child did not close before timeout"));
43+
}, timeoutMs);
44+
child.once("close", onClose);
45+
});
46+
}
47+
48+
async function stopFollowChild(child: ChildProcess): Promise<void> {
49+
if (hasExited(child)) {
50+
return;
51+
}
52+
child.kill("SIGINT");
53+
try {
54+
await waitForClose(child, 5_000);
55+
} catch {
56+
if (hasExited(child)) {
57+
return;
58+
}
59+
child.kill("SIGKILL");
60+
await waitForClose(child, 5_000);
61+
}
62+
}
63+
64+
export async function withOwnedFollowChild<T>(
65+
child: ChildProcess,
66+
operation: () => Promise<T>,
67+
): Promise<T> {
68+
try {
69+
return await operation();
70+
} finally {
71+
await stopFollowChild(child);
72+
}
73+
}
74+
2775
export async function runRemoteLogTailing(repoRoot: string, outputRoot: string) {
2876
const logPath = path.join(outputRoot, "gateway.jsonl");
2977
await mkdir(outputRoot, { recursive: true });
@@ -127,32 +175,23 @@ export async function runRemoteLogTailing(repoRoot: string, outputRoot: string)
127175
child.stderr.on("data", (chunk) => {
128176
stderr += String(chunk);
129177
});
130-
await new Promise<void>((resolve) => {
131-
setTimeout(resolve, 250);
132-
});
133-
await appendFile(logPath, logLine("qa-follow-line"));
134-
const deadline = Date.now() + 10_000;
135-
while (!stdout.includes("qa-follow-line") && Date.now() < deadline) {
178+
const followOutput = await withOwnedFollowChild(child, async () => {
136179
await new Promise<void>((resolve) => {
137-
setTimeout(resolve, 50);
138-
});
139-
}
140-
if (!stdout.includes("qa-follow-line")) {
141-
child.kill("SIGINT");
142-
throw new Error(`follow did not receive appended record: ${stderr}`);
143-
}
144-
child.kill("SIGINT");
145-
await new Promise<void>((resolve, reject) => {
146-
const timer = setTimeout(
147-
() => reject(new Error("follow child did not exit after SIGINT")),
148-
5000,
149-
);
150-
child.once("close", () => {
151-
clearTimeout(timer);
152-
resolve();
180+
setTimeout(resolve, 250);
153181
});
182+
await appendFile(logPath, logLine("qa-follow-line"));
183+
const deadline = Date.now() + 10_000;
184+
while (!stdout.includes("qa-follow-line") && Date.now() < deadline) {
185+
await new Promise<void>((resolve) => {
186+
setTimeout(resolve, 50);
187+
});
188+
}
189+
if (!stdout.includes("qa-follow-line")) {
190+
throw new Error(`follow did not receive appended record: ${stderr}`);
191+
}
192+
return stdout;
154193
});
155-
return { first, bounded, cursorTail, cliRecords, followOutput: stdout };
194+
return { first, bounded, cursorTail, cliRecords, followOutput };
156195
} finally {
157196
await gateway.stop();
158197
}

0 commit comments

Comments
 (0)