File size: 3,977 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { killProcessTree } from "./kill-tree.js";
const { spawnMock } = vi.hoisted(() => ({
spawnMock: vi.fn(),
}));
vi.mock("node:child_process", () => ({
spawn: (...args: unknown[]) => spawnMock(...args),
}));
async function withPlatform<T>(platform: NodeJS.Platform, run: () => Promise<T> | T): Promise<T> {
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
Object.defineProperty(process, "platform", { value: platform, configurable: true });
try {
return await run();
} finally {
if (originalPlatform) {
Object.defineProperty(process, "platform", originalPlatform);
}
}
}
describe("killProcessTree", () => {
let killSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
spawnMock.mockClear();
killSpy = vi.spyOn(process, "kill");
vi.useFakeTimers();
});
afterEach(() => {
killSpy.mockRestore();
vi.useRealTimers();
vi.clearAllMocks();
});
it("on Windows skips delayed force-kill when PID is already gone", async () => {
killSpy.mockImplementation(((pid: number, signal?: NodeJS.Signals | number) => {
if (pid === 4242 && signal === 0) {
throw new Error("ESRCH");
}
return true;
}) as typeof process.kill);
await withPlatform("win32", async () => {
killProcessTree(4242, { graceMs: 25 });
expect(spawnMock).toHaveBeenCalledTimes(1);
expect(spawnMock).toHaveBeenNthCalledWith(
1,
"taskkill",
["/T", "/PID", "4242"],
expect.objectContaining({ detached: true, stdio: "ignore" }),
);
await vi.advanceTimersByTimeAsync(25);
expect(spawnMock).toHaveBeenCalledTimes(1);
});
});
it("on Windows force-kills after grace period only when PID still exists", async () => {
killSpy.mockImplementation(((pid: number, signal?: NodeJS.Signals | number) => {
if (pid === 5252 && signal === 0) {
return true;
}
return true;
}) as typeof process.kill);
await withPlatform("win32", async () => {
killProcessTree(5252, { graceMs: 10 });
await vi.advanceTimersByTimeAsync(10);
expect(spawnMock).toHaveBeenCalledTimes(2);
expect(spawnMock).toHaveBeenNthCalledWith(
1,
"taskkill",
["/T", "/PID", "5252"],
expect.objectContaining({ detached: true, stdio: "ignore" }),
);
expect(spawnMock).toHaveBeenNthCalledWith(
2,
"taskkill",
["/F", "/T", "/PID", "5252"],
expect.objectContaining({ detached: true, stdio: "ignore" }),
);
});
});
it("on Unix sends SIGTERM first and skips SIGKILL when process exits", async () => {
killSpy.mockImplementation(((pid: number, signal?: NodeJS.Signals | number) => {
if (pid === -3333 && signal === 0) {
throw new Error("ESRCH");
}
if (pid === 3333 && signal === 0) {
throw new Error("ESRCH");
}
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
killProcessTree(3333, { graceMs: 10 });
await vi.advanceTimersByTimeAsync(10);
expect(killSpy).toHaveBeenCalledWith(-3333, "SIGTERM");
expect(killSpy).not.toHaveBeenCalledWith(-3333, "SIGKILL");
expect(killSpy).not.toHaveBeenCalledWith(3333, "SIGKILL");
});
});
it("on Unix sends SIGKILL after grace period when process is still alive", async () => {
killSpy.mockImplementation(((pid: number, signal?: NodeJS.Signals | number) => {
if (pid === -4444 && signal === 0) {
return true;
}
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
killProcessTree(4444, { graceMs: 5 });
await vi.advanceTimersByTimeAsync(5);
expect(killSpy).toHaveBeenCalledWith(-4444, "SIGTERM");
expect(killSpy).toHaveBeenCalledWith(-4444, "SIGKILL");
});
});
});
|