About: how pages are checked

tests/sims/anycast.test.ts

The automated tests for the /simulators/anycast 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. Converged routes are a stable state of the BGP decision process, valley-free and loop-free.
  2. The result does not depend on timing: synchronous, asynchronous and incremental runs agree.
  3. With every site down, no client has a route.
// /simulators/anycast: BGP route selection under the Gao-Rexford policies.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { converge, convergeAsync, NODES, LINKS, SITES, CLIENTS, OPERATOR_ASN, clientViews, type Config, type Route } from '../../src/lib/sims/anycast.ts';
import { checker, lcg } from './check.ts';

// Relationships rebuilt from the link list: rel[x][y] is what y is to x.
const rel: Record<string, Record<string, 'customer' | 'peer' | 'provider'>> = Object.fromEntries(NODES.map((n) => [n.id, {}]));
for (const l of LINKS) {
	if (l.type === 'peer') { rel[l.a][l.b] = 'peer'; rel[l.b][l.a] = 'peer'; }
	else { rel[l.a][l.b] = 'provider'; rel[l.b][l.a] = 'customer'; }
}
const asn = Object.fromEntries(NODES.map((n) => [n.id, n.asn]));
const rid = Object.fromEntries(NODES.map((n, i) => [n.id, i + 1]));
const LP = { origin: 4, customer: 3, peer: 2, provider: 1 } as const;

function randomConfig(r: () => number): Config {
	return { down: SITES.filter(() => r() < 0.3), prepend: Object.fromEntries(SITES.map((s) => [s, Math.floor(r() * 3)])) };
}

/**
 * A converged state is a fixed point: every node's route is the best of what its neighbours export to it
 * under the export rule (routes learned from customers, or originated, go to everyone; others only to
 * customers), ranked by local preference, then AS path length, then router ID.
 */
function stableViolations(best: Record<string, Route | null>, cfg: Config): string[] {
	const bad: string[] = [];
	for (const n of NODES) {
		if (n.kind === 'site') {
			const want = cfg.down.includes(n.id) ? null : Array(1 + (cfg.prepend[n.id] ?? 0)).fill(OPERATOR_ASN).join();
			const got = best[n.id] ? best[n.id]!.path.join() : null;
			if (want !== got) bad.push(`site ${n.id} announces ${got}, expected ${want}`);
			continue;
		}
		const offers: { via: string; path: number[]; site: string; lp: number }[] = [];
		for (const [m, r] of Object.entries(rel[n.id])) {
			const b = best[m];
			if (!b) continue;
			const exported = b.rel === 'origin' || b.rel === 'customer' || rel[m][n.id] === 'customer';
			if (!exported) continue;
			const path = b.rel === 'origin' ? b.path : [asn[m], ...b.path];
			if (path.includes(n.asn)) continue;
			offers.push({ via: m, path, site: b.site, lp: LP[r] });
		}
		offers.sort((a, b) => b.lp - a.lp || a.path.length - b.path.length || rid[a.via] - rid[b.via]);
		const want = offers[0] ? `${offers[0].via}:${offers[0].path.join()}` : 'none';
		const got = best[n.id] ? `${best[n.id]!.via}:${best[n.id]!.path.join()}` : 'none';
		if (want !== got) bad.push(`${n.id}: picked ${got}, best offer is ${want}`);
	}
	return bad;
}

/** Valley-free: walking from a node to the site, uphill (to providers) links come first, then at most one peer link, then downhill. */
function valleyFree(from: string, hops: string[]): boolean {
	let down = false;
	let prev = from;
	for (const h of hops) {
		const r = rel[prev][h];
		if (r === undefined) return false;
		if (r === 'provider') { if (down) return false; }
		else if (r === 'peer') { if (down) return false; down = true; }
		else down = true;
		prev = h;
	}
	return true;
}

test('converged routes are a stable state of the BGP decision process, valley-free and loop-free', () => {
	const { ok, done } = checker();
	const r = lcg(3);
	for (let it = 0; it < 300; it++) {
		const cfg = randomConfig(r);
		const best = converge(cfg).state.best;
		for (const v of stableViolations(best, cfg)) ok(false, `${JSON.stringify(cfg)}: ${v}`);
		for (const n of NODES) {
			const b = best[n.id];
			if (!b || b.rel === 'origin') continue;
			ok(valleyFree(n.id, b.hops), `${n.id} path ${b.hops.join(' ')} has a valley`);
			ok(!cfg.down.includes(b.site), `${n.id} routes to ${b.site}, which is down`);
			ok(new Set(b.hops).size === b.hops.length, `${n.id} path ${b.hops.join(' ')} loops`);
		}
	}
	done();
});

test('the result does not depend on timing: synchronous, asynchronous and incremental runs agree', () => {
	const { ok, done } = checker();
	const r = lcg(5);
	const key = (x: Route | null) => (x ? x.site + '|' + x.hops.join(',') : 'none');
	for (let it = 0; it < 200; it++) {
		const cfg = randomConfig(r);
		const a = converge(cfg).state.best;
		const b = convergeAsync(cfg, it + 1);
		const c = converge(cfg, converge(randomConfig(r)).state).state.best;
		for (const id of Object.keys(a)) ok(key(a[id]) === key(b[id]) && key(a[id]) === key(c[id]), `${id}: ${key(a[id])} / ${key(b[id])} / ${key(c[id])}`);
	}
	done();
});

test('with every site down, no client has a route', () => {
	const cfg: Config = { down: [...SITES], prepend: {} };
	const st = converge(cfg).state;
	for (const v of clientViews(st, cfg)) assert.equal(v.site, null, v.id);
	assert.ok(CLIENTS.length > 0);
});

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

Published