-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Allow disabling time auto-advance behavior in tests #6113
Open
l4l
wants to merge
1
commit into
tokio-rs:master
Choose a base branch
from
l4l:auto-advance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ cfg_not_test_util! { | |
} | ||
|
||
impl Clock { | ||
pub(crate) fn new(_enable_pausing: bool, _start_paused: bool) -> Clock { | ||
pub(crate) fn new(_enable_pausing: bool, _enable_auto_advance: bool, _start_paused: bool) -> Clock { | ||
Clock {} | ||
} | ||
|
||
|
@@ -81,6 +81,9 @@ cfg_test_util! { | |
/// True if the ability to pause time is enabled. | ||
enable_pausing: bool, | ||
|
||
/// True if auto-advance features is enabled. | ||
enable_auto_advance: bool, | ||
|
||
/// Instant to use as the clock's base instant. | ||
base: std::time::Instant, | ||
|
||
|
@@ -119,13 +122,16 @@ cfg_test_util! { | |
/// | ||
/// # Auto-advance | ||
/// | ||
/// If time is paused and the runtime has no work to do, the clock is | ||
/// By default if time is paused and the runtime has no work to do, the clock is | ||
/// auto-advanced to the next pending timer. This means that [`Sleep`] or | ||
/// other timer-backed primitives can cause the runtime to advance the | ||
/// current time when awaited. | ||
/// | ||
/// This behavior could be disabled with [`set_time_auto_advance`]. | ||
/// | ||
/// [`Sleep`]: crate::time::Sleep | ||
/// [`advance`]: crate::time::advance | ||
/// [`set_time_auto_advance`]: crate::time::set_time_auto_advance | ||
#[track_caller] | ||
pub fn pause() { | ||
with_clock(|maybe_clock| { | ||
|
@@ -190,7 +196,7 @@ cfg_test_util! { | |
/// | ||
/// # Auto-advance | ||
/// | ||
/// If the time is paused and there is no work to do, the runtime advances | ||
/// By default if the time is paused and there is no work to do, the runtime advances | ||
/// time to the next timer. See [`pause`](pause#auto-advance) for more | ||
/// details. | ||
/// | ||
|
@@ -208,6 +214,35 @@ cfg_test_util! { | |
crate::task::yield_now().await; | ||
} | ||
|
||
/// Controls time auto-advance behavior. | ||
/// | ||
/// When time is paused, by default the runtime automatically advances | ||
/// time to the next timer. See [`pause`](pause#auto-advance) for more details. | ||
/// | ||
/// This function allows enabling and disabling the auto-advance behavior. | ||
/// When auto-advance feature is disabled, sleeping will wait indefinitely | ||
/// until it's re-enabled, or the time is manually advanced using [`advance`]. | ||
/// This means that if all tasks call `sleep` simultaneously, the program will | ||
/// deadlock. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If called from outside of the Tokio runtime. | ||
/// | ||
/// [`sleep`]: fn@crate::time::sleep | ||
/// [`advance`]: crate::time::advance | ||
pub fn set_time_auto_advance(enable: bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the fn is in the Also, does this function need to exist (I couldn't say because I don't fully understand the use case yet). |
||
with_clock(|maybe_clock| { | ||
let clock = match maybe_clock { | ||
Some(clock) => clock, | ||
None => return Err("time cannot be frozen from outside the Tokio runtime"), | ||
}; | ||
|
||
clock.set_time_auto_advance(enable); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
/// Returns the current instant, factoring in frozen time. | ||
pub(crate) fn now() -> Instant { | ||
if !DID_PAUSE_CLOCK.load(Ordering::Acquire) { | ||
|
@@ -226,12 +261,13 @@ cfg_test_util! { | |
impl Clock { | ||
/// Returns a new `Clock` instance that uses the current execution context's | ||
/// source of time. | ||
pub(crate) fn new(enable_pausing: bool, start_paused: bool) -> Clock { | ||
pub(crate) fn new(enable_pausing: bool, enable_auto_advance: bool, start_paused: bool) -> Clock { | ||
let now = std::time::Instant::now(); | ||
|
||
let clock = Clock { | ||
inner: Mutex::new(Inner { | ||
enable_pausing, | ||
enable_auto_advance, | ||
base: now, | ||
unfrozen: Some(now), | ||
auto_advance_inhibit_count: 0, | ||
|
@@ -269,6 +305,18 @@ cfg_test_util! { | |
Ok(()) | ||
} | ||
|
||
pub(crate) fn set_time_auto_advance(&self, enable: bool) { | ||
let mut inner = self.inner.lock(); | ||
|
||
if !inner.enable_pausing { | ||
drop(inner); // avoid poisoning the lock | ||
panic!("`time::set_auto_advance()` requires the `current_thread` Tokio runtime. \ | ||
This is the default Runtime used by `#[tokio::test]."); | ||
} | ||
|
||
inner.enable_auto_advance = enable; | ||
} | ||
|
||
/// Temporarily stop auto-advancing the clock (see `tokio::time::pause`). | ||
pub(crate) fn inhibit_auto_advance(&self) { | ||
let mut inner = self.inner.lock(); | ||
|
@@ -282,7 +330,7 @@ cfg_test_util! { | |
|
||
pub(crate) fn can_auto_advance(&self) -> bool { | ||
let inner = self.inner.lock(); | ||
inner.unfrozen.is_none() && inner.auto_advance_inhibit_count == 0 | ||
inner.enable_auto_advance && inner.unfrozen.is_none() && inner.auto_advance_inhibit_count == 0 | ||
} | ||
|
||
pub(crate) fn advance(&self, duration: Duration) -> Result<(), &'static str> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this panic if using the multi-thread runtime flavor?
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.
Yeah, I think so, will add then