testSlotsAreServedInTurnAndSeveralTogether

tcmts.cli.budget.test.testSlotsAreServedInTurnAndSeveralTogether · #dfwyl2ih · term

import { slotPool } 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 testSlotsAreServedInTurnAndSeveralTogether = async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm slots "));
  try {
    const pool = slotPool(dir, 2);
    const a = await pool.take("a");
    // b wants both; c, after it, wants one: one is free, but b is first
    const b = pool.take("b", undefined, 2);
    await new Promise((r) => setTimeout(r, 50));
    const c = pool.take("c");
    assert.equal(await settlesWithin(c, 400), false);
    assert.equal(await settlesWithin(b, 0), false);
    a();
    assert.equal(await settlesWithin(b, 2000), true);
    assert.equal(await settlesWithin(c, 300), false);
    (await b)();
    assert.equal(await settlesWithin(c, 2000), true);
    (await c)();
    // more than there are is all of them
    const d = pool.take("d", undefined, 5);
    assert.equal(await settlesWithin(d, 2000), true);
    (await d)();
    assert.deepEqual(fs.readdirSync(dir), []);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses