1
1
use std:: time:: { Duration , Instant } ;
2
2
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
-
11
3
fn test_sleep ( ) {
4
+ // We sleep a *long* time here -- but the clock is virtual so the test should still pass quickly.
12
5
let before = Instant :: now ( ) ;
13
- std:: thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
6
+ std:: thread:: sleep ( Duration :: from_secs ( 3600 ) ) ;
14
7
let after = Instant :: now ( ) ;
15
- assert ! ( ( after - before) . as_millis ( ) >= 100 ) ;
8
+ assert ! ( ( after - before) . as_secs ( ) >= 3600 ) ;
16
9
}
17
10
18
- fn main ( ) {
11
+ /// Ensure that time passes even if we don't sleep (but just wor).
12
+ fn test_time_passes ( ) {
19
13
// Check `Instant`.
20
14
let now1 = Instant :: now ( ) ;
21
15
// Do some work to make time pass.
@@ -28,7 +22,14 @@ fn main() {
28
22
let diff = now2. duration_since ( now1) ;
29
23
assert_eq ! ( now1 + diff, now2) ;
30
24
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
+ }
32
31
32
+ fn main ( ) {
33
+ test_time_passes ( ) ;
33
34
test_sleep ( ) ;
34
35
}
0 commit comments