|
| 1 | +import { spawnSync } from "child_process"; |
| 2 | +import { constants } from "os"; |
| 3 | +import { join } from "path"; |
| 4 | +import { |
| 5 | + ISOLATED_BENCHMARK_ENV, |
| 6 | + Isolation, |
| 7 | + PROCESS_ISOLATION_ENV, |
| 8 | +} from "../src/isolation"; |
| 9 | + |
| 10 | +const FIXTURE = join(__dirname, "fixtures", "isolation.cjs"); |
| 11 | +/** Fails a run whose benchmark process got stuck instead of blocking the suite. */ |
| 12 | +const RUN_TIMEOUT_MS = 60_000; |
| 13 | +const SIGNAL_EXIT_CODE_OFFSET = 128; |
| 14 | +const CHILD_EXIT_CODE = 3; |
| 15 | +const RAN_LINE = /^ran (\d+) (\S+) (.+)$/; |
| 16 | +/** Passed to Node to check that children keep the flags of their parent. */ |
| 17 | +const NODE_FLAG = "--no-warnings"; |
| 18 | + |
| 19 | +/** `Isolation.current()` reads the environment once, so load a fresh copy. */ |
| 20 | +function currentIsolation(env: Record<string, string | undefined>): Isolation { |
| 21 | + const saved = Object.fromEntries( |
| 22 | + Object.keys(env).map((key) => [key, process.env[key]]), |
| 23 | + ); |
| 24 | + setEnv(env); |
| 25 | + try { |
| 26 | + let isolation: Isolation | undefined; |
| 27 | + jest.isolateModules(() => { |
| 28 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 29 | + isolation = require("../src/isolation").Isolation.current(); |
| 30 | + }); |
| 31 | + return isolation as unknown as Isolation; |
| 32 | + } finally { |
| 33 | + setEnv(saved); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function setEnv(env: Record<string, string | undefined>): void { |
| 38 | + for (const [key, value] of Object.entries(env)) { |
| 39 | + if (value === undefined) { |
| 40 | + delete process.env[key]; |
| 41 | + } else { |
| 42 | + process.env[key] = value; |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe("Isolation.current", () => { |
| 48 | + it.each([undefined, "1", "true"])( |
| 49 | + "runs one process per benchmark (isolation=%s)", |
| 50 | + (value) => { |
| 51 | + const isolation = currentIsolation({ |
| 52 | + [PROCESS_ISOLATION_ENV]: value, |
| 53 | + [ISOLATED_BENCHMARK_ENV]: undefined, |
| 54 | + }); |
| 55 | + |
| 56 | + expect(isolation.mode).toEqual({ kind: "ProcessPerBenchmark" }); |
| 57 | + expect(isolation.onlyBenchmark()).toBeUndefined(); |
| 58 | + expect(isolation.isChild()).toBe(false); |
| 59 | + expect(isolation.shouldRun("any")).toBe(true); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + it.each(["0", "false"])("shares one process (isolation=%s)", (value) => { |
| 64 | + const isolation = currentIsolation({ |
| 65 | + [PROCESS_ISOLATION_ENV]: value, |
| 66 | + [ISOLATED_BENCHMARK_ENV]: undefined, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(isolation.mode).toEqual({ kind: "SharedProcess" }); |
| 70 | + expect(isolation.isChild()).toBe(false); |
| 71 | + expect(isolation.shouldRun("any")).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it("restricts a child to its benchmark, whatever the isolation setting", () => { |
| 75 | + const isolation = currentIsolation({ |
| 76 | + [PROCESS_ISOLATION_ENV]: "false", |
| 77 | + [ISOLATED_BENCHMARK_ENV]: "file::bench", |
| 78 | + }); |
| 79 | + |
| 80 | + expect(isolation.mode).toEqual({ |
| 81 | + kind: "IsolatedBenchmark", |
| 82 | + uri: "file::bench", |
| 83 | + }); |
| 84 | + expect(isolation.onlyBenchmark()).toBe("file::bench"); |
| 85 | + expect(isolation.isChild()).toBe(true); |
| 86 | + expect(isolation.shouldRun("file::bench")).toBe(true); |
| 87 | + // Exact match only: a prefix is another benchmark. |
| 88 | + expect(isolation.shouldRun("file::bench 2")).toBe(false); |
| 89 | + expect(isolation.shouldRun("file")).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it("rejects any other value", () => { |
| 93 | + expect(() => |
| 94 | + currentIsolation({ |
| 95 | + [PROCESS_ISOLATION_ENV]: "maybe", |
| 96 | + [ISOLATED_BENCHMARK_ENV]: undefined, |
| 97 | + }), |
| 98 | + ).toThrow(`${PROCESS_ISOLATION_ENV} must be \`true\` or \`false\``); |
| 99 | + }); |
| 100 | + |
| 101 | + it("never spawns when no instrument measures", () => { |
| 102 | + expect(Isolation.shared().mode).toEqual({ kind: "SharedProcess" }); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +interface FixtureOptions { |
| 107 | + action?: "exit" | "kill"; |
| 108 | + sync?: boolean; |
| 109 | +} |
| 110 | + |
| 111 | +function runFixture({ action, sync }: FixtureOptions = {}) { |
| 112 | + const env: NodeJS.ProcessEnv = { |
| 113 | + ...process.env, |
| 114 | + ISOLATION_FIXTURE_ACTION: action, |
| 115 | + }; |
| 116 | + delete env[PROCESS_ISOLATION_ENV]; |
| 117 | + delete env[ISOLATED_BENCHMARK_ENV]; |
| 118 | + if (sync) { |
| 119 | + env.ISOLATION_FIXTURE_SYNC = "1"; |
| 120 | + } |
| 121 | + const result = spawnSync(process.execPath, [NODE_FLAG, FIXTURE], { |
| 122 | + env, |
| 123 | + encoding: "utf8", |
| 124 | + timeout: RUN_TIMEOUT_MS, |
| 125 | + }); |
| 126 | + const ran = result.stdout |
| 127 | + .split("\n") |
| 128 | + .map((line) => RAN_LINE.exec(line)) |
| 129 | + .filter((match): match is RegExpExecArray => match !== null); |
| 130 | + return { status: result.status, ran }; |
| 131 | +} |
| 132 | + |
| 133 | +describe.each([ |
| 134 | + { name: "async", sync: false }, |
| 135 | + { name: "sync", sync: true }, |
| 136 | +])("Isolation child process ($name run)", ({ sync }) => { |
| 137 | + it("runs each benchmark in a fresh process with the flags of its parent", () => { |
| 138 | + const { status, ran } = runFixture({ sync }); |
| 139 | + |
| 140 | + expect(status).toBe(0); |
| 141 | + expect(ran.map((match) => match[2])).toEqual(["a", "b"]); |
| 142 | + expect(new Set(ran.map((match) => match[1])).size).toBe(2); |
| 143 | + for (const match of ran) { |
| 144 | + expect(JSON.parse(match[3])).toEqual([NODE_FLAG]); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + it("exits with the exit code of a failing child", () => { |
| 149 | + const { status, ran } = runFixture({ sync, action: "exit" }); |
| 150 | + |
| 151 | + expect(status).toBe(CHILD_EXIT_CODE); |
| 152 | + // The parent stops at the first failure. |
| 153 | + expect(ran.map((match) => match[2])).toEqual(["a"]); |
| 154 | + }); |
| 155 | + |
| 156 | + it("exits with 128 + N when a signal N killed the child", () => { |
| 157 | + const { status, ran } = runFixture({ sync, action: "kill" }); |
| 158 | + |
| 159 | + expect(status).toBe(SIGNAL_EXIT_CODE_OFFSET + constants.signals.SIGKILL); |
| 160 | + expect(ran.map((match) => match[2])).toEqual(["a"]); |
| 161 | + }); |
| 162 | +}); |
0 commit comments