testSlotsAreTakenAndGivenBack

tcmts.cli.budget.test.testSlotsAreTakenAndGivenBack · #hlp2joby · 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 fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testSlotsAreTakenAndGivenBack = async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm slots "));
  try {
    const pool = slotPool(dir, 2);
    const a = await pool.take("a");
    const b = await pool.take("b");
    assert.deepEqual(fs.readdirSync(dir).sort(), ["slot-0", "slot-1"]);
    // the third waits, and says on what
    const said: SlotWait[] = [];
    const c = pool.take("c", (w) => said.push(w));
    assert.equal(await settlesWithin(c, 400), false);
    assert.deepEqual(
      said[0]!.holders.map((h) => [h.what, h.pid]),
      [
        ["a", process.pid],
        ["b", process.pid],
      ],
    );
    assert.deepEqual([said[0]!.ahead, said[0]!.want, said[0]!.size], [0, 1, 2]);
    // (told each time it looks)
    assert.ok(said.length > 1);
    a();
    assert.equal(await settlesWithin(c, 2000), true);
    b();
    (await c)();
    assert.deepEqual(fs.readdirSync(dir), []);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses