Skip to content

Commit 6778a7d

Browse files
authored
time: document paused time details better (#4061)
1 parent 9a97eb3 commit 6778a7d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tokio/src/time/clock.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,24 @@ cfg_test_util! {
6464
/// Pause time
6565
///
6666
/// The current value of `Instant::now()` is saved and all subsequent calls
67-
/// to `Instant::now()` until the timer wheel is checked again will return
68-
/// the saved value. Once the timer wheel is checked, time will immediately
69-
/// advance to the next registered `Sleep`. This is useful for running tests
70-
/// that depend on time.
67+
/// to `Instant::now()` will return the saved value. The saved value can be
68+
/// changed by [`advance`] or by the time auto-advancing once the runtime
69+
/// has no work to do. This only affects the `Instant` type in Tokio, and
70+
/// the `Instant` in std continues to work as normal.
7171
///
7272
/// Pausing time requires the `current_thread` Tokio runtime. This is the
7373
/// default runtime used by `#[tokio::test]`. The runtime can be initialized
7474
/// with time in a paused state using the `Builder::start_paused` method.
7575
///
76+
/// For cases where time is immediately paused, it is better to pause
77+
/// the time using the `main` or `test` macro:
78+
/// ```
79+
/// #[tokio::main(flavor = "current_thread", start_paused = true)]
80+
/// async fn main() {
81+
/// println!("Hello world");
82+
/// }
83+
/// ```
84+
///
7685
/// # Panics
7786
///
7887
/// Panics if time is already frozen or if called from outside of a
@@ -86,6 +95,7 @@ cfg_test_util! {
8695
/// current time when awaited.
8796
///
8897
/// [`Sleep`]: crate::time::Sleep
98+
/// [`advance`]: crate::time::advance
8999
pub fn pause() {
90100
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
91101
clock.pause();
@@ -116,6 +126,11 @@ cfg_test_util! {
116126
/// Increments the saved `Instant::now()` value by `duration`. Subsequent
117127
/// calls to `Instant::now()` will return the result of the increment.
118128
///
129+
/// Although this function is async, it does not wait for timers with a
130+
/// deadline less than the given duration to complete. If you wish to wait
131+
/// for those, you should use the [`sleep`] method instead and rely on the
132+
/// auto-advance feature.
133+
///
119134
/// # Panics
120135
///
121136
/// Panics if time is not frozen or if called from outside of the Tokio
@@ -126,6 +141,8 @@ cfg_test_util! {
126141
/// If the time is paused and there is no work to do, the runtime advances
127142
/// time to the next timer. See [`pause`](pause#auto-advance) for more
128143
/// details.
144+
///
145+
/// [`sleep`]: fn@crate::time::sleep
129146
pub async fn advance(duration: Duration) {
130147
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
131148
clock.advance(duration);

0 commit comments

Comments
 (0)