diff options
| author | Ralf Jung <post@ralfj.de> | 2022-09-01 12:30:11 +0200 |
|---|---|---|
| committer | Christian Poveda <git@pvdrz.com> | 2022-09-13 15:16:41 -0500 |
| commit | b16d301dd9f2138b91a05e398a61d3ddeb8f6b7b (patch) | |
| tree | a14a15b5da2c233342228b987b455a1430e8e0d1 | |
| parent | 613a436cfc7243d3b37d95b83fcf7aa9639f84da (diff) | |
| download | rust-b16d301dd9f2138b91a05e398a61d3ddeb8f6b7b.tar.gz rust-b16d301dd9f2138b91a05e398a61d3ddeb8f6b7b.zip | |
test fast sleeping
| -rw-r--r-- | tests/pass/shims/time-with-isolation.rs | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/tests/pass/shims/time-with-isolation.rs b/tests/pass/shims/time-with-isolation.rs index 3366b77690d..7e6c74d71cb 100644 --- a/tests/pass/shims/time-with-isolation.rs +++ b/tests/pass/shims/time-with-isolation.rs @@ -1,21 +1,15 @@ use std::time::{Duration, Instant}; -fn duration_sanity(diff: Duration) { - // The virtual clock is deterministic and I got 29us on a 64-bit Linux machine. However, this - // changes according to the platform so we use an interval to be safe. This should be updated - // if `NANOSECONDS_PER_BASIC_BLOCK` changes. - assert!(diff.as_micros() > 10); - assert!(diff.as_micros() < 40); -} - fn test_sleep() { + // We sleep a *long* time here -- but the clock is virtual so the test should still pass quickly. let before = Instant::now(); - std::thread::sleep(Duration::from_millis(100)); + std::thread::sleep(Duration::from_secs(3600)); let after = Instant::now(); - assert!((after - before).as_millis() >= 100); + assert!((after - before).as_secs() >= 3600); } -fn main() { +/// Ensure that time passes even if we don't sleep (but just wor). +fn test_time_passes() { // Check `Instant`. let now1 = Instant::now(); // Do some work to make time pass. @@ -28,7 +22,14 @@ fn main() { let diff = now2.duration_since(now1); assert_eq!(now1 + diff, now2); assert_eq!(now2 - diff, now1); - duration_sanity(diff); + // The virtual clock is deterministic and I got 29us on a 64-bit Linux machine. However, this + // changes according to the platform so we use an interval to be safe. This should be updated + // if `NANOSECONDS_PER_BASIC_BLOCK` changes. + assert!(diff.as_micros() > 10); + assert!(diff.as_micros() < 40); +} +fn main() { + test_time_passes(); test_sleep(); } |
