@@ -204,6 +204,113 @@ async fn reset() {
204
204
check_interval_poll ! ( i, start, 1001 ) ;
205
205
}
206
206
207
+ #[ tokio:: test( start_paused = true ) ]
208
+ async fn reset_immediatelly ( ) {
209
+ let start = Instant :: now ( ) ;
210
+
211
+ // This is necessary because the timer is only so granular, and in order for
212
+ // all our ticks to resolve, the time needs to be 1ms ahead of what we
213
+ // expect, so that the runtime will see that it is time to resolve the timer
214
+ time:: advance ( ms ( 1 ) ) . await ;
215
+
216
+ let mut i = task:: spawn ( time:: interval_at ( start, ms ( 300 ) ) ) ;
217
+
218
+ check_interval_poll ! ( i, start, 0 ) ;
219
+
220
+ time:: advance ( ms ( 100 ) ) . await ;
221
+ check_interval_poll ! ( i, start) ;
222
+
223
+ time:: advance ( ms ( 200 ) ) . await ;
224
+ check_interval_poll ! ( i, start, 300 ) ;
225
+
226
+ time:: advance ( ms ( 100 ) ) . await ;
227
+ check_interval_poll ! ( i, start) ;
228
+
229
+ i. reset_immediately ( ) ;
230
+
231
+ // We add one because when using `reset` method, `Interval` adds the
232
+ // `period` from `Instant::now()`, which will always be off by one
233
+ check_interval_poll ! ( i, start, 401 ) ;
234
+
235
+ time:: advance ( ms ( 100 ) ) . await ;
236
+ check_interval_poll ! ( i, start) ;
237
+
238
+ time:: advance ( ms ( 200 ) ) . await ;
239
+ check_interval_poll ! ( i, start, 701 ) ;
240
+ }
241
+
242
+ #[ tokio:: test( start_paused = true ) ]
243
+ async fn reset_after ( ) {
244
+ let start = Instant :: now ( ) ;
245
+
246
+ // This is necessary because the timer is only so granular, and in order for
247
+ // all our ticks to resolve, the time needs to be 1ms ahead of what we
248
+ // expect, so that the runtime will see that it is time to resolve the timer
249
+ time:: advance ( ms ( 1 ) ) . await ;
250
+
251
+ let mut i = task:: spawn ( time:: interval_at ( start, ms ( 300 ) ) ) ;
252
+
253
+ check_interval_poll ! ( i, start, 0 ) ;
254
+
255
+ time:: advance ( ms ( 100 ) ) . await ;
256
+ check_interval_poll ! ( i, start) ;
257
+
258
+ time:: advance ( ms ( 200 ) ) . await ;
259
+ check_interval_poll ! ( i, start, 300 ) ;
260
+
261
+ time:: advance ( ms ( 100 ) ) . await ;
262
+ check_interval_poll ! ( i, start) ;
263
+
264
+ i. reset_after ( Duration :: from_millis ( 20 ) ) ;
265
+
266
+ // We add one because when using `reset` method, `Interval` adds the
267
+ // `period` from `Instant::now()`, which will always be off by one
268
+ time:: advance ( ms ( 20 ) ) . await ;
269
+ check_interval_poll ! ( i, start, 421 ) ;
270
+
271
+ time:: advance ( ms ( 100 ) ) . await ;
272
+ check_interval_poll ! ( i, start) ;
273
+
274
+ time:: advance ( ms ( 200 ) ) . await ;
275
+ check_interval_poll ! ( i, start, 721 ) ;
276
+ }
277
+
278
+ #[ tokio:: test( start_paused = true ) ]
279
+ async fn reset_at ( ) {
280
+ let start = Instant :: now ( ) ;
281
+
282
+ // This is necessary because the timer is only so granular, and in order for
283
+ // all our ticks to resolve, the time needs to be 1ms ahead of what we
284
+ // expect, so that the runtime will see that it is time to resolve the timer
285
+ time:: advance ( ms ( 1 ) ) . await ;
286
+
287
+ let mut i = task:: spawn ( time:: interval_at ( start, ms ( 300 ) ) ) ;
288
+
289
+ check_interval_poll ! ( i, start, 0 ) ;
290
+
291
+ time:: advance ( ms ( 100 ) ) . await ;
292
+ check_interval_poll ! ( i, start) ;
293
+
294
+ time:: advance ( ms ( 200 ) ) . await ;
295
+ check_interval_poll ! ( i, start, 300 ) ;
296
+
297
+ time:: advance ( ms ( 100 ) ) . await ;
298
+ check_interval_poll ! ( i, start) ;
299
+
300
+ i. reset_at ( Instant :: now ( ) + Duration :: from_millis ( 40 ) ) ;
301
+
302
+ // We add one because when using `reset` method, `Interval` adds the
303
+ // `period` from `Instant::now()`, which will always be off by one
304
+ time:: advance ( ms ( 40 ) ) . await ;
305
+ check_interval_poll ! ( i, start, 441 ) ;
306
+
307
+ time:: advance ( ms ( 100 ) ) . await ;
308
+ check_interval_poll ! ( i, start) ;
309
+
310
+ time:: advance ( ms ( 200 ) ) . await ;
311
+ check_interval_poll ! ( i, start, 741 ) ;
312
+ }
313
+
207
314
fn poll_next ( interval : & mut task:: Spawn < time:: Interval > ) -> Poll < Instant > {
208
315
interval. enter ( |cx, mut interval| interval. poll_tick ( cx) )
209
316
}
0 commit comments