Skip to content

Commit

Permalink
Migrate futures tests to use futures-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Aug 4, 2018
1 parent e76d3b7 commit d7a989c
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 134 deletions.
3 changes: 3 additions & 0 deletions futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
19 changes: 10 additions & 9 deletions futures/tests/abortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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::<wake::Counter, executor::Panic>::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]
Expand Down
8 changes: 2 additions & 6 deletions futures/tests/fuse.rs
Original file line number Diff line number Diff line change
@@ -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::<i32>(2).fuse();
support::with_panic_waker_context(|cx| {
with_context::<wake::Panic, executor::Panic, _, _>(|cx| {
assert!(future.poll_unpin(cx).is_ready());
assert!(future.poll_unpin(cx).is_pending());
})
Expand Down
12 changes: 4 additions & 8 deletions futures/tests/futures_ordered.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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::<wake::Noop, executor::Panic, _, _>(|cx| {
assert!(stream.poll_next_unpin(cx).is_pending());
});

Expand All @@ -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::<wake::Noop, executor::Panic, _, _>(|cx| {
a_tx.send(33).unwrap();
b_tx.send(33).unwrap();
assert!(stream.poll_next_unpin(cx).is_ready());
Expand Down Expand Up @@ -78,7 +74,7 @@ fn queue_never_unblocked() {
Box::new(b_rx.select(c_rx).then(|res| Ok(Box::new(res) as Box<Any+Send>))) as _,
]);
support::with_noop_waker_context(f)(|cx| {
with_context::<wake::Noop, executor::Panic, _, _>(|cx| {
for _ in 0..10 {
assert!(stream.poll_next(cx).unwrap().is_pending());
}
Expand Down
6 changes: 3 additions & 3 deletions futures/tests/futures_unordered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>();
Expand Down Expand Up @@ -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::<wake::Noop, executor::Panic, _, _>(|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))));
Expand Down
9 changes: 4 additions & 5 deletions futures/tests/support/assert.rs
Original file line number Diff line number Diff line change
@@ -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<S: Stream>(stream: PinMut<S>) {
with_noop_waker_context(|cx| {
with_context::<wake::Noop, executor::Panic, _, _>(|cx| {
match stream.poll_next(cx) {
Poll::Ready(_) => panic!("stream is not pending"),
Poll::Pending => {},
Expand All @@ -16,7 +15,7 @@ pub fn assert_stream_pending<S: Stream>(stream: PinMut<S>) {
pub fn assert_stream_next<S: Stream>(stream: PinMut<S>, item: S::Item)
where S::Item: Eq + fmt::Debug
{
with_panic_waker_context(|cx| {
with_context::<wake::Panic, executor::Panic, _, _>(|cx| {
match stream.poll_next(cx) {
Poll::Ready(Some(x)) => assert_eq!(x, item),
Poll::Ready(None) => panic!("stream is at its end"),
Expand All @@ -27,7 +26,7 @@ pub fn assert_stream_next<S: Stream>(stream: PinMut<S>, item: S::Item)

pub fn assert_stream_done<S: Stream>(stream: PinMut<S>)
{
with_panic_waker_context(|cx| {
with_context::<wake::Panic, executor::Panic, _, _>(|cx| {
match stream.poll_next(cx) {
Poll::Ready(Some(_)) => panic!("stream had more elements"),
Poll::Ready(None) => {},
Expand Down
36 changes: 0 additions & 36 deletions futures/tests/support/counter_waker_context.rs

This file was deleted.

11 changes: 0 additions & 11 deletions futures/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32, u32> { Ok(a).into_future() }
// pub fn f_err(a: u32) -> FutureResult<i32, u32> { Err(a).into_future() }
Expand Down
22 changes: 0 additions & 22 deletions futures/tests/support/noop_waker_context.rs

This file was deleted.

10 changes: 0 additions & 10 deletions futures/tests/support/panic_executor.rs

This file was deleted.

24 changes: 0 additions & 24 deletions futures/tests/support/panic_waker_context.rs

This file was deleted.

0 comments on commit d7a989c

Please sign in to comment.