-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_async.rs
42 lines (33 loc) · 1.06 KB
/
simple_async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use netsim_async::{Edge, EdgePolicy, Latency, SimConfiguration, SimContext};
use std::time::Duration;
use tokio::time::Instant;
const MSG: &str = "Hello World!";
#[tokio::main]
async fn main() {
let configuration = SimConfiguration::default();
let mut context: SimContext<&'static str> = SimContext::with_config(configuration);
let net1 = context.open().unwrap();
let mut net2 = context.open().unwrap();
context
.set_edge_policy(
Edge::new((net1.id(), net2.id())),
EdgePolicy {
latency: Latency::new(Duration::from_secs(1)),
..Default::default()
},
)
.unwrap();
net1.send_to(net2.id(), MSG).unwrap();
let instant = Instant::now();
let Some((from, msg)) = net2.recv().await else {
panic!("expecting message from NET1")
};
let elapsed = instant.elapsed();
assert_eq!(from, net1.id());
println!(
"{from} -> {net2} ({}ms): {msg}",
elapsed.as_millis(),
net2 = net2.id()
);
context.shutdown().unwrap();
}