From 34e388619faf84d61560fb8c9fef910d6458f743 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Thu, 19 Sep 2019 20:20:18 +0200 Subject: [PATCH] timer: delay_for should use tokio_timer::clock::now (#1572) --- tokio-timer/src/interval.rs | 2 +- tokio-timer/src/lib.rs | 4 ++-- tokio/tests/clock.rs | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tokio-timer/src/interval.rs b/tokio-timer/src/interval.rs index 55c5ea3b1..8e103c667 100644 --- a/tokio-timer/src/interval.rs +++ b/tokio-timer/src/interval.rs @@ -40,7 +40,7 @@ impl Interval { /// Creates new `Interval` that yields with interval of `duration`. /// - /// The function is shortcut for `Interval::new(Instant::now() + duration, duration)`. + /// The function is shortcut for `Interval::new(tokio_timer::clock::now() + duration, duration)`. /// /// The `duration` argument must be a non-zero duration. /// diff --git a/tokio-timer/src/lib.rs b/tokio-timer/src/lib.rs index a8cce26e6..df1fe4882 100644 --- a/tokio-timer/src/lib.rs +++ b/tokio-timer/src/lib.rs @@ -70,9 +70,9 @@ pub fn delay(deadline: Instant) -> Delay { /// Create a Future that completes in `duration` from now. /// -/// Equivalent to `delay(Instant::now() + duration)`. Analogous to `std::thread::sleep`. +/// Equivalent to `delay(tokio_timer::clock::now() + duration)`. Analogous to `std::thread::sleep`. pub fn delay_for(duration: Duration) -> Delay { - delay(Instant::now() + duration) + delay(clock::now() + duration) } // ===== Internal utils ===== diff --git a/tokio/tests/clock.rs b/tokio/tests/clock.rs index c8d37da15..64f4b6076 100644 --- a/tokio/tests/clock.rs +++ b/tokio/tests/clock.rs @@ -47,3 +47,13 @@ fn clock_and_timer_single_threaded() { assert!(Instant::now() < when); }); } + +#[test] +fn mocked_clock_delay_for() { + tokio_test::clock::mock(|handle| { + let mut f = tokio_test::task::spawn(delay_for(Duration::from_millis(1))); + tokio_test::assert_pending!(f.poll()); + handle.advance(Duration::from_millis(1)); + tokio_test::assert_ready!(f.poll()); + }); +}