About: how pages are checked

tests/sims/splitbrain.test.ts

The automated tests for the /simulators/split-brain 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. Majority quorum: a 2–3 split elects one leader and loses nothing.
  2. An even 2–2 split elects no leader, and a witness breaks the tie.
  3. Without a quorum both sides lead, and the old side’s acknowledged writes are lost on heal.
  4. Quorum size is ⌊N/2⌋ + 1 and tolerates ⌊(N − 1)/2⌋ failures.
// /simulators/split-brain
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { run, quorum, tolerates, type Setup } from '../../src/lib/sims/splitbrain.ts';

const base: Setup = { sides: [0, 0, 1, 1, 1], witness: -1, leader: 0, mode: 'majority', seconds: 10, election: 1000, rate: 10, keys: 20, skew: 0 };

test('majority quorum: a 2–3 split elects one leader and loses nothing', () => {
	const r = run(base);
	assert.equal(r.leaders, 1);
	assert.equal(r.lost, 0);
});

test('an even 2–2 split elects no leader, and a witness breaks the tie', () => {
	assert.equal(run({ ...base, sides: [0, 0, 1, 1] }).leaders, 0);
	assert.equal(run({ ...base, sides: [0, 0, 1, 1], witness: 1 }).leaders, 1);
});

test('without a quorum both sides lead, and the old side’s acknowledged writes are lost on heal', () => {
	const r = run({ ...base, mode: 'any' });
	assert.equal(r.leaders, 2);
	assert.equal(r.lost, r.sides[0].acked);
});

test('quorum size is ⌊N/2⌋ + 1 and tolerates ⌊(N − 1)/2⌋ failures', () => {
	for (let n = 1; n <= 9; n++) {
		assert.equal(quorum(n), Math.floor(n / 2) + 1);
		assert.equal(tolerates(n), Math.floor((n - 1) / 2));
	}
});

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

Published