About: how pages are checked

tests/sims/acid.test.ts

The automated tests for the /simulators/acid 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. The balances after each scenario, worked out by hand, with each safeguard on and off.
  2. With the log on, a changed page never reaches disk before the log record describing the change is on disk (the write-ahead rule).
  3. Each scenario's own verdict agrees with the hand-worked result: safe with the safeguard on, broken with it off.
// /simulators/acid: write-ahead logging, constraints, locking and commit flushing, one property at a time.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Engine, SCENARIOS, DEFAULT_OPTS, type Opts } from '../../src/lib/sims/acid.ts';

const runScenario = (id: string, over: Partial<Opts>) => {
	const e = new Engine(SCENARIOS.find((s) => s.id === id)!, { ...DEFAULT_OPTS, ...over });
	let guard = 0;
	while (!e.done && guard++ < 100) e.step();
	return e;
};

test('the balances after each scenario, worked out by hand, with each safeguard on and off', () => {
	// Atomicity: A is debited 30 and that page reaches disk before the crash. With a log, recovery undoes it.
	assert.deepEqual(runScenario('A', { wal: true }).disk, { A: 100, B: 100 });
	assert.deepEqual(runScenario('A', { wal: false }).disk, { A: 70, B: 100 });
	// Consistency: moving 130 out of 100. The CHECK constraint refuses it; without one, A goes to -30.
	const c1 = runScenario('C', { constraint: true }), c0 = runScenario('C', { constraint: false });
	assert.deepEqual([c1.value('A'), c1.value('B')], [100, 100]);
	assert.deepEqual([c0.value('A'), c0.value('B')], [-30, 230]);
	// Isolation: deposits of 50 and 20 to 100. With locks the second waits and reads 150; without, it overwrites with 120.
	assert.equal(runScenario('I', { locking: true }).value('A'), 170);
	assert.equal(runScenario('I', { locking: false }).value('A'), 120);
	// Durability: commit, then crash before any page is written. Flushing the log at commit lets redo restore it.
	assert.deepEqual(runScenario('D', { fsync: true }).disk, { A: 70, B: 130 });
	assert.deepEqual(runScenario('D', { fsync: false }).disk, { A: 100, B: 100 });
});

test('with the log on, a changed page never reaches disk before the log record describing the change is on disk (the write-ahead rule)', () => {
	for (const sc of SCENARIOS) for (const fsync of [true, false]) {
		const e = new Engine(sc, { ...DEFAULT_OPTS, fsync });
		let guard = 0;
		while (!e.done && guard++ < 100) {
			e.step();
			if (e.crashed) continue;
			for (const k of ['A', 'B'] as const) {
				if (e.disk[k] === sc.start[k]) continue;
				assert.ok(e.wal.some((r) => r.kind === 'update' && r.key === k && r.after === e.disk[k] && r.durable), `${sc.id}, fsync ${fsync}: ${k} = ${e.disk[k]} on disk with no durable log record for it`);
			}
		}
	}
});

test("each scenario's own verdict agrees with the hand-worked result: safe with the safeguard on, broken with it off", () => {
	for (const sc of SCENARIOS) {
		assert.equal(sc.expect(runScenario(sc.id, { [sc.toggle]: true })).ok, true, `${sc.id} with ${sc.toggle}`);
		assert.equal(sc.expect(runScenario(sc.id, { [sc.toggle]: false })).ok, false, `${sc.id} without ${sc.toggle}`);
	}
});

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

Published