Cotton code
50 lines · 2.4 KB · TypeScriptRaw
// RULE: On Linux (the box) the build lock passes every caller straight through, unlocked and unlogged, and only a release (the Play cut) takes the lock: the box's cgroup weights share its CPU, and a file lock only queued its lanes (15 Nov 2026).import { spawnSync } from "node:child_process";import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { afterEach, expect, it } from "vitest";const SCRIPT = resolve(__dirname, "../with-build-lock.sh");let scratch = "";afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = "";});function run(cls: string, kind: string, command: string[]) {  scratch ||= mkdtempSync(join(tmpdir(), "build-lock-linux-"));  const lock = join(scratch, "lock");  const result = spawnSync("bash", [SCRIPT, ...command], {    encoding: "utf8",    env: {      ...process.env, HOME: join(scratch, "home"), BUILD_LOCK_HOST_KIND: kind, BUILD_LOCK_CLASS: cls,      BUILD_LOCK_DIR: lock, BUILD_LOCK_LOG: join(scratch, "wait.log"), BUILD_LOCK_TIMEOUT_SECONDS: "2",      BUILD_LOCK_MEMORY_BYTES: "134217728000", TINMEN_BUILD_LOCK_HELD: "", TINMEN_BUILD_LOCK_DIR: "",    },  });  return { ...result, lock };}it("runs a ci or local caller straight through on Linux, even past a held lock, and keeps its exit", () => {  scratch = mkdtempSync(join(tmpdir(), "build-lock-linux-"));  // A live holder in both slots: a Mac's caller would wait on it.  for (const slot of ["lock", "lock.slot2"]) {    mkdirSync(join(scratch, slot), { recursive: true });    writeFileSync(join(scratch, slot, "pid"), `${process.pid}\n`);  }  for (const cls of ["ci", "local", "walk"]) {    const ran = run(cls, "linux", ["bash", "-c", "echo ran-$0; exit 7", cls]);    expect(ran.status, ran.stderr).toBe(7);    expect(ran.stdout).toBe(`ran-${cls}\n`);  }  expect(existsSync(join(scratch, "wait.log"))).toBe(false);});it("still locks a release on Linux, and every class on a Mac", () => {  const release = run("release", "linux", ["bash", "-c", "test -d \"$TINMEN_BUILD_LOCK_DIR\" && echo held"]);  expect(release.status, release.stderr).toBe(0);  expect(release.stdout).toContain("held");  const mac = run("local", "mac", ["bash", "-c", "test -n \"$TINMEN_BUILD_LOCK_HELD\" && echo held"]);  expect(mac.stdout).toContain("held");});