|
| 1 | +import { spawn } from "node:child_process" |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import { ProcessTree } from "../../script/process-tree" |
| 4 | + |
| 5 | +describe("ProcessTree.plan", () => { |
| 6 | + test("Windows: taskkill walks the tree from the child", () => { |
| 7 | + expect(ProcessTree.plan({ platform: "win32", pid: 42, running: true })).toEqual({ |
| 8 | + kind: "taskkill", |
| 9 | + args: ["/pid", "42", "/T", "/F"], |
| 10 | + }) |
| 11 | + }) |
| 12 | + |
| 13 | + test("macOS and Linux: the child's whole process group is signalled, not the child alone", () => { |
| 14 | + expect(ProcessTree.plan({ platform: "linux", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 }) |
| 15 | + expect(ProcessTree.plan({ platform: "darwin", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 }) |
| 16 | + }) |
| 17 | + |
| 18 | + test("a child that has exited is never targeted: its PID may be reused", () => { |
| 19 | + expect(ProcessTree.plan({ platform: "win32", pid: 42, running: false })).toBeUndefined() |
| 20 | + expect(ProcessTree.plan({ platform: "linux", pid: 42, running: false })).toBeUndefined() |
| 21 | + expect(ProcessTree.plan({ platform: "linux", pid: undefined, running: true })).toBeUndefined() |
| 22 | + }) |
| 23 | + |
| 24 | + test("on macOS and Linux the child leads its own group; Windows gets no console window", () => { |
| 25 | + expect(ProcessTree.spawnOptions("linux").detached).toBe(true) |
| 26 | + expect(ProcessTree.spawnOptions("darwin").detached).toBe(true) |
| 27 | + expect(ProcessTree.spawnOptions("win32").detached).toBe(false) |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +const alive = (pid: number) => { |
| 32 | + try { |
| 33 | + process.kill(pid, 0) |
| 34 | + return true |
| 35 | + } catch { |
| 36 | + return false |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe("ProcessTree.stop", () => { |
| 41 | + // Runs the real path for this platform: taskkill on Windows, the group signal elsewhere. |
| 42 | + test("stops the child and the process it started", async () => { |
| 43 | + const script = `const g = Bun.spawn([process.execPath, "-e", "setTimeout(() => {}, 60000)"]); console.log(g.pid); setTimeout(() => {}, 60000)` |
| 44 | + const child = spawn(process.execPath, ["-e", script], { |
| 45 | + ...ProcessTree.spawnOptions(), |
| 46 | + stdio: ["ignore", "pipe", "ignore"], |
| 47 | + }) |
| 48 | + const grandchild = await new Promise<number>((done) => |
| 49 | + child.stdout!.once("data", (data) => done(Number(data.toString().trim()))), |
| 50 | + ) |
| 51 | + expect(alive(grandchild)).toBe(true) |
| 52 | + |
| 53 | + await ProcessTree.stop(child, 5000) |
| 54 | + |
| 55 | + expect(child.exitCode !== null || child.signalCode !== null).toBe(true) |
| 56 | + const deadline = Date.now() + 5000 |
| 57 | + while (alive(grandchild) && Date.now() < deadline) await Bun.sleep(100) |
| 58 | + expect(alive(grandchild)).toBe(false) |
| 59 | + }, 30_000) |
| 60 | +}) |
0 commit comments