79 lines · 4.4 KB · TypeScriptRaw
import { spawn, spawnSync } from "node:child_process";import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { describe, expect, it } from "vitest";const root = join(__dirname, "../..");const script = join(root, "scripts/ci/with-cargo-lock.sh");function run(fake: string, command = 'printf "%s" "$RUSTC_WRAPPER"', extra = {}) { const dir = mkdtempSync(join(tmpdir(), "cache-fallback-")); try { writeFileSync(join(dir, "sccache"), `#!/bin/bash\n${fake}\n`, { mode: 0o755 }); const start = Date.now(); const result = spawnSync("bash", [script, "bash", "-c", command], { encoding: "utf8", timeout: 15000, input: "lane input\n", env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, RUSTC_WRAPPER: "sccache", COTTON_CARGO_LOCK: join(dir, "lock"), ...extra }, }); return { ...result, elapsed: Date.now() - start }; } finally { rmSync(dir, { recursive: true, force: true }); }}describe("cache fallback under the cargo lock", () => { /// RULE: A cache that never answers must not hold the build queue; probe it for at most five seconds and compile uncached. it("bounds a stuck probe and still runs the command", () => { const r = run('if [ "$1" = --show-stats ]; then exec sleep 60; fi'); expect(r.status, r.stderr).toBe(0); expect(r.elapsed).toBeGreaterThanOrEqual(4900); expect(r.elapsed).toBeLessThan(8000); expect(r.stdout).toBe(""); expect(r.stderr).toContain("sccache not answering; building without it"); }, 15000); /// RULE: A responsive cache stays enabled; caching must not be disabled merely because a probe is required. it("keeps the wrapper when stats answer", () => { expect(run('echo "Compile requests 10"').stdout).toBe("sccache"); }); it("passes stdin through the supervisor", () => { const r = run('echo "Compile requests 10"', 'read line; printf "%s" "$line"'); expect(r.status, r.stderr).toBe(0); expect(r.stdout).toBe("lane input"); }); it("rejects failed or explicitly nonaccepting stats", () => { expect(run('exit 1').stdout).toBe(""); expect(run('echo "Server is not accepting requests"').stdout).toBe(""); }); /// RULE: Only this command's idle wrapper tree may be stopped, and its uncached retry is final, so another lane's cache and work survive. it("retries an idle wrapper once and preserves the final exit code", () => { const r = run('if [ "$1" = --show-stats ]; then echo "Compile requests 10"; else while :; do sleep 1; done; fi', 'echo "attempt:$RUSTC_WRAPPER"; if [ -n "$RUSTC_WRAPPER" ]; then sccache /fake/rustc; else exit 23; fi', { COTTON_SCCACHE_IDLE_S: "1", COTTON_SCCACHE_POLL_S: "0.1" }); expect(r.status, r.stderr).toBe(23); expect(r.stdout.trim().split("\n")).toEqual(["attempt:sccache", "attempt:"]); expect(r.stderr).toContain("building without it"); }); it("leaves a busy wrapper enabled past the idle deadline", () => { const r = run('if [ "$1" = --show-stats ]; then echo "Compile requests 10"; else end=$((SECONDS+2)); while [ "$SECONDS" -lt "$end" ]; do :; done; fi', 'echo "attempt:$RUSTC_WRAPPER"; sccache /fake/rustc', { COTTON_SCCACHE_IDLE_S: "0.4", COTTON_SCCACHE_POLL_S: "0.1" }); expect(r.status, r.stderr).toBe(0); expect(r.stdout.trim()).toBe("attempt:sccache"); }); it("leaves an unrelated idle wrapper alive during recovery", () => { const unrelated = spawn("bash", ["-c", 'exec -a "sccache /unrelated/rustc" sleep 30'], { stdio: "ignore" }); try { const r = run('if [ "$1" = --show-stats ]; then echo "Compile requests 10"; else exec -a "sccache /fake/rustc" sleep 30; fi', 'if [ -n "$RUSTC_WRAPPER" ]; then sccache /fake/rustc; else echo recovered; fi', { COTTON_SCCACHE_IDLE_S: "0.4", COTTON_SCCACHE_POLL_S: "0.1" }); expect(r.status, r.stderr).toBe(0); expect(r.stdout.trim()).toBe("recovered"); expect(() => process.kill(unrelated.pid!, 0)).not.toThrow(); } finally { unrelated.kill("SIGKILL"); } }); /// RULE: Cache recovery never signals a shared server or a process selected by name; only started PIDs and verified descendants belong to the lane. it("has no process-name kill path", () => { for (const file of [script, join(root, "scripts/ci/sccache-up.sh")]) { const text = readFileSync(file, "utf8"); expect(text).not.toMatch(/\b(?:pkill|pgrep|killall)\b/); expect(text).not.toContain("--stop-server"); } });});