-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time: prevent time::advance
from going too far
#3712
Conversation
Previously, `time::advance` would set the mocked clock forward the requested amount, then yield. However, if there was no work ready to perform immediately, this would result in advancing to the next expiring sleep. Now, `time::advance(...)` will unblock at the requested time. The difference between `time::advance(...)` and `time::sleep(...)` is a bit fuzzy. The main difference is `time::sleep(...)` operates on the current task and `time::advance(...)` operates at the runtime level. Fixes #3710
Co-authored-by: Alice Ryhl <alice@ryhl.io>
macro_rules! assert_elapsed { | ||
($now:expr, $ms:expr) => {{ | ||
let elapsed = $now.elapsed(); | ||
let lower = ms($ms); | ||
|
||
// Handles ms rounding | ||
assert!( | ||
elapsed >= lower && elapsed <= lower + ms(1), | ||
"actual = {:?}, expected = {:?}", | ||
elapsed, | ||
lower | ||
); | ||
}}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would actually be an awesome macro to expose publicly, I end up adding a utility like this in any test cases I write that track time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have similar macros in tokio-test
. I don't see why we couldn't have this one there too
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Previously,
time::advance
would set the mocked clock forward therequested amount, then yield. However, if there was no work ready to
perform immediately, this would result in advancing to the next expiring
sleep.
Now,
time::advance(...)
will unblock at the requested time. Thedifference between
time::advance(...)
andtime::sleep(...)
is a bitfuzzy. The main difference is
time::sleep(...)
operates on the currenttask and
time::advance(...)
operates at the runtime level.Fixes #3710