Skip to content

Commit 3165d8f

Browse files
committed
add test for time with isolation
1 parent bd504d0 commit 3165d8f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@compile-flags: -Zmiri-disable-isolation
2+
3+
use std::time::{Duration, Instant};
4+
5+
fn duration_sanity(diff: Duration) {
6+
// On my laptop, I observed times around 15-40ms. Add 10x lee-way both ways.
7+
assert!(diff.as_millis() > 1);
8+
assert!(diff.as_millis() < 500);
9+
}
10+
11+
fn test_sleep() {
12+
let before = Instant::now();
13+
std::thread::sleep(Duration::from_millis(100));
14+
let after = Instant::now();
15+
assert!((after - before).as_millis() >= 100);
16+
}
17+
18+
fn main() {
19+
// Check `Instant`.
20+
let now1 = Instant::now();
21+
// Do some work to make time pass.
22+
for _ in 0..10 {
23+
drop(vec![42]);
24+
}
25+
let now2 = Instant::now();
26+
assert!(now2 > now1);
27+
// Sanity-check the difference we got.
28+
let diff = now2.duration_since(now1);
29+
assert_eq!(now1 + diff, now2);
30+
assert_eq!(now2 - diff, now1);
31+
duration_sanity(diff);
32+
33+
test_sleep();
34+
}
35+

0 commit comments

Comments
 (0)