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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use super::{Config, Runtime};
use futures::StreamExt;
use std::future::Future;
use std::time::{Duration, SystemTime};
pub struct Builder {
pub seed: u64,
pub count: u64,
pub jobs: u16,
pub config: Config,
pub time_limit: Option<Duration>,
pub check: bool,
}
impl Builder {
pub fn from_env() -> Self {
let seed: u64 = if let Ok(seed_str) = std::env::var("MADSIM_TEST_SEED") {
seed_str
.parse()
.expect("MADSIM_TEST_SEED should be an integer")
} else {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
};
let jobs: u16 = if let Ok(jobs_str) = std::env::var("MADSIM_TEST_JOBS") {
jobs_str
.parse()
.expect("MADSIM_TEST_JOBS should be an integer")
} else {
1
};
let config = if let Ok(config_path) = std::env::var("MADSIM_TEST_CONFIG") {
let content = std::fs::read_to_string(config_path).expect("failed to read config file");
content
.parse::<Config>()
.expect("failed to parse config file")
} else {
Config::default()
};
let mut count: u64 = if let Ok(num_str) = std::env::var("MADSIM_TEST_NUM") {
num_str
.parse()
.expect("MADSIM_TEST_NUM should be an integer")
} else {
1
};
let time_limit = std::env::var("MADSIM_TEST_TIME_LIMIT").ok().map(|num_str| {
Duration::from_secs_f64(
num_str
.parse::<f64>()
.expect("MADSIM_TEST_TIME_LIMIT should be an number"),
)
});
let check = std::env::var("MADSIM_TEST_CHECK_DETERMINISM").is_ok();
if check {
count = count.max(2);
}
Builder {
seed,
count,
jobs,
config,
time_limit,
check,
}
}
pub fn run<F>(self, f: fn() -> F) -> F::Output
where
F: Future + 'static,
F::Output: Send,
{
if self.check {
return Runtime::check_determinism(self.seed, self.config, f);
}
let stream = futures::stream::iter(self.seed..self.seed + self.count)
.map(|seed| {
let config = self.config.clone();
async move {
let (tx, rx) = tokio::sync::oneshot::channel();
let handle = std::thread::spawn(move || {
let mut rt = Runtime::with_seed_and_config(seed, config);
if let Some(limit) = self.time_limit {
rt.set_time_limit(limit);
}
let ret = rt.block_on(f());
tx.send(()).unwrap();
ret
});
let _ = rx.await;
(seed, handle.join())
}
})
.buffer_unordered(self.jobs as usize);
let mut return_value = None;
for (seed, res) in futures::executor::block_on_stream(stream) {
match res {
Ok(ret) => return_value = Some(ret),
Err(e) => super::panic_with_info(seed, self.config.hash(), e),
}
}
return_value.unwrap()
}
}