From df6acf0c2aeb9b8e77172ef30008744a1b5e0815 Mon Sep 17 00:00:00 2001 From: RT Date: Wed, 19 Sep 2018 19:17:39 +0530 Subject: [PATCH] tokio-timer: reset timeout after elapsed in stream (#648) --- tokio-timer/src/timeout.rs | 1 + tokio-timer/tests/timeout.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tokio-timer/src/timeout.rs b/tokio-timer/src/timeout.rs index 06547af50..72b3af80b 100644 --- a/tokio-timer/src/timeout.rs +++ b/tokio-timer/src/timeout.rs @@ -214,6 +214,7 @@ where T: Stream, match self.delay.poll() { Ok(Async::NotReady) => Ok(Async::NotReady), Ok(Async::Ready(_)) => { + self.delay.reset_timeout(); Err(Error::elapsed()) }, Err(e) => Err(Error::timer(e)), diff --git a/tokio-timer/tests/timeout.rs b/tokio-timer/tests/timeout.rs index 4cf9837ec..e7b08bf34 100644 --- a/tokio-timer/tests/timeout.rs +++ b/tokio-timer/tests/timeout.rs @@ -152,3 +152,28 @@ fn stream_and_timeout_in_future() { assert!(item.is_some()); }); } + +#[test] +fn idle_stream_timesout_periodically() { + mocked(|timer, _time| { + // Not yet complete + let (_tx, rx) = mpsc::unbounded::<()>(); + + // Wrap it with a deadline + let mut stream = Timeout::new(rx, ms(100)); + + // Not ready + assert_not_ready!(stream); + + // Turn the timer, it runs for the elapsed time + advance(timer, ms(100)); + + assert_elapsed!(stream); + // Stream's timeout should reset + assert_not_ready!(stream); + + // Turn the timer, it runs for the elapsed time + advance(timer, ms(100)); + assert_elapsed!(stream); + }); +}