fix(bench): kill gateway child trees on windows

This commit is contained in:
Vincent Koc
2026-06-20 13:30:28 +02:00
parent 087d999fce
commit 0cd8db97f9
2 changed files with 97 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
// Gateway benchmark child test support simulates child process behavior for script tests.
import type { spawnSync } from "node:child_process";
import { EventEmitter } from "node:events";
import { expect, it, vi } from "vitest";
@@ -10,7 +11,12 @@ type StopChildResult = {
type StopChild<TChild> = (
child: TChild,
options?: { killGraceMs?: number; teardownGraceMs?: number },
options?: {
killGraceMs?: number;
platform?: NodeJS.Platform;
runTaskkill?: typeof spawnSync;
teardownGraceMs?: number;
},
) => Promise<StopChildResult>;
export function registerStopChildBehaviorTests<TChild>(params: {
@@ -101,6 +107,52 @@ export function registerStopChildBehaviorTests<TChild>(params: {
expect(child.unref).toHaveBeenCalledOnce();
});
it("signals Windows child process trees with taskkill", async () => {
const child = new EventEmitter() as EventEmitter & {
exitCode: number | null;
kill: ReturnType<typeof vi.fn>;
pid: number;
signalCode: NodeJS.Signals | null;
stderr: { destroy: ReturnType<typeof vi.fn> };
stdin: { destroy: ReturnType<typeof vi.fn> };
stdout: { destroy: ReturnType<typeof vi.fn> };
unref: ReturnType<typeof vi.fn>;
};
child.exitCode = null;
child.kill = vi.fn(() => true);
child.pid = 4450;
child.signalCode = null;
child.stderr = { destroy: vi.fn() };
child.stdin = { destroy: vi.fn() };
child.stdout = { destroy: vi.fn() };
child.unref = vi.fn();
const runTaskkill = vi.fn(() => ({ error: undefined, status: 0 }));
await expect(
params.stopChild(child as unknown as TChild, {
killGraceMs: 1,
platform: "win32",
runTaskkill,
teardownGraceMs: 1,
}),
).resolves.toEqual({
exitedBeforeTeardown: false,
exitCode: null,
signal: "SIGKILL",
});
expect(runTaskkill).toHaveBeenNthCalledWith(1, "taskkill", ["/PID", "4450", "/T"], {
stdio: "ignore",
});
expect(runTaskkill).toHaveBeenNthCalledWith(2, "taskkill", ["/PID", "4450", "/T", "/F"], {
stdio: "ignore",
});
expect(child.kill).not.toHaveBeenCalled();
expect(child.stdin.destroy).toHaveBeenCalledOnce();
expect(child.stdout.destroy).toHaveBeenCalledOnce();
expect(child.stderr.destroy).toHaveBeenCalledOnce();
expect(child.unref).toHaveBeenCalledOnce();
});
it.skipIf(process.platform === "win32")(
"preserves pre-teardown wrapper exits while cleaning the process group",
async () => {
@@ -144,9 +196,7 @@ export function registerStopChildBehaviorTests<TChild>(params: {
child.exitCode = 0;
child.emit("exit", 0, null);
});
await expect(
stopped,
).resolves.toEqual({
await expect(stopped).resolves.toEqual({
exitedBeforeTeardown: true,
exitCode: 0,
signal: null,