testASlotWhoseHolderIsGoneIsTakenOver

tcmts.cli.budget.test.testASlotWhoseHolderIsGoneIsTakenOver · #eoshynpf · term

import { slotPool } from "#codebase/tcmts/cli/budget";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testASlotWhoseHolderIsGoneIsTakenOver = async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm slots "));
  try {
    // left by a process that's gone (no pid is this high), and a place in line of one
    const gone = JSON.stringify({ pid: 2147483646, what: "gone", since: 0, token: "x" });
    fs.writeFileSync(path.join(dir, "slot-0"), gone);
    fs.writeFileSync(path.join(dir, "line-000000000000000-0000000001-000000"), gone);
    const release = await slotPool(dir, 1).take("me");
    assert.equal(JSON.parse(fs.readFileSync(path.join(dir, "slot-0"), "utf8")).pid, process.pid);
    assert.deepEqual(fs.readdirSync(dir), ["slot-0"]);
    release();
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses