Skip to main content

Module sim

Module sim 

Source
Expand description

An in-process network with a virtual clock.

This is a supported platform, not a test fixture. It implements the same traits a real machine does, so the stack above it cannot tell the difference — and it gives three things a real machine cannot:

Time costs nothing. SimNet is itself the Timer, and advances to the next scheduled deadline instead of waiting for it, so an hour of an intermittently-connected device’s idle period, or the whole of MRP’s five-transmission backoff ladder, runs in microseconds.

The network misbehaves on demand. SimNet drops, duplicates, delays and reorders datagrams to a schedule you set, so “what happens when the third retransmission is the one that gets through” is a test rather than a hope.

Everything is deterministic. Same inputs, same interleaving, same result, every time — including the pseudo-random choices, which come from a seeded generator you can write down in the test.

use matter_kit::platform::sim::{SimNet, block_on};
use matter_kit::platform::{Timer, Udp, Duration};

let net = SimNet::new(42);
let a = net.node(1);
let b = net.node(2);

block_on(&net, async {
    a.send_to(b"hello", b.addr()).await.unwrap();
    let mut buf = [0u8; 32];
    let (n, from) = b.recv_from(&mut buf).await.unwrap();
    assert_eq!(&buf[..n], b"hello");
    assert_eq!(from, a.addr());
});

Structs§

Impairment
What the simulated network should do to the datagrams crossing it.
SimKv
An in-memory key-value store.
SimNet
A simulated network: a virtual clock, a set of nodes, and the datagrams between them.
SimNode
One node’s view of a SimNet: a Udp socket with an address.
SimRng
A deterministic pseudo-random generator.

Functions§

block_on
Runs a future to completion on virtual time.