diff --git a/tokio-timer/src/delay.rs b/tokio-timer/src/delay.rs index 8f8b6b1e8..7411e0717 100644 --- a/tokio-timer/src/delay.rs +++ b/tokio-timer/src/delay.rs @@ -43,8 +43,12 @@ impl Delay { Delay { registration } } - pub(crate) fn new_with_handle(deadline: Instant, handle: HandlePriv) -> Delay { - let mut registration = Registration::new(deadline, Duration::from_millis(0)); + pub(crate) fn new_with_handle( + deadline: Instant, + duration: Duration, + handle: HandlePriv, + ) -> Delay { + let mut registration = Registration::new(deadline, duration); registration.register_with(handle); Delay { registration } diff --git a/tokio-timer/src/timer/handle.rs b/tokio-timer/src/timer/handle.rs index d08cdadbf..2abdba3a4 100644 --- a/tokio-timer/src/timer/handle.rs +++ b/tokio-timer/src/timer/handle.rs @@ -1,3 +1,4 @@ +use crate::clock::now; use crate::timer::Inner; use crate::{Delay, Error, /*Interval,*/ Timeout}; use std::cell::RefCell; @@ -130,15 +131,21 @@ impl Handle { /// Create a `Delay` driven by this handle's associated `Timer`. pub fn delay(&self, deadline: Instant) -> Delay { + self.delay_timeout(deadline, Duration::from_secs(0)) + } + + fn delay_timeout(&self, deadline: Instant, duration: Duration) -> Delay { match self.inner { - Some(ref handle_priv) => Delay::new_with_handle(deadline, handle_priv.clone()), - None => Delay::new(deadline), + Some(ref handle_priv) => { + Delay::new_with_handle(deadline, Duration::from_secs(0), handle_priv.clone()) + } + None => Delay::new_timeout(deadline, duration), } } /// Create a `Timeout` driven by this handle's associated `Timer`. - pub fn timeout(&self, value: T, deadline: Instant) -> Timeout { - Timeout::new_with_delay(value, self.delay(deadline)) + pub fn timeout(&self, value: T, timeout: Duration) -> Timeout { + Timeout::new_with_delay(value, self.delay_timeout(now() + timeout, timeout)) } /* diff --git a/tokio-timer/tests/hammer.rs b/tokio-timer/tests/hammer.rs index 27c529fae..3f23d3bc0 100644 --- a/tokio-timer/tests/hammer.rs +++ b/tokio-timer/tests/hammer.rs @@ -109,16 +109,13 @@ fn hammer_cancel() { barrier.wait(); for _ in 0..PER_THREAD { - let deadline1 = - Instant::now() + Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY)); + let timeout1 = Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY)); + let timeout2 = Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY)); - let deadline2 = - Instant::now() + Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY)); + let deadline = Instant::now() + cmp::min(timeout1, timeout2); - let deadline = cmp::min(deadline1, deadline2); - - let delay = handle.delay(deadline1); - let join = handle.timeout(delay, deadline2); + let delay = handle.delay(Instant::now() + timeout1); + let join = handle.timeout(delay, timeout2); exec.spawn(async move { let _ = join.await;