tests/sims/breaker.test.ts
The automated tests for the /simulators/circuit-breaker simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- Every request is counted exactly once: succeeded, failed, timed out, rejected by the open breaker or turned away busy.
- The breaker only moves closed to open, open to half-open, and half-open to open or closed.
- With no outage the breaker never opens, and during an outage it keeps most requests away from the failing service.
// /simulators/circuit-breaker: a service under a 20-second outage, with and without a circuit breaker.
import { test } from 'node:test';
import { simulate, DEMO_BREAKER, type Cfg } from '../../src/lib/sims/breaker.ts';
import { checker } from './check.ts';
const base: Cfg = { rate: 50, durationMs: 60000, workers: 20, timeoutMs: 2000, outageStart: 15000, outageEnd: 35000, mode: 'slow', breaker: null, seed: 1 };
const runs = (['errors', 'slow'] as const).flatMap((mode) => [null, DEMO_BREAKER].flatMap((breaker) => [1, 2, 3].map((seed) => ({ mode, breaker, seed, r: simulate({ ...base, mode, breaker, seed }) }))));
test('every request is counted exactly once: succeeded, failed, timed out, rejected by the open breaker or turned away busy', () => {
const { ok, done } = checker();
for (const { mode, breaker, seed, r } of runs) {
const t = r.totals;
ok(t.ok + t.error + t.timeout + t.open + t.busy === t.requests, `${mode} ${breaker ? 'breaker' : 'none'} seed ${seed}: outcomes do not add up to requests`);
ok(t.reached === t.ok + t.error + t.timeout, `${mode} seed ${seed}: requests that reached the service do not add up`);
}
done();
});
test('the breaker only moves closed to open, open to half-open, and half-open to open or closed', () => {
const { ok, done } = checker();
const legal = new Set(['CLOSED>OPEN', 'OPEN>HALF_OPEN', 'HALF_OPEN>OPEN', 'HALF_OPEN>CLOSED']);
for (const { mode, seed, r } of runs) {
let prev = 'CLOSED';
for (const x of r.transitions) { ok(legal.has(`${x.from}>${x.to}`) && x.from === prev, `${mode} seed ${seed}: ${x.from} -> ${x.to} after ${prev}`); prev = x.to; }
}
done();
});
test('with no outage the breaker never opens, and during an outage it keeps most requests away from the failing service', () => {
const { ok, done } = checker();
const healthy = simulate({ ...base, outageStart: 1e9, outageEnd: 1e9, breaker: DEMO_BREAKER });
ok(healthy.transitions.length === 0, 'breaker opened while the service was healthy');
for (const mode of ['errors', 'slow'] as const) {
const without = simulate({ ...base, mode }), withBr = simulate({ ...base, mode, breaker: DEMO_BREAKER });
ok(withBr.totals.reachedDuringOutage < without.totals.reachedDuringOutage / 4, `${mode}: ${withBr.totals.reachedDuringOutage} requests reached the service with a breaker, ${without.totals.reachedDuringOutage} without`);
}
done();
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.