Menu

1 / 2

Build a deterministic network simulator in TypeScript

DEV Community: node·Adrian Turbiński·14 days ago
#D1kTGHE6
#devtonodejs#devto#dev#seed#timer
Reading 0:00
15s threshold

Within my collaborative diagram editor in coschema the network simulator was the most useful component. Not due to an effective network simulation but because on failure it would print a seed and replay exactly. The last full run had 1,756,564 messages across 5,000 seeds with 0 failures.

In this tutorial we will implement a small example similar to this in TypeScript. We will create a virtual clock, a seeded random number generator, a link with latency, jitter, loss and duplication and a runner that finds a failing seed and replays it. We assume basic knowledge of TypeScript and Node 24.

The complete project, with tests and configuration, is in the virtual-network-test-harness repository . The page explains the parts that matter, the repository holds every file.

Seeded randomness you can fork

A flaky network test usually has two causes: Math.random and real timers. I replace both. Randomness comes first, because everything else draws from it.

The generator in src/prng.ts is a small 32 bit mixer.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.