1 | use futures::channel::oneshot; |
2 | use futures::executor::block_on; |
3 | use futures::future::{abortable, Aborted, FutureExt}; |
4 | use futures::task::{Context, Poll}; |
5 | use futures_test::task::new_count_waker; |
6 | |
7 | #[test] |
8 | fn abortable_works() { |
9 | let (_tx, a_rx) = oneshot::channel::<()>(); |
10 | let (abortable_rx, abort_handle) = abortable(a_rx); |
11 | |
12 | abort_handle.abort(); |
13 | assert!(abortable_rx.is_aborted()); |
14 | assert_eq!(Err(Aborted), block_on(abortable_rx)); |
15 | } |
16 | |
17 | #[test] |
18 | fn abortable_awakens() { |
19 | let (_tx, a_rx) = oneshot::channel::<()>(); |
20 | let (mut abortable_rx, abort_handle) = abortable(a_rx); |
21 | |
22 | let (waker, counter) = new_count_waker(); |
23 | let mut cx = Context::from_waker(&waker); |
24 | |
25 | assert_eq!(counter, 0); |
26 | assert_eq!(Poll::Pending, abortable_rx.poll_unpin(&mut cx)); |
27 | assert_eq!(counter, 0); |
28 | |
29 | abort_handle.abort(); |
30 | assert_eq!(counter, 1); |
31 | assert!(abortable_rx.is_aborted()); |
32 | assert_eq!(Poll::Ready(Err(Aborted)), abortable_rx.poll_unpin(&mut cx)); |
33 | } |
34 | |
35 | #[test] |
36 | fn abortable_resolves() { |
37 | let (tx, a_rx) = oneshot::channel::<()>(); |
38 | let (abortable_rx, _abort_handle) = abortable(a_rx); |
39 | |
40 | tx.send(()).unwrap(); |
41 | |
42 | assert!(!abortable_rx.is_aborted()); |
43 | assert_eq!(Ok(Ok(())), block_on(abortable_rx)); |
44 | } |
45 | |