From d7a989c008b66920f6e469e563f3c8890b2e64c7 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 4 Aug 2018 19:06:40 +0200 Subject: [PATCH] Migrate futures tests to use futures-testing --- futures/Cargo.toml | 3 ++ futures/tests/abortable.rs | 19 +++++----- futures/tests/fuse.rs | 8 ++--- futures/tests/futures_ordered.rs | 12 +++---- futures/tests/futures_unordered.rs | 6 ++-- futures/tests/support/assert.rs | 9 +++-- .../tests/support/counter_waker_context.rs | 36 ------------------- futures/tests/support/mod.rs | 11 ------ futures/tests/support/noop_waker_context.rs | 22 ------------ futures/tests/support/panic_executor.rs | 10 ------ futures/tests/support/panic_waker_context.rs | 24 ------------- 11 files changed, 26 insertions(+), 134 deletions(-) delete mode 100644 futures/tests/support/counter_waker_context.rs delete mode 100644 futures/tests/support/noop_waker_context.rs delete mode 100644 futures/tests/support/panic_executor.rs delete mode 100644 futures/tests/support/panic_waker_context.rs diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 5f16841f09..2c8daeccc6 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -32,6 +32,9 @@ futures-io-preview = { path = "../futures-io", version = "0.3.0-alpha.2", defaul futures-sink-preview = { path = "../futures-sink", version = "0.3.0-alpha.2", default-features = false } futures-util-preview = { path = "../futures-util", version = "0.3.0-alpha.2", default-features = false } +[dev-dependencies] +futures-testing-preview = { path = "../futures-testing", version = "0.3.0-alpha.3", default-features = false } + [features] nightly = ["futures-util-preview/nightly"] std = ["futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std"] diff --git a/futures/tests/abortable.rs b/futures/tests/abortable.rs index 733fede0db..41845af6c4 100644 --- a/futures/tests/abortable.rs +++ b/futures/tests/abortable.rs @@ -7,9 +7,9 @@ use futures::channel::oneshot; use futures::executor::block_on; use futures::future::{abortable, Aborted}; use futures::task::Poll; +use futures_testing::task::{TestContext, wake, executor}; mod support; -use self::support::with_counter_waker_context; #[test] fn abortable_works() { @@ -25,14 +25,15 @@ fn abortable_awakens() { let (_tx, a_rx) = oneshot::channel::<()>(); let (mut abortable_rx, abort_handle) = abortable(a_rx); - with_counter_waker_context(|cx, counter| { - assert_eq!(0, counter.get()); - assert_eq!(Poll::Pending, abortable_rx.poll_unpin(cx)); - assert_eq!(0, counter.get()); - abort_handle.abort(); - assert_eq!(1, counter.get()); - assert_eq!(Poll::Ready(Err(Aborted)), abortable_rx.poll_unpin(cx)); - }) + let mut context = TestContext::::default(); + assert_eq!(0, context.wake().count()); + assert_eq!(Poll::Pending, context.with(|cx| abortable_rx.poll_unpin(cx))); + assert_eq!(0, context.wake().count()); + abort_handle.abort(); + assert_eq!(1, context.wake().count()); + assert_eq!( + Poll::Ready(Err(Aborted)), + context.with(|cx| abortable_rx.poll_unpin(cx))); } #[test] diff --git a/futures/tests/fuse.rs b/futures/tests/fuse.rs index 5fa9e89efe..9c75d91df8 100644 --- a/futures/tests/fuse.rs +++ b/futures/tests/fuse.rs @@ -1,17 +1,13 @@ #![feature(pin, arbitrary_self_types, futures_api)] -#[macro_use] -extern crate futures; - use futures::future; use futures::prelude::*; - -mod support; +use futures_testing::task::{executor, wake, with_context}; #[test] fn fuse() { let mut future = future::ready::(2).fuse(); - support::with_panic_waker_context(|cx| { + with_context::(|cx| { assert!(future.poll_unpin(cx).is_ready()); assert!(future.poll_unpin(cx).is_pending()); }) diff --git a/futures/tests/futures_ordered.rs b/futures/tests/futures_ordered.rs index e0f3b824f9..d87bf56ee7 100644 --- a/futures/tests/futures_ordered.rs +++ b/futures/tests/futures_ordered.rs @@ -1,15 +1,11 @@ #![feature(pin, arbitrary_self_types, futures_api)] -#[macro_use] -extern crate futures; - use futures::channel::oneshot; use futures::executor::{block_on, block_on_stream}; use futures::future::{self, FutureObj}; use futures::prelude::*; use futures::stream::{futures_ordered, FuturesOrdered}; - -mod support; +use futures_testing::task::{executor, wake, with_context}; #[test] fn works_1() { @@ -20,7 +16,7 @@ fn works_1() { let mut stream = futures_ordered(vec![a_rx, b_rx, c_rx]); b_tx.send(99).unwrap(); - support::with_noop_waker_context(|cx| { + with_context::(|cx| { assert!(stream.poll_next_unpin(cx).is_pending()); }); @@ -45,7 +41,7 @@ fn works_2() { FutureObj::new(Box::new(b_rx.join(c_rx).map(|(a, b)| Ok(a? + b?)))), ]); - support::with_noop_waker_context(|cx| { + with_context::(|cx| { a_tx.send(33).unwrap(); b_tx.send(33).unwrap(); assert!(stream.poll_next_unpin(cx).is_ready()); @@ -78,7 +74,7 @@ fn queue_never_unblocked() { Box::new(b_rx.select(c_rx).then(|res| Ok(Box::new(res) as Box))) as _, ]); - support::with_noop_waker_context(f)(|cx| { + with_context::(|cx| { for _ in 0..10 { assert!(stream.poll_next(cx).unwrap().is_pending()); } diff --git a/futures/tests/futures_unordered.rs b/futures/tests/futures_unordered.rs index 7de102dc76..0be46df221 100644 --- a/futures/tests/futures_unordered.rs +++ b/futures/tests/futures_unordered.rs @@ -8,10 +8,9 @@ use futures::executor::{block_on, block_on_stream}; use futures::future::{self, FutureObj}; use futures::stream::{futures_unordered, FuturesUnordered}; use futures::prelude::*; +use futures_testing::task::{executor, wake, with_context}; use std::boxed::Box; -mod support; - #[test] fn works_1() { let (a_tx, a_rx) = oneshot::channel::(); @@ -43,7 +42,8 @@ fn works_2() { a_tx.send(9).unwrap(); b_tx.send(10).unwrap(); - support::with_noop_waker_context(|cx| { + + with_context::(|cx| { assert_eq!(stream.poll_next_unpin(cx), Poll::Ready(Some(Ok(9)))); c_tx.send(20).unwrap(); assert_eq!(stream.poll_next_unpin(cx), Poll::Ready(Some(Ok(30)))); diff --git a/futures/tests/support/assert.rs b/futures/tests/support/assert.rs index d17aef9a79..c6fb89eafe 100644 --- a/futures/tests/support/assert.rs +++ b/futures/tests/support/assert.rs @@ -1,11 +1,10 @@ use futures::prelude::*; +use futures_testing::task::{executor, wake, with_context}; use std::fmt; use std::mem::PinMut; -use super::{with_noop_waker_context, with_panic_waker_context}; - pub fn assert_stream_pending(stream: PinMut) { - with_noop_waker_context(|cx| { + with_context::(|cx| { match stream.poll_next(cx) { Poll::Ready(_) => panic!("stream is not pending"), Poll::Pending => {}, @@ -16,7 +15,7 @@ pub fn assert_stream_pending(stream: PinMut) { pub fn assert_stream_next(stream: PinMut, item: S::Item) where S::Item: Eq + fmt::Debug { - with_panic_waker_context(|cx| { + with_context::(|cx| { match stream.poll_next(cx) { Poll::Ready(Some(x)) => assert_eq!(x, item), Poll::Ready(None) => panic!("stream is at its end"), @@ -27,7 +26,7 @@ pub fn assert_stream_next(stream: PinMut, item: S::Item) pub fn assert_stream_done(stream: PinMut) { - with_panic_waker_context(|cx| { + with_context::(|cx| { match stream.poll_next(cx) { Poll::Ready(Some(_)) => panic!("stream had more elements"), Poll::Ready(None) => {}, diff --git a/futures/tests/support/counter_waker_context.rs b/futures/tests/support/counter_waker_context.rs deleted file mode 100644 index 7960af6035..0000000000 --- a/futures/tests/support/counter_waker_context.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::sync::Arc; -use std::sync::atomic::{AtomicUsize, Ordering}; - -use futures::task::Wake; -use futures::prelude::*; - -use super::panic_executor::PanicExecutor; - -pub struct CounterWaker(AtomicUsize); - -impl CounterWaker { - pub fn get(&self) -> usize { - self.0.load(Ordering::SeqCst) - } - - pub fn set(&self, x: usize) { - self.0.store(x, Ordering::SeqCst) - } -} - -pub fn with_counter_waker_context(f: F) -> R - where F: FnOnce(&mut task::Context, &Arc) -> R -{ - impl Wake for CounterWaker { - fn wake(arc_self: &Arc) { - arc_self.0.fetch_add(1, Ordering::SeqCst); - } - } - - let counter_arc = Arc::new(CounterWaker(AtomicUsize::new(0))); - let counter_waker = unsafe { task::local_waker_ref(&counter_arc) }; - let exec = &mut PanicExecutor; - - let cx = &mut task::Context::new(&counter_waker, exec); - f(cx, &counter_arc) -} diff --git a/futures/tests/support/mod.rs b/futures/tests/support/mod.rs index 1a501c089e..db145aa71e 100644 --- a/futures/tests/support/mod.rs +++ b/futures/tests/support/mod.rs @@ -8,17 +8,6 @@ pub use self::delayed::{delayed, Delayed}; mod run_in_background; pub use self::run_in_background::RunInBackgroundExt; -mod counter_waker_context; -pub use self::counter_waker_context::with_counter_waker_context; - -mod noop_waker_context; -pub use self::noop_waker_context::with_noop_waker_context; - -mod panic_executor; - -mod panic_waker_context; -pub use self::panic_waker_context::with_panic_waker_context; - // pub fn f_ok(a: i32) -> FutureResult { Ok(a).into_future() } // pub fn f_err(a: u32) -> FutureResult { Err(a).into_future() } diff --git a/futures/tests/support/noop_waker_context.rs b/futures/tests/support/noop_waker_context.rs deleted file mode 100644 index 673e5962a5..0000000000 --- a/futures/tests/support/noop_waker_context.rs +++ /dev/null @@ -1,22 +0,0 @@ -use std::sync::Arc; - -use futures::task::Wake; -use futures::prelude::*; - -use super::panic_executor::PanicExecutor; - -pub fn with_noop_waker_context(f: F) -> R - where F: FnOnce(&mut task::Context) -> R -{ - struct NoopWake; - - impl Wake for NoopWake { - fn wake(_: &Arc) {} - } - - let noop_waker = unsafe { task::local_waker(Arc::new(NoopWake)) }; - let exec = &mut PanicExecutor; - - let cx = &mut task::Context::new(&noop_waker, exec); - f(cx) -} diff --git a/futures/tests/support/panic_executor.rs b/futures/tests/support/panic_executor.rs deleted file mode 100644 index 1d4b503929..0000000000 --- a/futures/tests/support/panic_executor.rs +++ /dev/null @@ -1,10 +0,0 @@ -use futures::future::FutureObj; -use futures::task::{Executor, SpawnObjError}; - -pub struct PanicExecutor; - -impl Executor for PanicExecutor { - fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> { - panic!("should not spawn") - } -} diff --git a/futures/tests/support/panic_waker_context.rs b/futures/tests/support/panic_waker_context.rs deleted file mode 100644 index 3792a5ccad..0000000000 --- a/futures/tests/support/panic_waker_context.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::sync::Arc; - -use futures::task::Wake; -use futures::prelude::*; - -use super::panic_executor::PanicExecutor; - -pub fn with_panic_waker_context(f: F) -> R - where F: FnOnce(&mut task::Context) -> R -{ - struct PanicWake; - - impl Wake for PanicWake { - fn wake(_: &Arc) { - panic!("should not be woken"); - } - } - - let panic_waker = unsafe { task::local_waker(Arc::new(PanicWake)) }; - let exec = &mut PanicExecutor; - - let cx = &mut task::Context::new(&panic_waker, exec); - f(cx) -}