About: how pages are checked

tests/sims/distlock.test.ts

The automated tests for the /simulators/distributed-lock 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. When a pause or network delay outlasts the 5 s lease, a stale write is accepted unless the storage checks fencing tokens.
  2. A clock jump on a Redlock node lets two clients hold the lock at once.
// /simulators/distributed-lock: lock leases, process pauses, clock jumps and fencing tokens.
import { test } from 'node:test';
import { run, SERVICES, SCENARIOS } from '../../src/lib/sims/distLock.ts';
import { checker } from './check.ts';

test('when a pause or network delay outlasts the 5 s lease, a stale write is accepted unless the storage checks fencing tokens', () => {
	const { ok, done } = checker();
	for (const s of SERVICES) for (const sc of SCENARIOS) for (const fencing of [true, false]) for (const pause of [500, 3000, 12000]) {
		if (sc.redlockOnly && s.id !== 'redlock') continue;
		if (sc.id === 'clockjump') continue;
		const o = run(s.id, sc.id, { ttl: 5000, pause, fencing });
		const fenced = fencing && s.fencing;
		const expired = (sc.id === 'pause' || sc.id === 'delay') && 200 + pause > 5000;
		const tag = `${s.id} ${sc.id} pause ${pause} ${fencing ? 'with' : 'without'} fencing`;
		if (!expired) ok(!o.staleAccepted && o.overlapMs === 0, `${tag}: unsafe although the lease never expired`);
		else {
			ok(o.staleAccepted === !fenced, `${tag}: stale write accepted ${o.staleAccepted}`);
			ok(o.finalValue === (fenced ? 'from B' : 'from A'), `${tag}: final value ${o.finalValue}`);
		}
	}
	done();
});

test('a clock jump on a Redlock node lets two clients hold the lock at once', () => {
	const { ok, done } = checker();
	for (const fencing of [true, false]) for (const pause of [500, 3000]) {
		const o = run('redlock', 'clockjump', { ttl: 5000, pause, fencing });
		ok(o.overlapMs > 0, `fencing ${fencing}, pause ${pause}: no overlap`);
	}
	done();
});

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

Published