Skip to content

Commit b16d301

Browse files
RalfJungpvdrz
authored andcommitted
test fast sleeping
1 parent 613a436 commit b16d301

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
use std::time::{Duration, Instant};
22

3-
fn duration_sanity(diff: Duration) {
4-
// The virtual clock is deterministic and I got 29us on a 64-bit Linux machine. However, this
5-
// changes according to the platform so we use an interval to be safe. This should be updated
6-
// if `NANOSECONDS_PER_BASIC_BLOCK` changes.
7-
assert!(diff.as_micros() > 10);
8-
assert!(diff.as_micros() < 40);
9-
}
10-
113
fn test_sleep() {
4+
// We sleep a *long* time here -- but the clock is virtual so the test should still pass quickly.
125
let before = Instant::now();
13-
std::thread::sleep(Duration::from_millis(100));
6+
std::thread::sleep(Duration::from_secs(3600));
147
let after = Instant::now();
15-
assert!((after - before).as_millis() >= 100);
8+
assert!((after - before).as_secs() >= 3600);
169
}
1710

18-
fn main() {
11+
/// Ensure that time passes even if we don't sleep (but just wor).
12+
fn test_time_passes() {
1913
// Check `Instant`.
2014
let now1 = Instant::now();
2115
// Do some work to make time pass.
@@ -28,7 +22,14 @@ fn main() {
2822
let diff = now2.duration_since(now1);
2923
assert_eq!(now1 + diff, now2);
3024
assert_eq!(now2 - diff, now1);
31-
duration_sanity(diff);
25+
// The virtual clock is deterministic and I got 29us on a 64-bit Linux machine. However, this
26+
// changes according to the platform so we use an interval to be safe. This should be updated
27+
// if `NANOSECONDS_PER_BASIC_BLOCK` changes.
28+
assert!(diff.as_micros() > 10);
29+
assert!(diff.as_micros() < 40);
30+
}
3231

32+
fn main() {
33+
test_time_passes();
3334
test_sleep();
3435
}

0 commit comments

Comments
 (0)