Initial commit
diff --git a/tests/basic.rs b/tests/basic.rs
new file mode 100644
index 0000000..b9e181b
--- /dev/null
+++ b/tests/basic.rs
@@ -0,0 +1,314 @@
+#![feature(async_await)]
+
+use std::future::Future;
+use std::pin::Pin;
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::task::{Context, Poll};
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use crossbeam::channel;
+use futures::future;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, POLL, DROP)`
+//
+// The future `f` always returns `Poll::Ready`.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! future {
+ ($name:pat, $poll:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Fut(Box<i32>);
+
+ impl Future for Fut {
+ type Output = Box<i32>;
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ $poll.fetch_add(1);
+ Poll::Ready(Box::new(0))
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ Fut(Box::new(0))
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, SCHED, DROP)`
+//
+// The schedule function `s` does nothing.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! schedule {
+ ($name:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ move |_task| {
+ &guard;
+ $sched.fetch_add(1);
+ }
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+#[test]
+fn cancel_and_drop_handle() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ task.cancel();
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ drop(task);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn run_and_drop_handle() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn drop_handle_and_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn cancel_and_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn run_and_cancel() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn schedule() {
+ let (s, r) = channel::unbounded();
+ let schedule = move |t| s.send(t).unwrap();
+ let (task, _handle) = async_task::spawn(
+ future::poll_fn(|_| Poll::<()>::Pending),
+ schedule,
+ Box::new(0),
+ );
+
+ assert!(r.is_empty());
+ task.schedule();
+
+ let task = r.recv().unwrap();
+ assert!(r.is_empty());
+ task.schedule();
+
+ let task = r.recv().unwrap();
+ assert!(r.is_empty());
+ task.schedule();
+
+ r.recv().unwrap();
+}
+
+#[test]
+fn tag() {
+ let (s, r) = channel::unbounded();
+ let schedule = move |t| s.send(t).unwrap();
+ let (task, handle) = async_task::spawn(
+ future::poll_fn(|_| Poll::<()>::Pending),
+ schedule,
+ AtomicUsize::new(7),
+ );
+
+ assert!(r.is_empty());
+ task.schedule();
+
+ let task = r.recv().unwrap();
+ assert!(r.is_empty());
+ handle.tag().fetch_add(1, Ordering::SeqCst);
+ task.schedule();
+
+ let task = r.recv().unwrap();
+ assert_eq!(task.tag().load(Ordering::SeqCst), 8);
+ assert!(r.is_empty());
+ task.schedule();
+
+ r.recv().unwrap();
+}
+
+#[test]
+fn schedule_counter() {
+ let (s, r) = channel::unbounded();
+ let schedule = move |t: Task<AtomicUsize>| {
+ t.tag().fetch_add(1, Ordering::SeqCst);
+ s.send(t).unwrap();
+ };
+ let (task, handle) = async_task::spawn(
+ future::poll_fn(|_| Poll::<()>::Pending),
+ schedule,
+ AtomicUsize::new(0),
+ );
+ task.schedule();
+
+ assert_eq!(handle.tag().load(Ordering::SeqCst), 1);
+ r.recv().unwrap().schedule();
+
+ assert_eq!(handle.tag().load(Ordering::SeqCst), 2);
+ r.recv().unwrap().schedule();
+
+ assert_eq!(handle.tag().load(Ordering::SeqCst), 3);
+ r.recv().unwrap();
+}
diff --git a/tests/join.rs b/tests/join.rs
new file mode 100644
index 0000000..e082939
--- /dev/null
+++ b/tests/join.rs
@@ -0,0 +1,454 @@
+#![feature(async_await)]
+
+use std::cell::Cell;
+use std::future::Future;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use futures::executor::block_on;
+use futures::future;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, POLL, DROP_F, DROP_O)`
+//
+// The future `f` outputs `Poll::Ready`.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP_F` is incremented.
+// When the output gets dropped, `DROP_O` is incremented.
+macro_rules! future {
+ ($name:pat, $poll:ident, $drop_f:ident, $drop_o:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop_f: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop_o: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Fut(Box<i32>);
+
+ impl Future for Fut {
+ type Output = Out;
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ $poll.fetch_add(1);
+ Poll::Ready(Out(Box::new(0)))
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop_f.fetch_add(1);
+ }
+ }
+
+ struct Out(Box<i32>);
+
+ impl Drop for Out {
+ fn drop(&mut self) {
+ $drop_o.fetch_add(1);
+ }
+ }
+
+ Fut(Box::new(0))
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, SCHED, DROP)`
+//
+// The schedule function `s` does nothing.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! schedule {
+ ($name:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ move |task: Task<_>| {
+ &guard;
+ task.schedule();
+ $sched.fetch_add(1);
+ }
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn cancel_and_join() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ assert_eq!(DROP_O.load(), 0);
+
+ task.cancel();
+ drop(task);
+ assert_eq!(DROP_O.load(), 0);
+
+ assert!(block_on(handle).is_none());
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 0);
+}
+
+#[test]
+fn run_and_join() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ assert_eq!(DROP_O.load(), 0);
+
+ task.run();
+ assert_eq!(DROP_O.load(), 0);
+
+ assert!(block_on(handle).is_some());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+}
+
+#[test]
+fn drop_handle_and_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ assert_eq!(DROP_O.load(), 0);
+
+ drop(handle);
+ assert_eq!(DROP_O.load(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+}
+
+#[test]
+fn join_twice() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ assert_eq!(DROP_O.load(), 0);
+
+ task.run();
+ assert_eq!(DROP_O.load(), 0);
+
+ assert!(block_on(&mut handle).is_some());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 1);
+
+ assert!(block_on(&mut handle).is_none());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 1);
+
+ drop(handle);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn join_and_cancel() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ thread::sleep(ms(100));
+
+ task.cancel();
+ drop(task);
+
+ thread::sleep(ms(200));
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_O.load(), 0);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ assert!(block_on(handle).is_none());
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_O.load(), 0);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn join_and_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ thread::sleep(ms(200));
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ assert!(block_on(handle).is_some());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn try_join_and_run_and_join() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ thread::sleep(ms(200));
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ assert!(block_on(handle).is_some());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn try_join_and_cancel_and_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ thread::sleep(ms(200));
+
+ task.run();
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn try_join_and_run_and_cancel() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ thread::sleep(ms(200));
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ });
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ thread::sleep(ms(400));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn await_output() {
+ struct Fut<T>(Cell<Option<T>>);
+
+ impl<T> Fut<T> {
+ fn new(t: T) -> Fut<T> {
+ Fut(Cell::new(Some(t)))
+ }
+ }
+
+ impl<T> Future for Fut<T> {
+ type Output = T;
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ Poll::Ready(self.0.take().unwrap())
+ }
+ }
+
+ for i in 0..10 {
+ let (task, handle) = async_task::spawn(Fut::new(i), drop, Box::new(0));
+ task.run();
+ assert_eq!(block_on(handle), Some(i));
+ }
+
+ for i in 0..10 {
+ let (task, handle) = async_task::spawn(Fut::new(vec![7; i]), drop, Box::new(0));
+ task.run();
+ assert_eq!(block_on(handle), Some(vec![7; i]));
+ }
+
+ let (task, handle) = async_task::spawn(Fut::new("foo".to_string()), drop, Box::new(0));
+ task.run();
+ assert_eq!(block_on(handle), Some("foo".to_string()));
+}
diff --git a/tests/panic.rs b/tests/panic.rs
new file mode 100644
index 0000000..68058a2
--- /dev/null
+++ b/tests/panic.rs
@@ -0,0 +1,288 @@
+#![feature(async_await)]
+
+use std::future::Future;
+use std::panic::catch_unwind;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use futures::executor::block_on;
+use futures::future;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, POLL, DROP)`
+//
+// The future `f` sleeps for 200 ms and then panics.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! future {
+ ($name:pat, $poll:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Fut(Box<i32>);
+
+ impl Future for Fut {
+ type Output = ();
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ $poll.fetch_add(1);
+ thread::sleep(ms(200));
+ panic!()
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ Fut(Box::new(0))
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, SCHED, DROP)`
+//
+// The schedule function `s` does nothing.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! schedule {
+ ($name:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ move |_task: Task<_>| {
+ &guard;
+ $sched.fetch_add(1);
+ }
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn cancel_during_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn run_and_join() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ assert!(block_on(handle).is_none());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn try_join_and_run_and_join() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 0);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+
+ assert!(block_on(handle).is_none());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn join_during_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ assert!(block_on(handle).is_none());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn try_join_during_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ drop(handle);
+ })
+ .unwrap();
+}
+
+#[test]
+fn drop_handle_during_run() {
+ future!(f, POLL, DROP_F);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ })
+ .unwrap();
+}
diff --git a/tests/ready.rs b/tests/ready.rs
new file mode 100644
index 0000000..ecca328
--- /dev/null
+++ b/tests/ready.rs
@@ -0,0 +1,265 @@
+#![feature(async_await)]
+
+use std::future::Future;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use futures::executor::block_on;
+use futures::future;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, POLL, DROP_F, DROP_O)`
+//
+// The future `f` sleeps for 200 ms and outputs `Poll::Ready`.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP_F` is incremented.
+// When the output gets dropped, `DROP_O` is incremented.
+macro_rules! future {
+ ($name:pat, $poll:ident, $drop_f:ident, $drop_o:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop_f: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop_o: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Fut(Box<i32>);
+
+ impl Future for Fut {
+ type Output = Out;
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ $poll.fetch_add(1);
+ thread::sleep(ms(200));
+ Poll::Ready(Out(Box::new(0)))
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop_f.fetch_add(1);
+ }
+ }
+
+ struct Out(Box<i32>);
+
+ impl Drop for Out {
+ fn drop(&mut self) {
+ $drop_o.fetch_add(1);
+ }
+ }
+
+ Fut(Box::new(0))
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, SCHED, DROP)`
+//
+// The schedule function `s` does nothing.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+macro_rules! schedule {
+ ($name:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let $name = {
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ move |_task: Task<_>| {
+ &guard;
+ $sched.fetch_add(1);
+ }
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn cancel_during_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 1);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn join_during_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ assert!(block_on(handle).is_some());
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+
+ thread::sleep(ms(100));
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ })
+ .unwrap();
+}
+
+#[test]
+fn try_join_during_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, mut handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ block_on(future::select(&mut handle, future::ready(())));
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+ drop(handle);
+ })
+ .unwrap();
+}
+
+#[test]
+fn drop_handle_during_run() {
+ future!(f, POLL, DROP_F, DROP_O);
+ schedule!(s, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(DROP_O.load(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ drop(handle);
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(DROP_O.load(), 0);
+ })
+ .unwrap();
+}
diff --git a/tests/waker_panic.rs b/tests/waker_panic.rs
new file mode 100644
index 0000000..a683f26
--- /dev/null
+++ b/tests/waker_panic.rs
@@ -0,0 +1,357 @@
+#![feature(async_await)]
+
+use std::cell::Cell;
+use std::future::Future;
+use std::panic::catch_unwind;
+use std::pin::Pin;
+use std::task::Waker;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use crossbeam::channel;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, waker, POLL, DROP)`
+//
+// The future `f` always sleeps for 200 ms, and panics the second time it is polled.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Every time the future is run, it stores the waker into a global variable.
+// This waker can be extracted using the `waker` function.
+macro_rules! future {
+ ($name:pat, $waker:pat, $poll:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ static ref WAKER: AtomicCell<Option<Waker>> = AtomicCell::new(None);
+ }
+
+ let ($name, $waker) = {
+ struct Fut(Cell<bool>, Box<i32>);
+
+ impl Future for Fut {
+ type Output = ();
+
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ WAKER.store(Some(cx.waker().clone()));
+ $poll.fetch_add(1);
+ thread::sleep(ms(200));
+
+ if self.0.get() {
+ panic!()
+ } else {
+ self.0.set(true);
+ Poll::Pending
+ }
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ (Fut(Cell::new(false), Box::new(0)), || {
+ WAKER.swap(None).unwrap()
+ })
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, chan, SCHED, DROP)`
+//
+// The schedule function `s` pushes the task into `chan`.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Receiver `chan` extracts the task when it is scheduled.
+macro_rules! schedule {
+ ($name:pat, $chan:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($name, $chan) = {
+ let (s, r) = channel::unbounded();
+
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ let sched = move |task: Task<_>| {
+ &guard;
+ $sched.fetch_add(1);
+ s.send(task).unwrap();
+ };
+
+ (sched, r)
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn wake_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ w.wake();
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn cancel_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn wake_and_cancel_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ w.wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn cancel_and_wake_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ assert!(catch_unwind(|| task.run()).is_err());
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ w.wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
diff --git a/tests/waker_pending.rs b/tests/waker_pending.rs
new file mode 100644
index 0000000..547ff7a
--- /dev/null
+++ b/tests/waker_pending.rs
@@ -0,0 +1,348 @@
+#![feature(async_await)]
+
+use std::future::Future;
+use std::pin::Pin;
+use std::task::Waker;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use crossbeam::channel;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, waker, POLL, DROP)`
+//
+// The future `f` always sleeps for 200 ms and returns `Poll::Pending`.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Every time the future is run, it stores the waker into a global variable.
+// This waker can be extracted using the `waker` function.
+macro_rules! future {
+ ($name:pat, $waker:pat, $poll:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ static ref WAKER: AtomicCell<Option<Waker>> = AtomicCell::new(None);
+ }
+
+ let ($name, $waker) = {
+ struct Fut(Box<i32>);
+
+ impl Future for Fut {
+ type Output = ();
+
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ WAKER.store(Some(cx.waker().clone()));
+ $poll.fetch_add(1);
+ thread::sleep(ms(200));
+ Poll::Pending
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ (Fut(Box::new(0)), || WAKER.swap(None).unwrap())
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, chan, SCHED, DROP)`
+//
+// The schedule function `s` pushes the task into `chan`.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Receiver `chan` extracts the task when it is scheduled.
+macro_rules! schedule {
+ ($name:pat, $chan:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($name, $chan) = {
+ let (s, r) = channel::unbounded();
+
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ let sched = move |task: Task<_>| {
+ &guard;
+ $sched.fetch_add(1);
+ s.send(task).unwrap();
+ };
+
+ (sched, r)
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn wake_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, _handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 2);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 1);
+ });
+
+ thread::sleep(ms(100));
+
+ w.wake_by_ref();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 2);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 1);
+ })
+ .unwrap();
+
+ chan.recv().unwrap();
+ drop(waker());
+}
+
+#[test]
+fn cancel_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn wake_and_cancel_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ w.wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
+
+#[test]
+fn cancel_and_wake_during_run() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, handle, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ w.wake_by_ref();
+ let task = chan.recv().unwrap();
+
+ crossbeam::scope(|scope| {
+ scope.spawn(|_| {
+ task.run();
+ drop(waker());
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ });
+
+ thread::sleep(ms(100));
+
+ handle.cancel();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(handle);
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ w.wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ thread::sleep(ms(200));
+
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+ })
+ .unwrap();
+}
diff --git a/tests/waker_ready.rs b/tests/waker_ready.rs
new file mode 100644
index 0000000..e64cc55
--- /dev/null
+++ b/tests/waker_ready.rs
@@ -0,0 +1,328 @@
+#![feature(async_await)]
+
+use std::cell::Cell;
+use std::future::Future;
+use std::pin::Pin;
+use std::task::Waker;
+use std::task::{Context, Poll};
+use std::thread;
+use std::time::Duration;
+
+use async_task::Task;
+use crossbeam::atomic::AtomicCell;
+use crossbeam::channel;
+use lazy_static::lazy_static;
+
+// Creates a future with event counters.
+//
+// Usage: `future!(f, waker, POLL, DROP)`
+//
+// The future `f` always sleeps for 200 ms, and returns `Poll::Ready` the second time it is polled.
+// When it gets polled, `POLL` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Every time the future is run, it stores the waker into a global variable.
+// This waker can be extracted using the `waker` function.
+macro_rules! future {
+ ($name:pat, $waker:pat, $poll:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $poll: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ static ref WAKER: AtomicCell<Option<Waker>> = AtomicCell::new(None);
+ }
+
+ let ($name, $waker) = {
+ struct Fut(Cell<bool>, Box<i32>);
+
+ impl Future for Fut {
+ type Output = Box<i32>;
+
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ WAKER.store(Some(cx.waker().clone()));
+ $poll.fetch_add(1);
+ thread::sleep(ms(200));
+
+ if self.0.get() {
+ Poll::Ready(Box::new(0))
+ } else {
+ self.0.set(true);
+ Poll::Pending
+ }
+ }
+ }
+
+ impl Drop for Fut {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ (Fut(Cell::new(false), Box::new(0)), || {
+ WAKER.swap(None).unwrap()
+ })
+ };
+ };
+}
+
+// Creates a schedule function with event counters.
+//
+// Usage: `schedule!(s, chan, SCHED, DROP)`
+//
+// The schedule function `s` pushes the task into `chan`.
+// When it gets invoked, `SCHED` is incremented.
+// When it gets dropped, `DROP` is incremented.
+//
+// Receiver `chan` extracts the task when it is scheduled.
+macro_rules! schedule {
+ ($name:pat, $chan:pat, $sched:ident, $drop:ident) => {
+ lazy_static! {
+ static ref $sched: AtomicCell<usize> = AtomicCell::new(0);
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($name, $chan) = {
+ let (s, r) = channel::unbounded();
+
+ struct Guard(Box<i32>);
+
+ impl Drop for Guard {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ let guard = Guard(Box::new(0));
+ let sched = move |task: Task<_>| {
+ &guard;
+ $sched.fetch_add(1);
+ s.send(task).unwrap();
+ };
+
+ (sched, r)
+ };
+ };
+}
+
+// Creates a task with event counters.
+//
+// Usage: `task!(task, handle f, s, DROP)`
+//
+// A task with future `f` and schedule function `s` is created.
+// The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively.
+// When the tag inside the task gets dropped, `DROP` is incremented.
+macro_rules! task {
+ ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => {
+ lazy_static! {
+ static ref $drop: AtomicCell<usize> = AtomicCell::new(0);
+ }
+
+ let ($task, $handle) = {
+ struct Tag(Box<i32>);
+
+ impl Drop for Tag {
+ fn drop(&mut self) {
+ $drop.fetch_add(1);
+ }
+ }
+
+ async_task::spawn($future, $schedule, Tag(Box::new(0)))
+ };
+ };
+}
+
+fn ms(ms: u64) -> Duration {
+ Duration::from_millis(ms)
+}
+
+#[test]
+fn wake() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(mut task, _, f, s, DROP_D);
+
+ assert!(chan.is_empty());
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ waker().wake();
+ task = chan.recv().unwrap();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ waker().wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+}
+
+#[test]
+fn wake_by_ref() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(mut task, _, f, s, DROP_D);
+
+ assert!(chan.is_empty());
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ waker().wake_by_ref();
+ task = chan.recv().unwrap();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ task.run();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ waker().wake_by_ref();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+}
+
+#[test]
+fn clone() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(mut task, _, f, s, DROP_D);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ let w2 = waker().clone();
+ let w3 = w2.clone();
+ let w4 = w3.clone();
+ w4.wake();
+
+ task = chan.recv().unwrap();
+ task.run();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ w3.wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ drop(w2);
+ drop(waker());
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+}
+
+#[test]
+fn wake_cancelled() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, _, f, s, DROP_D);
+
+ task.run();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ let w = waker();
+
+ w.wake_by_ref();
+ chan.recv().unwrap().cancel();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ w.wake();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+}
+
+#[test]
+fn wake_completed() {
+ future!(f, waker, POLL, DROP_F);
+ schedule!(s, chan, SCHEDULE, DROP_S);
+ task!(task, _, f, s, DROP_D);
+
+ task.run();
+ let w = waker();
+ assert_eq!(POLL.load(), 1);
+ assert_eq!(SCHEDULE.load(), 0);
+ assert_eq!(DROP_F.load(), 0);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ w.wake();
+ chan.recv().unwrap().run();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 0);
+ assert_eq!(DROP_D.load(), 0);
+ assert_eq!(chan.len(), 0);
+
+ waker().wake();
+ assert_eq!(POLL.load(), 2);
+ assert_eq!(SCHEDULE.load(), 1);
+ assert_eq!(DROP_F.load(), 1);
+ assert_eq!(DROP_S.load(), 1);
+ assert_eq!(DROP_D.load(), 1);
+ assert_eq!(chan.len(), 0);
+}