20
20
//! });
21
21
//! ```
22
22
23
- use futures:: { future:: lazy, Future } ;
23
+ use tokio_executor:: park:: { Park , Unpark } ;
24
+ use tokio_timer:: clock:: { Clock , Now } ;
25
+ use tokio_timer:: Timer ;
26
+
24
27
use std:: marker:: PhantomData ;
25
28
use std:: rc:: Rc ;
26
29
use std:: sync:: { Arc , Mutex } ;
27
30
use std:: time:: { Duration , Instant } ;
28
- use tokio_executor:: park:: { Park , Unpark } ;
29
- use tokio_timer:: clock:: { Clock , Now } ;
30
- use tokio_timer:: Timer ;
31
31
32
32
/// Run the provided closure with a `MockClock` that starts at the current time.
33
33
pub fn mock < F , R > ( f : F ) -> R
@@ -123,17 +123,16 @@ impl MockClock {
123
123
where
124
124
F : FnOnce ( & mut Handle ) -> R ,
125
125
{
126
- let mut enter = :: tokio_executor:: enter ( ) . unwrap ( ) ;
127
-
128
- :: tokio_timer:: clock:: with_default ( & self . clock , & mut enter, |enter| {
126
+ :: tokio_timer:: clock:: with_default ( & self . clock , || {
129
127
let park = self . time . mock_park ( ) ;
130
128
let timer = Timer :: new ( park) ;
131
129
let handle = timer. handle ( ) ;
132
130
let time = self . time . clone ( ) ;
133
131
134
- :: tokio_timer:: with_default ( & handle, enter , |_ | {
132
+ :: tokio_timer:: with_default ( & handle, | | {
135
133
let mut handle = Handle :: new ( timer, time) ;
136
- lazy ( || Ok :: < _ , ( ) > ( f ( & mut handle) ) ) . wait ( ) . unwrap ( )
134
+ f ( & mut handle)
135
+ // lazy(|| Ok::<_, ()>(f(&mut handle))).wait().unwrap()
137
136
} )
138
137
} )
139
138
}
@@ -145,8 +144,13 @@ impl Handle {
145
144
}
146
145
147
146
/// Turn the internal timer and mock park for the provided duration.
148
- pub fn turn ( & mut self , duration : Option < Duration > ) {
149
- self . timer . turn ( duration) . unwrap ( ) ;
147
+ pub fn turn ( & mut self ) {
148
+ self . timer . turn ( None ) . unwrap ( ) ;
149
+ }
150
+
151
+ /// Turn the internal timer and mock park for the provided duration.
152
+ pub fn turn_for ( & mut self , duration : Duration ) {
153
+ self . timer . turn ( Some ( duration) ) . unwrap ( ) ;
150
154
}
151
155
152
156
/// Advance the `MockClock` by the provided duration.
@@ -156,14 +160,26 @@ impl Handle {
156
160
157
161
while inner. lock ( ) . unwrap ( ) . now ( ) < deadline {
158
162
let dur = deadline - inner. lock ( ) . unwrap ( ) . now ( ) ;
159
- self . turn ( Some ( dur) ) ;
163
+ self . turn_for ( dur) ;
160
164
}
161
165
}
162
166
167
+ /// Returns the total amount of time the time has been advanced.
168
+ pub fn advanced ( & self ) -> Duration {
169
+ self . time . inner . lock ( ) . unwrap ( ) . advance
170
+ }
171
+
163
172
/// Get the currently mocked time
164
173
pub fn now ( & mut self ) -> Instant {
165
174
self . time . now ( )
166
175
}
176
+
177
+ /// Turn the internal timer once, but force "parking" for `duration` regardless of any pending
178
+ /// timeouts
179
+ pub fn park_for ( & mut self , duration : Duration ) {
180
+ self . time . inner . lock ( ) . unwrap ( ) . park_for = Some ( duration) ;
181
+ self . turn ( )
182
+ }
167
183
}
168
184
169
185
impl MockTime {
0 commit comments