Skip to content
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

Merged
merged 5 commits into from
Apr 21, 2021
Merged

time: prevent time::advance from going too far #3712

merged 5 commits into from
Apr 21, 2021

Conversation

carllerche
Copy link
Member

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

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
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-time Module: tokio/time labels Apr 17, 2021
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
carllerche and others added 2 commits April 20, 2021 10:45
Co-authored-by: Alice Ryhl <alice@ryhl.io>
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
tokio/tests/time_pause.rs Outdated Show resolved Hide resolved
Comment on lines +15 to +28
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
);
}};
}
Copy link
Contributor

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

Copy link
Contributor

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

carllerche and others added 2 commits April 21, 2021 14:39
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-time Module: tokio/time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

time::advance advances timer wheel too far when called after time::Sleep is polled
3 participants