blob: 4b1bf3b70a75f8740f4d0a467ad6bdf951207830 [file] [log] [blame]
#![feature(pin, arbitrary_self_types, futures_api)]
use futures::channel::oneshot;
use futures::executor::block_on;
use futures::future::{abortable, Aborted, FutureExt};
use futures::task::Poll;
use futures_test::task::{panic_context, WakeCounter};
#[test]
fn abortable_works() {
let (_tx, a_rx) = oneshot::channel::<()>();
let (abortable_rx, abort_handle) = abortable(a_rx);
abort_handle.abort();
assert_eq!(Err(Aborted), block_on(abortable_rx));
}
#[test]
fn abortable_awakens() {
let (_tx, a_rx) = oneshot::channel::<()>();
let (mut abortable_rx, abort_handle) = abortable(a_rx);
let wake_counter = WakeCounter::new();
let mut cx = panic_context();
let cx = &mut cx.with_waker(wake_counter.local_waker());
assert_eq!(0, wake_counter.count());
assert_eq!(Poll::Pending, abortable_rx.poll_unpin(cx));
assert_eq!(0, wake_counter.count());
abort_handle.abort();
assert_eq!(1, wake_counter.count());
assert_eq!(Poll::Ready(Err(Aborted)), abortable_rx.poll_unpin(cx));
}
#[test]
fn abortable_resolves() {
let (tx, a_rx) = oneshot::channel::<()>();
let (abortable_rx, _abort_handle) = abortable(a_rx);
tx.send(()).unwrap();
assert_eq!(Ok(Ok(())), block_on(abortable_rx));
}