testWorkWaitsForASlotAndGivesItBack

tcmts.cli.budget.test.testWorkWaitsForASlotAndGivesItBack · #45ghw7cm · term

Work in a slot waits for one, says so, and gives it back when it ends, however it ends.

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

export const testWorkWaitsForASlotAndGivesItBack = async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm slots "));
  try {
    const pool = slotPool(dir, 1);
    let finish!: () => void;
    const first = withSlot(
      "checks of /x/codebase on session/a",
      () => new Promise<void>((r) => (finish = r)),
      {
        pool,
        say: () => {},
      },
    );
    await new Promise((r) => setTimeout(r, 50));
    const said: string[] = [];
    let ran = false;
    const second = withSlot(
      "app build of /x/codebase on session/b",
      async () => {
        ran = true;
      },
      { pool, say: (l) => said.push(l) },
    );
    assert.equal(await settlesWithin(second, 400), false);
    assert.equal(ran, false);
    assert.match(
      said[0]!,
      /^waiting for a slot: 1 of 1 in use \(1 by this run\) \(TCM_SLOTS sets how many\)$/,
    );
    finish();
    await first;
    assert.equal(await settlesWithin(second, 2000), true);
    assert.equal(ran, true);
    assert.match(said.at(-1)!, /^got a slot after /);
    // (given back when the work throws, too)
    await assert.rejects(
      withSlot("verify of /x/codebase", () => Promise.reject(new Error("no")), {
        pool,
        say: () => {},
      }),
    );
    assert.deepEqual(fs.readdirSync(dir), []);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses