Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3
4import { describe, expect, it } from "@effect/vitest";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6
7import { signalProcessGroup } from "./processGroup.ts";
8
9const errorCode = (run: () => void) => {
10 try {
11 run();
12 return undefined;
13 } catch (cause) {
14 return (cause as NodeJS.ErrnoException).code;
15 }
16};
17
18describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("signalProcessGroup", () => {
19 // Signal 0 probes without killing.
20 it("never reaches this server's group or every process the user owns", () => {
21 for (const pid of [1, 0, -1, Number.NaN, 1.5]) {
22 expect(
23 errorCode(() => signalProcessGroup(pid, 0)),
24 `pid ${pid}`,
25 ).toBe("ESRCH");
26 }
27 });
28
29 it("signals a spawned process group", async () => {
30 const child = NodeChildProcess.spawn("/bin/sh", ["-c", "sleep 600 & wait"], {
31 detached: true,
32 stdio: "ignore",
33 });
34 const exited = new Promise<NodeJS.Signals | null>((resolve) =>
35 child.once("exit", (_code, signal) => resolve(signal)),
36 );
37 const pid = child.pid!;
38
39 expect(errorCode(() => signalProcessGroup(pid, 0))).toBeUndefined();
40 signalProcessGroup(pid, "SIGKILL");
41 expect(await exited).toBe("SIGKILL");
42 });
43});