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
pub mod std_thread;
pub mod tokio_impl;
#[cfg(test)]
mod test {
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
const TWENTY: Duration = Duration::from_millis(20);
use crate::std_thread;
use crate::tokio_impl;
#[test]
fn simple_test() {
let dyn_timeout = std_thread::DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).unwrap();
}
#[test]
fn cancel_test() {
let mut dyn_timeout = std_thread::DynTimeout::new(Duration::from_secs(20), || {
panic!("Should never append");
});
dyn_timeout.add(Duration::from_secs(20)).unwrap();
dyn_timeout.cancel().unwrap();
}
#[tokio::test]
async fn tokio_test() {
let dyn_timeout = tokio_impl::DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).await.unwrap();
}
lazy_static::lazy_static! {
static ref TIME: Arc::<Mutex::<SystemTime>> = Arc::new(Mutex::new(SystemTime::now()));
}
#[tokio::test]
async fn tokio_test_bench() {
{
let mut time = TIME.lock().unwrap();
*time = SystemTime::now();
}
let mut dyn_timeout = tokio_impl::DynTimeout::new(TWENTY, move || {
let st = TIME.lock().unwrap();
let dur = st.elapsed().unwrap();
assert!(dur > Duration::from_millis(36) && dur < Duration::from_millis(44));
});
dyn_timeout.add(TWENTY).await.unwrap();
dyn_timeout.wait().await.unwrap();
}
}