About: how pages are checked

tests/sims/tcpudp.test.ts

The automated tests for the /simulators/tcp-vs-udp 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 comparison is like for like: a message is lost over UDP exactly when its first TCP transmission is lost, with the same network delay otherwise.
  2. TCP delivers every message, in order, and a message waits only for an earlier one that has not arrived yet.
  3. Each TCP retry waits twice as long as the one before (RFC 6298 backoff), starting from an RTO of at least 200 ms as on Linux.
  4. Over 20,000 messages the share lost is within five standard deviations of the loss rate set.
// /simulators/tcp-vs-udp: the same messages over the same lossy network, as UDP datagrams and over TCP.
import { test } from 'node:test';
import { simulate, summarize, type Settings } from '../../src/lib/sims/tcpUdp.ts';
import { checker, lcg } from './check.ts';

const r = lcg(21);
const settings: Settings[] = Array.from({ length: 200 }, (_, k) => ({ count: 200 + Math.floor(r() * 300), interval: [5, 20, 50][k % 3], rtt: [20, 80, 200][Math.floor(r() * 3)], jitter: [0, 10, 40][Math.floor(r() * 3)], loss: [0, 0.01, 0.05, 0.2][Math.floor(r() * 4)], seed: k + 1 }));

test('the comparison is like for like: a message is lost over UDP exactly when its first TCP transmission is lost, with the same network delay otherwise', () => {
	const { ok, done } = checker();
	for (const s of settings) {
		const o = simulate(s);
		for (const m of o.msgs) {
			ok((m.udpArrive === null) === m.tcpTries > 1, `seed ${s.seed} message ${m.i}: UDP lost ${m.udpArrive === null}, TCP tries ${m.tcpTries}`);
			if (m.udpArrive !== null) ok(Math.abs(m.tcpArrive - m.udpArrive - (Math.max(m.sent, o.handshake) - m.sent)) < 1e-9, `seed ${s.seed} message ${m.i}: different delays`);
		}
	}
	done();
});

test('TCP delivers every message, in order, and a message waits only for an earlier one that has not arrived yet', () => {
	const { ok, done } = checker();
	for (const s of settings) {
		const o = simulate(s);
		let prev = -Infinity;
		for (const m of o.msgs) {
			ok(m.tcpDeliver >= prev && m.tcpDeliver >= m.tcpArrive, `seed ${s.seed} message ${m.i} delivered out of order`);
			ok(m.blocked === m.tcpDeliver - m.tcpArrive && (m.blocked === 0 || m.tcpDeliver === prev), `seed ${s.seed} message ${m.i}: blocked time wrong`);
			prev = m.tcpDeliver;
		}
		const t = summarize(o, 'tcp', null), u = summarize(o, 'udp', null);
		ok(t.delivered === s.count && t.lost === 0, `seed ${s.seed}: TCP lost messages`);
		ok(u.delivered + u.lost === s.count && t.retransmits === o.msgs.reduce((a, m) => a + m.tcpTries - 1, 0), `seed ${s.seed}: summary counts`);
	}
	done();
});

test('each TCP retry waits twice as long as the one before (RFC 6298 backoff), starting from an RTO of at least 200 ms as on Linux', () => {
	const { ok, done } = checker();
	for (const s of settings) {
		const o = simulate(s);
		ok(o.rto >= 200, `seed ${s.seed}: RTO ${o.rto}`);
		for (const m of o.msgs) {
			if (m.tcpTries > 8) continue; // the 60 s cap would apply
			const lastSend = Math.max(m.sent, o.handshake) + o.rto * (2 ** (m.tcpTries - 1) - 1);
			const delay = m.tcpArrive - lastSend;
			ok(delay >= s.rtt / 2 - 1e-9 && delay <= s.rtt / 2 + s.jitter + 1e-9, `seed ${s.seed} message ${m.i}: try ${m.tcpTries} implies a delay of ${delay}`);
		}
	}
	done();
});

test('over 20,000 messages the share lost is within five standard deviations of the loss rate set', () => {
	const { ok, done } = checker();
	for (const loss of [0.01, 0.05, 0.2]) {
		const u = summarize(simulate({ count: 20000, interval: 1, rtt: 50, jitter: 10, loss, seed: 9 }), 'udp', null);
		const sd = Math.sqrt((loss * (1 - loss)) / 20000);
		ok(Math.abs(u.lost / 20000 - loss) < 5 * sd, `loss ${loss}: ${u.lost} lost`);
	}
	done();
});

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

Published