About: how pages are checked

tests/sims/deadlock.test.ts

The automated tests for the /simulators/mutex-deadlock 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.

  1. Schedule and deadlock counts match walking every interleaving one by one.
  2. Each example deadlock replays step by step into a state where the wait-for graph has a cycle.
  3. Lock ordering and the fixed dining philosophers never deadlock; opposite order, three threads and naive dining can.
  4. The exact chance a random scheduler deadlocks is within 1 point of 40,000 simulated runs.
// /simulators/mutex-deadlock: every interleaving of the example programs, and the chance a random scheduler deadlocks.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { PROGRAMS, explore, initial, canRun, step, isDeadlock, waitFor, hasCycle, done, type Program } from '../../src/lib/sims/deadlock.ts';
import { checker, lcg } from './check.ts';

// walk every schedule without memoising states, so it shares nothing with the model's search
function brute(p: Program) {
	let all = 0, dl = 0;
	const rec = (s: ReturnType<typeof initial>) => {
		const ts = p.threads.map((_, t) => t).filter((t) => canRun(p, s, t));
		if (!ts.length) { all++; if (!p.threads.every((_, t) => done(p, s, t))) dl++; return; }
		for (const t of ts) rec(step(p, s, t).s);
	};
	rec(initial(p));
	return { all, dl };
}

test('schedule and deadlock counts match walking every interleaving one by one', () => {
	const { ok, done: finish } = checker();
	for (const p of PROGRAMS) {
		const e = explore(p);
		if (e.cyclic || p.threads.length > 3) continue;
		const b = brute(p);
		ok(b.all === e.schedules && b.dl === e.deadlockSchedules, `${p.id}: ${b.all} schedules / ${b.dl} deadlocks by brute force, model says ${e.schedules} / ${e.deadlockSchedules}`);
	}
	finish();
});

test('each example deadlock replays step by step into a state where the wait-for graph has a cycle', () => {
	const { ok, done: finish } = checker();
	for (const p of PROGRAMS) {
		const e = explore(p);
		if (!e.example) continue;
		let s = initial(p);
		for (const t of e.example) { ok(canRun(p, s, t), `${p.id}: thread ${t} cannot run`); s = step(p, s, t).s; }
		ok(isDeadlock(p, s) && hasCycle(p.threads.length, waitFor(p, s)), `${p.id}: example does not end in a deadlock with a cycle`);
	}
	finish();
});

test('lock ordering and the fixed dining philosophers never deadlock; opposite order, three threads and naive dining can', () => {
	const get = (id: string) => explore(PROGRAMS.find((p) => p.id === id)!);
	assert.equal(get('ordered').deadlockStates, 0);
	assert.equal(get('dining-fixed').deadlockStates, 0);
	for (const id of ['opposite', 'three', 'dining']) assert.ok(get(id).deadlockStates > 0, id);
	assert.equal(get('trylock').deadlockStates, 0);
	assert.ok(get('trylock').cyclic, 'trylock can loop forever (livelock) without deadlocking');
});

test('the exact chance a random scheduler deadlocks is within 1 point of 40,000 simulated runs', () => {
	const r = lcg(5);
	for (const id of ['opposite', 'three', 'dining']) {
		const p = PROGRAMS.find((x) => x.id === id)!;
		const e = explore(p);
		let d = 0;
		const N = 40000;
		for (let i = 0; i < N; i++) {
			let s = initial(p);
			for (;;) {
				const ts = p.threads.map((_, t) => t).filter((t) => canRun(p, s, t));
				if (!ts.length) { if (isDeadlock(p, s)) d++; break; }
				s = step(p, s, ts[Math.floor(r() * ts.length)]).s;
			}
		}
		assert.ok(Math.abs(d / N - e.randomDeadlock!) <= 0.01, `${id}: exact ${e.randomDeadlock}, sampled ${d / N}`);
	}
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published