tests/sims/transport.test.ts
The automated tests for the /simulators/http2-streams and /simulators/http3-quic simulators. 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.
- Every object finishes, and no protocol sends faster than the link allows, across 72 combinations of page, latency, bandwidth, loss and migration.
- A lost packet holds up other HTTP/2 streams on the same TCP connection, but never other streams in HTTP/3.
- With no loss, HTTP/3 finishes one round trip sooner than HTTP/2, because QUIC sets up transport and TLS 1.3 in one round trip instead of TCP's extra one.
// /simulators/http2-streams, /simulators/http3-quic: HTTP/1.1, HTTP/2 over TCP and HTTP/3 over QUIC under latency and loss.
import { test } from 'node:test';
import { simulate, PAGES, PKT, type Proto } from '../../src/lib/sims/transport.ts';
import { checker } from './check.ts';
const PROTOS: Proto[] = ['h1', 'h2', 'h3'];
const grid = PAGES.flatMap((pg) => [20, 100].flatMap((rtt) => [5, 50].flatMap((mbps) => [0, 0.01, 0.03].flatMap((loss) => [null, 400].map((mig) => ({ pg, rtt, mbps, loss, mig }))))));
const runOf = (p: Proto, c: (typeof grid)[number]) => simulate(p, { objects: c.pg.objects, rttMs: c.rtt, mbps: c.mbps, loss: c.loss, zeroRtt: false, seed: 7, migrateAt: c.mig, sequential: c.rtt === 20 });
test('every object finishes, and no protocol sends faster than the link allows, across 72 combinations of page, latency, bandwidth, loss and migration', () => {
const { ok, done } = checker();
for (const c of grid) for (const p of PROTOS) {
const r = runOf(p, c), tag = `${c.pg.id} ${p} ${c.rtt} ms ${c.mbps} Mbps loss ${c.loss}${c.mig ? ' migrating' : ''}`;
ok(r.streams.every((s) => s.done > 0), `${tag}: an object never finished`);
ok(r.maxRate <= Math.ceil((c.mbps * 1e6) / 8 / PKT / 1000) + 1, `${tag}: sent ${r.maxRate} packets in a millisecond`);
}
done();
});
test('a lost packet holds up other HTTP/2 streams on the same TCP connection, but never other streams in HTTP/3', () => {
const { ok, done } = checker();
for (const c of grid) {
ok(runOf('h3', c).crossWaitTotal === 0, `${c.pg.id} loss ${c.loss}: an HTTP/3 stream waited on another stream's loss`);
if (c.loss === 0 && c.mig === null) for (const p of PROTOS) {
const r = runOf(p, c);
ok(r.retransmits === 0 && r.crossWaitTotal === 0, `${p} with no loss retransmitted or waited`);
}
}
done();
});
test("with no loss, HTTP/3 finishes one round trip sooner than HTTP/2, because QUIC sets up transport and TLS 1.3 in one round trip instead of TCP's extra one", () => {
const { ok, done } = checker();
for (const c of grid) {
if (c.loss !== 0 || c.mig !== null) continue;
const d = runOf('h2', c).done - runOf('h3', c).done;
ok(Math.abs(d - c.rtt) <= 2, `${c.pg.id} ${c.rtt} ms: HTTP/3 ahead by ${d} ms`);
}
done();
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.