moonpool ~
GitHub →
$ techniques

One thread, one clock, one seed

Run a seed two times. You get the same run, event for event.

The problem

A distributed system fails on a rare order of events: a message that comes late, a crash between two writes. On real machines, the scheduler, the network and the clock make this order. You cannot make it again. A test that failed one time and then passes tells you nothing.

The technique

moonpool runs all the processes of a test on one thread, with one executor. The network, the disk and the clock are simulated. One stream of random numbers makes each choice: the order of the tasks, the latency of each message, each fault. A seed starts this stream. Thus one seed gives one run.

A sleep does not wait. When all the tasks wait, the clock jumps to the next event. Seconds of simulated time take milliseconds of real time.

The live run

The left run and the right run use the same seed. Your browser runs both, and each event on the right is the same as the event on the left at the same step. Give the right run another seed: the first event that is not the same gets the ≠ mark. The arrow keys step the two runs together.

The code

The small system of this page is also an example simulation of the repository (cargo xtask sim run register). check_determinism() runs each seed two times and fails the seed when the two runs do not make the same random draws:

fn main() {
    moonpool_sim::init_sim_tracing(tracing::Level::WARN);
    let report = simulation(&RunConfig::default())
        .check_determinism()
        .run()
        .expect("simulation configuration is valid");
    report.eprint();
    std::process::exit(report.exit_code());
}

In a test, set_debug_seeds(vec![seed]) and set_iterations(1) replay one seed alone.

In the Guide

All the techniques