testSlotsHeldByAnotherProcessCount

tcmts.cli.budget.test.testSlotsHeldByAnotherProcessCount · #lg7xd6o2 · term

import { slotPool } from "#codebase/tcmts/cli/budget";
import type { SlotWait } from "#codebase/tcmts/cli/budget";
import { settlesWithin } from "#codebase/tcmts/cli/budget/test";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testSlotsHeldByAnotherProcessCount = async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm slots "));
  const other = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });
  try {
    // another process holds both: this one waits until it's gone
    for (const i of [0, 1])
      fs.writeFileSync(
        path.join(dir, `slot-${i}`),
        JSON.stringify({ pid: other.pid, what: "tcm test of elsewhere", since: 0, token: `t${i}` }),
      );
    const said: SlotWait[] = [];
    const mine = slotPool(dir, 2).take("me", (w) => said.push(w));
    assert.equal(await settlesWithin(mine, 400), false);
    assert.deepEqual(
      said[0]!.holders.map((h) => [h.what, h.pid]),
      [
        ["tcm test of elsewhere", other.pid],
        ["tcm test of elsewhere", other.pid],
      ],
    );
    other.kill("SIGKILL");
    assert.equal(await settlesWithin(mine, 3000), true);
    (await mine)();
  } finally {
    other.kill("SIGKILL");
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses