-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.rs
More file actions
54 lines (47 loc) · 1.95 KB
/
Copy pathsimulation.rs
File metadata and controls
54 lines (47 loc) · 1.95 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
extern crate circus_simulation;
use circus_simulation::deterministic::platform::SimulationPlatform;
use circus_simulation::deterministic::runtime::executor::DeterministicExecutor;
use circus_simulation::deterministic::runtime::reactor::DeterministicReactor;
use circus_simulation::deterministic::runtime::task::Task;
use circus_simulation::platform::{Platform, PlatformProvider};
use std::time::Duration;
use tracing::Level;
fn main() {
tracing_subscriber::fmt()
.with_max_level(Level::TRACE)
.init();
// let's create an Deterministic executor and runtime
let reactor = DeterministicReactor::default();
let mut executor = DeterministicExecutor::new_with_reactor(reactor.clone());
// let's create a simulated platform. You can swap implementation between:
// * production, allowing you to talk to your OS,
// * dev, with an buggified deterministic simulation.
let platform: PlatformProvider = SimulationPlatform::new(42, reactor).into();
// let's run our async function
executor.spawn(Task::new(run_platform(platform)));
executor.run();
}
async fn run_platform(mut platform: PlatformProvider) {
// opening a file with the simulated platform can trigger:
// * `buggified` errors,
// * random time to open the file.
let start_time = platform.now();
// We are going to loop opening a file.
for i in 0..10 {
let file_result = platform.open("/etc/hosts".as_ref()).await;
// here, with the seed 42, the first `open` will take 817ms.
// Don't worry, this is simulated time, so we are not waiting 817ms!
if i == 0 {
assert!(platform
.now()
.duration_since(start_time)
.eq(&Duration::from_millis(817)));
}
if i == 8 {
// using the seed 42, the 8th opening will trigger an error.
assert!(file_result.is_err());
} else {
assert!(file_result.is_ok());
}
}
}