File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments