From 28c3524b9b9153c38be73f3c603338c6f01ed567 Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Wed, 28 Aug 2024 14:19:53 -0700 Subject: [PATCH] base: enable the timer unit tests Instead of checking for an error range of elapsed time, just make sure the lower bound was reached. No code using this should be written expecting a tight upper bound. Change-Id: Iffb31bf4c621a3dd91b7904d096dc92713be76c6 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5825217 Reviewed-by: Alexandre Courbot Commit-Queue: Frederick Mayle --- base/src/timer.rs | 86 +++++------------------------------------------ 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/base/src/timer.rs b/base/src/timer.rs index 40631ffd24..3caf34a379 100644 --- a/base/src/timer.rs +++ b/base/src/timer.rs @@ -220,118 +220,48 @@ mod tests { use super::*; - // clock error is 2*clock_resolution + 100 microseconds to handle - // time change from calling now() to arming timer - fn get_clock_error() -> Duration { - Timer::new() - .unwrap() - .resolution() - .expect("expected to be able to read timer resolution") - .checked_mul(2) - .expect("timer resolution x 2 should not overflow") - .checked_add(Duration::from_micros(100)) - .expect("timer resolution x 2 + 100 microsecond should not overflow") - } - #[test] - #[ignore] fn one_shot() { - // This test relies on the host having a reliable clock and not being - // overloaded, so it's marked as "ignore". You can run by running - // cargo test -p base timer -- --ignored - let mut tfd = Timer::new().expect("failed to create Timer"); - let dur = Duration::from_millis(1000); - let clock_error = get_clock_error(); - + let dur = Duration::from_millis(10); let now = Instant::now(); tfd.reset_oneshot(dur).expect("failed to arm timer"); - tfd.wait().expect("unable to wait for timer"); let elapsed = now.elapsed(); - // elapsed is within +-clock_error from expected duration - assert!( - elapsed - clock_error <= dur, - "expected {:?} - {:?} <= {:?}", - elapsed, - clock_error, - dur - ); - assert!( - elapsed + clock_error >= dur, - "expected {:?} + {:?} >= {:?}", - elapsed, - clock_error, - dur - ); + assert!(elapsed >= dur, "expected {:?} >= {:?}", elapsed, dur); } /// Similar to one_shot, except this one waits for a clone of the timer. #[test] - #[ignore] fn one_shot_cloned() { let mut tfd = Timer::new().expect("failed to create Timer"); - let dur = Duration::from_millis(1000); let mut cloned_tfd = tfd.try_clone().expect("failed to clone timer"); - // clock error is 2*clock_resolution + 100 microseconds to handle - // time change from calling now() to arming timer - let clock_error = get_clock_error(); - + let dur = Duration::from_millis(10); let now = Instant::now(); tfd.reset_oneshot(dur).expect("failed to arm timer"); cloned_tfd.wait().expect("unable to wait for timer"); let elapsed = now.elapsed(); - - // elapsed is within +-clock_error from expected duration - assert!( - elapsed - clock_error <= dur, - "expected {:?} - {:?} <= {:?}", - elapsed, - clock_error, - dur - ); - assert!( - elapsed + clock_error >= dur, - "expected {:?} + {:?} >= {:?}", - elapsed, - clock_error, - dur - ); + assert!(elapsed >= dur, "expected {:?} >= {:?}", elapsed, dur); } #[test] - #[ignore] fn repeating() { - // This test relies on the host having a reliable clock and not being - // overloaded, so it's marked as "ignore". You can run by running - // cargo test -p base timer -- --ignored - let mut tfd = Timer::new().expect("failed to create Timer"); - // clock error is 2*clock_resolution + 100 microseconds to handle - // time change from calling now() to arming timer - let clock_error = Timer::new() - .unwrap() - .resolution() - .expect("expected to be able to read timer resolution") - .checked_mul(2) - .expect("timer resolution x 2 should not overflow") - .checked_add(Duration::from_micros(100)) - .expect("timer resolution x 2 + 100 microsecond should not overflow"); - let interval = Duration::from_millis(100); + let interval = Duration::from_millis(10); let now = Instant::now(); tfd.reset_repeating(interval).expect("failed to arm timer"); tfd.wait().expect("unable to wait for timer"); // should take `interval` duration for the first wait - assert!(now.elapsed() + clock_error >= interval); + assert!(now.elapsed() >= interval); tfd.wait().expect("unable to wait for timer"); // subsequent waits should take "interval" duration - assert!(now.elapsed() + clock_error >= interval * 2); + assert!(now.elapsed() >= interval * 2); tfd.wait().expect("unable to wait for timer"); - assert!(now.elapsed() + clock_error >= interval * 3); + assert!(now.elapsed() >= interval * 3); } #[test]