Crates.io | bach |
lib.rs | bach |
version | 0.1.1 |
created_at | 2020-11-26 00:49:43.808275+00 |
updated_at | 2025-09-17 23:30:18.322839+00 |
description | Discrete-event simulation environment for async workflows |
homepage | |
repository | https://github.com/camshaft/bach |
max_upload_size | |
id | 316501 |
size | 315,590 |
Bach is a Rust-based framework for simulating and testing complex async/await-based systems in a non-real-time environment. It's capable of modeling network protocols, queueing systems, and concurrent task interactions with testing and visualization tools.
tokio
or async-std
.cargo test
.This example simulates two clients sending "ping" to a server over UDP, which responds with "pong".
use bach::{ext::*, net::UdpSocket};
#[test]
fn ping_pong() {
bach::sim(|| {
for i in 0..2 {
async move {
let socket = UdpSocket::bind("0.0.0.0:0").await.unwrap();
socket.send_to(b"ping", "server:8080").await.unwrap();
let mut data = [0; 4];
let (len, _) = socket.recv_from(&mut data).await.unwrap();
assert_eq!(&data[..len], b"pong");
}
.group(format!("client_{i}"))
.primary()
.spawn();
}
async {
let socket = UdpSocket::bind("server:8080").await.unwrap();
loop {
let mut data = [0; 4];
let (len, addr) = socket.recv_from(&mut data).await.unwrap();
assert_eq!(&data[..len], b"ping");
socket.send_to(b"pong", addr).await.unwrap();
}
}
.group("server")
.spawn();
});
}
This test can be executed with the following command, while also exporting pcaps showing the interaction between the tasks:
$ BACH_PCAP_DIR=target/bach/pcaps cargo test
Add to Cargo.toml
:
[dependencies]
bach = "0.1"
Explore the Bach repository for more details or to contribute!