@@ -64,15 +64,24 @@ cfg_test_util! {
64
64
/// Pause time
65
65
///
66
66
/// 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 .
71
71
///
72
72
/// Pausing time requires the `current_thread` Tokio runtime. This is the
73
73
/// default runtime used by `#[tokio::test]`. The runtime can be initialized
74
74
/// with time in a paused state using the `Builder::start_paused` method.
75
75
///
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
+ ///
76
85
/// # Panics
77
86
///
78
87
/// Panics if time is already frozen or if called from outside of a
@@ -86,6 +95,7 @@ cfg_test_util! {
86
95
/// current time when awaited.
87
96
///
88
97
/// [`Sleep`]: crate::time::Sleep
98
+ /// [`advance`]: crate::time::advance
89
99
pub fn pause( ) {
90
100
let clock = clock( ) . expect( "time cannot be frozen from outside the Tokio runtime" ) ;
91
101
clock. pause( ) ;
@@ -116,6 +126,11 @@ cfg_test_util! {
116
126
/// Increments the saved `Instant::now()` value by `duration`. Subsequent
117
127
/// calls to `Instant::now()` will return the result of the increment.
118
128
///
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
+ ///
119
134
/// # Panics
120
135
///
121
136
/// Panics if time is not frozen or if called from outside of the Tokio
@@ -126,6 +141,8 @@ cfg_test_util! {
126
141
/// If the time is paused and there is no work to do, the runtime advances
127
142
/// time to the next timer. See [`pause`](pause#auto-advance) for more
128
143
/// details.
144
+ ///
145
+ /// [`sleep`]: fn@crate::time::sleep
129
146
pub async fn advance( duration: Duration ) {
130
147
let clock = clock( ) . expect( "time cannot be frozen from outside the Tokio runtime" ) ;
131
148
clock. advance( duration) ;
0 commit comments