diff options
author | Carl Lerche <me@carllerche.com> | 2019-06-30 08:48:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-30 08:48:53 -0700 |
commit | b2c777846eda2f8d68b3877d0cd5dff77ccde76f (patch) | |
tree | a2d5cbe6518a74b89f5587ee9996bdb79f127bb1 /tokio-test/src | |
parent | 8e7d8af5886000c20bbe6b6b046bbc1c8256015f (diff) |
timer: finish updating timer (#1222)
* timer: restructure feature flags
* update timer tests
* Add `async-traits` to CI
This also disables a buggy `threadpool` test. This test should be fixed in the future.
Refs #1225
Diffstat (limited to 'tokio-test/src')
-rw-r--r-- | tokio-test/src/clock.rs | 40 | ||||
-rw-r--r-- | tokio-test/src/lib.rs | 2 | ||||
-rw-r--r-- | tokio-test/src/macros.rs | 33 |
3 files changed, 33 insertions, 42 deletions
diff --git a/tokio-test/src/clock.rs b/tokio-test/src/clock.rs index 75d3ad62..37e6acc3 100644 --- a/tokio-test/src/clock.rs +++ b/tokio-test/src/clock.rs @@ -20,14 +20,14 @@ //! }); //! ``` -use futures::{future::lazy, Future}; +use tokio_executor::park::{Park, Unpark}; +use tokio_timer::clock::{Clock, Now}; +use tokio_timer::Timer; + use std::marker::PhantomData; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -use tokio_executor::park::{Park, Unpark}; -use tokio_timer::clock::{Clock, Now}; -use tokio_timer::Timer; /// Run the provided closure with a `MockClock` that starts at the current time. pub fn mock<F, R>(f: F) -> R @@ -123,17 +123,16 @@ impl MockClock { where F: FnOnce(&mut Handle) -> R, { - let mut enter = ::tokio_executor::enter().unwrap(); - - ::tokio_timer::clock::with_default(&self.clock, &mut enter, |enter| { + ::tokio_timer::clock::with_default(&self.clock, || { let park = self.time.mock_park(); let timer = Timer::new(park); let handle = timer.handle(); let time = self.time.clone(); - ::tokio_timer::with_default(&handle, enter, |_| { + ::tokio_timer::with_default(&handle, || { let mut handle = Handle::new(timer, time); - lazy(|| Ok::<_, ()>(f(&mut handle))).wait().unwrap() + f(&mut handle) + // lazy(|| Ok::<_, ()>(f(&mut handle))).wait().unwrap() }) }) } @@ -145,8 +144,13 @@ impl Handle { } /// Turn the internal timer and mock park for the provided duration. - pub fn turn(&mut self, duration: Option<Duration>) { - self.timer.turn(duration).unwrap(); + pub fn turn(&mut self) { + self.timer.turn(None).unwrap(); + } + + /// Turn the internal timer and mock park for the provided duration. + pub fn turn_for(&mut self, duration: Duration) { + self.timer.turn(Some(duration)).unwrap(); } /// Advance the `MockClock` by the provided duration. @@ -156,14 +160,26 @@ impl Handle { while inner.lock().unwrap().now() < deadline { let dur = deadline - inner.lock().unwrap().now(); - self.turn(Some(dur)); + self.turn_for(dur); } } + /// Returns the total amount of time the time has been advanced. + pub fn advanced(&self) -> Duration { + self.time.inner.lock().unwrap().advance + } + /// Get the currently mocked time pub fn now(&mut self) -> Instant { self.time.now() } + + /// Turn the internal timer once, but force "parking" for `duration` regardless of any pending + /// timeouts + pub fn park_for(&mut self, duration: Duration) { + self.time.inner.lock().unwrap().park_for = Some(duration); + self.turn() + } } impl MockTime { diff --git a/tokio-test/src/lib.rs b/tokio-test/src/lib.rs index eca6762a..a31b6337 100644 --- a/tokio-test/src/lib.rs +++ b/tokio-test/src/lib.rs @@ -20,7 +20,7 @@ //! assert_ready!(fut.poll()); //! ``` -// pub mod clock; +pub mod clock; mod macros; pub mod task; diff --git a/tokio-test/src/macros.rs b/tokio-test/src/macros.rs index ff3b6eb2..acf5f551 100644 --- a/tokio-test/src/macros.rs +++ b/tokio-test/src/macros.rs @@ -74,41 +74,16 @@ macro_rules! assert_pending { }}; } -/* /// Assert if a poll is ready and check for equality on the value #[macro_export] macro_rules! assert_ready_eq { ($e:expr, $expect:expr) => { - use $crate::codegen::futures::Async::Ready; - match $e { - Ok(e) => assert_eq!(e, Ready($expect)), - Err(e) => panic!("error = {:?}", e), - } + let val = $crate::assert_ready!($e); + assert_eq!(val, $expect) }; ($e:expr, $expect:expr, $($msg:tt),+) => { - use $crate::codegen::futures::Async::Ready; - match $e { - Ok(e) => assert_eq!(e, Ready($expect), $($msg)+), - Err(e) => { - let msg = format_args!($($msg),+); - panic!("error = {:?}; {}", e, msg) - } - } - }; -} -*/ - -/* -/// Assert if the deadline has passed -#[macro_export] -macro_rules! assert_elapsed { - ($e:expr) => { - assert!($e.unwrap_err().is_elapsed()); - }; - - ($e:expr, $($msg:expr),+) => { - assert!($e.unwrap_err().is_elapsed(), $msg); + let val = $crate::assert_ready!($e); + assert_eq!(val, $expect, $($msg),*) }; } -*/ |