|
| 1 | +import { spawn, type ChildProcess } from "node:child_process"; |
| 2 | +import { existsSync, mkdtempSync, readFileSync, readdirSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | +import { waitForTestCondition } from "../utils/processTestUtils.js"; |
| 8 | + |
| 9 | +const fixturePath = join(dirname(fileURLToPath(import.meta.url)), "launch.fixture.ts"); |
| 10 | +const children = new Set<ChildProcess>(); |
| 11 | + |
| 12 | +/** Pids of every process whose command line carries this profile dir (Chromium and its helpers). */ |
| 13 | +function browserPids(profileDir: string): number[] { |
| 14 | + return readdirSync("/proc") |
| 15 | + .filter((name) => /^\d+$/.test(name)) |
| 16 | + .filter((name) => { |
| 17 | + try { |
| 18 | + return readFileSync(`/proc/${name}/cmdline`, "utf8").includes(profileDir); |
| 19 | + } catch { |
| 20 | + return false; |
| 21 | + } |
| 22 | + }) |
| 23 | + .map(Number); |
| 24 | +} |
| 25 | + |
| 26 | +function startFixture(profileDir: string, readyPath: string, mode = "launch"): ChildProcess { |
| 27 | + const child = spawn( |
| 28 | + process.execPath, |
| 29 | + ["--import", "tsx", fixturePath, profileDir, readyPath, mode], |
| 30 | + { stdio: "ignore" }, |
| 31 | + ); |
| 32 | + children.add(child); |
| 33 | + return child; |
| 34 | +} |
| 35 | + |
| 36 | +const exited = (child: ChildProcess) => |
| 37 | + new Promise<{ code: number | null; signal: string | null }>((resolve) => |
| 38 | + child.once("exit", (code, signal) => resolve({ code, signal })), |
| 39 | + ); |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + for (const child of children) child.kill("SIGKILL"); |
| 43 | + children.clear(); |
| 44 | +}); |
| 45 | + |
| 46 | +describe.skipIf(process.platform !== "linux")("managed browser shutdown", () => { |
| 47 | + it.each([ |
| 48 | + ["SIGTERM", "ready", 143], |
| 49 | + ["SIGINT", "ready", 130], |
| 50 | + ["SIGTERM", "launching", 143], |
| 51 | + ] as const)( |
| 52 | + "%s while %s leaves no browser and asks the CLI to exit", |
| 53 | + async (signal, phase, code) => { |
| 54 | + const dir = mkdtempSync(join(tmpdir(), "hf-launch-")); |
| 55 | + const profileDir = join(dir, "profile"); |
| 56 | + const readyPath = join(dir, "ready"); |
| 57 | + const child = startFixture(profileDir, readyPath); |
| 58 | + const done = exited(child); |
| 59 | + await waitForTestCondition( |
| 60 | + () => existsSync(readyPath) && readFileSync(readyPath, "utf8") === phase, |
| 61 | + 30_000, |
| 62 | + ); |
| 63 | + if (phase === "ready") |
| 64 | + await waitForTestCondition(() => browserPids(profileDir).length > 0, 30_000); |
| 65 | + child.kill(signal); |
| 66 | + await done; |
| 67 | + expect(readFileSync(`${readyPath}.exit`, "utf8")).toBe(String(code)); |
| 68 | + await waitForTestCondition(() => browserPids(profileDir).length === 0, 5_000); |
| 69 | + }, |
| 70 | + 60_000, |
| 71 | + ); |
| 72 | + |
| 73 | + it("SIGTERM during the GPU probe asks the CLI to exit instead of being swallowed", async () => { |
| 74 | + const dir = mkdtempSync(join(tmpdir(), "hf-launch-")); |
| 75 | + const readyPath = join(dir, "ready"); |
| 76 | + const child = startFixture(join(dir, "profile"), readyPath, "probe"); |
| 77 | + const done = exited(child); |
| 78 | + await waitForTestCondition( |
| 79 | + () => existsSync(readyPath) && readFileSync(readyPath, "utf8") === "probing", |
| 80 | + 30_000, |
| 81 | + ); |
| 82 | + child.kill("SIGTERM"); |
| 83 | + await done; |
| 84 | + expect(readFileSync(`${readyPath}.exit`, "utf8")).toBe("143"); |
| 85 | + }, 60_000); |
| 86 | +}); |
0 commit comments