mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
time: document paused time details better (#4061)
This commit is contained in:
parent
9a97eb36bc
commit
6778a7def6
@ -64,15 +64,24 @@ cfg_test_util! {
|
|||||||
/// Pause time
|
/// Pause time
|
||||||
///
|
///
|
||||||
/// The current value of `Instant::now()` is saved and all subsequent calls
|
/// The current value of `Instant::now()` is saved and all subsequent calls
|
||||||
/// to `Instant::now()` until the timer wheel is checked again will return
|
/// to `Instant::now()` will return the saved value. The saved value can be
|
||||||
/// the saved value. Once the timer wheel is checked, time will immediately
|
/// changed by [`advance`] or by the time auto-advancing once the runtime
|
||||||
/// advance to the next registered `Sleep`. This is useful for running tests
|
/// has no work to do. This only affects the `Instant` type in Tokio, and
|
||||||
/// that depend on time.
|
/// the `Instant` in std continues to work as normal.
|
||||||
///
|
///
|
||||||
/// Pausing time requires the `current_thread` Tokio runtime. This is the
|
/// Pausing time requires the `current_thread` Tokio runtime. This is the
|
||||||
/// default runtime used by `#[tokio::test]`. The runtime can be initialized
|
/// default runtime used by `#[tokio::test]`. The runtime can be initialized
|
||||||
/// with time in a paused state using the `Builder::start_paused` method.
|
/// with time in a paused state using the `Builder::start_paused` method.
|
||||||
///
|
///
|
||||||
|
/// For cases where time is immediately paused, it is better to pause
|
||||||
|
/// the time using the `main` or `test` macro:
|
||||||
|
/// ```
|
||||||
|
/// #[tokio::main(flavor = "current_thread", start_paused = true)]
|
||||||
|
/// async fn main() {
|
||||||
|
/// println!("Hello world");
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if time is already frozen or if called from outside of a
|
/// Panics if time is already frozen or if called from outside of a
|
||||||
@ -86,6 +95,7 @@ cfg_test_util! {
|
|||||||
/// current time when awaited.
|
/// current time when awaited.
|
||||||
///
|
///
|
||||||
/// [`Sleep`]: crate::time::Sleep
|
/// [`Sleep`]: crate::time::Sleep
|
||||||
|
/// [`advance`]: crate::time::advance
|
||||||
pub fn pause() {
|
pub fn pause() {
|
||||||
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
|
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
|
||||||
clock.pause();
|
clock.pause();
|
||||||
@ -116,6 +126,11 @@ cfg_test_util! {
|
|||||||
/// Increments the saved `Instant::now()` value by `duration`. Subsequent
|
/// Increments the saved `Instant::now()` value by `duration`. Subsequent
|
||||||
/// calls to `Instant::now()` will return the result of the increment.
|
/// calls to `Instant::now()` will return the result of the increment.
|
||||||
///
|
///
|
||||||
|
/// Although this function is async, it does not wait for timers with a
|
||||||
|
/// deadline less than the given duration to complete. If you wish to wait
|
||||||
|
/// for those, you should use the [`sleep`] method instead and rely on the
|
||||||
|
/// auto-advance feature.
|
||||||
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if time is not frozen or if called from outside of the Tokio
|
/// Panics if time is not frozen or if called from outside of the Tokio
|
||||||
@ -126,6 +141,8 @@ cfg_test_util! {
|
|||||||
/// If the time is paused and there is no work to do, the runtime advances
|
/// 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
|
/// time to the next timer. See [`pause`](pause#auto-advance) for more
|
||||||
/// details.
|
/// details.
|
||||||
|
///
|
||||||
|
/// [`sleep`]: fn@crate::time::sleep
|
||||||
pub async fn advance(duration: Duration) {
|
pub async fn advance(duration: Duration) {
|
||||||
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
|
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
|
||||||
clock.advance(duration);
|
clock.advance(duration);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user