1 #![cfg(not(soong))]
2 #![cfg(all(feature = "async", not(loom)))]
3 
4 use core::mem;
5 use core::time::Duration;
6 
7 mod helpers;
8 use helpers::DropCounter;
9 
10 #[tokio::test]
send_before_await_tokio()11 async fn send_before_await_tokio() {
12     let (sender, receiver) = oneshot::channel();
13     assert!(sender.send(19i128).is_ok());
14     assert_eq!(receiver.await, Ok(19i128));
15 }
16 
17 #[async_std::test]
send_before_await_async_std()18 async fn send_before_await_async_std() {
19     let (sender, receiver) = oneshot::channel();
20     assert!(sender.send(19i128).is_ok());
21     assert_eq!(receiver.await, Ok(19i128));
22 }
23 
24 #[tokio::test]
await_with_dropped_sender_tokio()25 async fn await_with_dropped_sender_tokio() {
26     let (sender, receiver) = oneshot::channel::<u128>();
27     mem::drop(sender);
28     receiver.await.unwrap_err();
29 }
30 
31 #[async_std::test]
await_with_dropped_sender_async_std()32 async fn await_with_dropped_sender_async_std() {
33     let (sender, receiver) = oneshot::channel::<u128>();
34     mem::drop(sender);
35     receiver.await.unwrap_err();
36 }
37 
38 #[tokio::test]
await_before_send_tokio()39 async fn await_before_send_tokio() {
40     let (sender, receiver) = oneshot::channel();
41     let (message, counter) = DropCounter::new(79u128);
42     let t = tokio::spawn(async move {
43         tokio::time::sleep(Duration::from_millis(10)).await;
44         sender.send(message)
45     });
46     let returned_message = receiver.await.unwrap();
47     assert_eq!(counter.count(), 0);
48     assert_eq!(*returned_message.value(), 79u128);
49     mem::drop(returned_message);
50     assert_eq!(counter.count(), 1);
51     t.await.unwrap().unwrap();
52 }
53 
54 #[async_std::test]
await_before_send_async_std()55 async fn await_before_send_async_std() {
56     let (sender, receiver) = oneshot::channel();
57     let (message, counter) = DropCounter::new(79u128);
58     let t = async_std::task::spawn(async move {
59         async_std::task::sleep(Duration::from_millis(10)).await;
60         sender.send(message)
61     });
62     let returned_message = receiver.await.unwrap();
63     assert_eq!(counter.count(), 0);
64     assert_eq!(*returned_message.value(), 79u128);
65     mem::drop(returned_message);
66     assert_eq!(counter.count(), 1);
67     t.await.unwrap();
68 }
69 
70 #[tokio::test]
await_before_send_then_drop_sender_tokio()71 async fn await_before_send_then_drop_sender_tokio() {
72     let (sender, receiver) = oneshot::channel::<u128>();
73     let t = tokio::spawn(async {
74         tokio::time::sleep(Duration::from_millis(10)).await;
75         mem::drop(sender);
76     });
77     assert!(receiver.await.is_err());
78     t.await.unwrap();
79 }
80 
81 #[async_std::test]
await_before_send_then_drop_sender_async_std()82 async fn await_before_send_then_drop_sender_async_std() {
83     let (sender, receiver) = oneshot::channel::<u128>();
84     let t = async_std::task::spawn(async {
85         async_std::task::sleep(Duration::from_millis(10)).await;
86         mem::drop(sender);
87     });
88     assert!(receiver.await.is_err());
89     t.await;
90 }
91 
92 // Tests that the Receiver handles being used synchronously even after being polled
93 #[tokio::test]
poll_future_and_then_try_recv()94 async fn poll_future_and_then_try_recv() {
95     use core::future::Future;
96     use core::pin::Pin;
97     use core::task::{self, Poll};
98 
99     struct StupidReceiverFuture(oneshot::Receiver<()>);
100 
101     impl Future for StupidReceiverFuture {
102         type Output = Result<(), oneshot::RecvError>;
103 
104         fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
105             let poll_result = Future::poll(Pin::new(&mut self.0), cx);
106             self.0.try_recv().expect_err("Should never be a message");
107             poll_result
108         }
109     }
110 
111     let (sender, receiver) = oneshot::channel();
112     let t = tokio::spawn(async {
113         tokio::time::sleep(Duration::from_millis(20)).await;
114         mem::drop(sender);
115     });
116     StupidReceiverFuture(receiver).await.unwrap_err();
117     t.await.unwrap();
118 }
119 
120 #[tokio::test]
poll_receiver_then_drop_it()121 async fn poll_receiver_then_drop_it() {
122     let (sender, receiver) = oneshot::channel::<()>();
123     // This will poll the receiver and then give up after 100 ms.
124     tokio::time::timeout(Duration::from_millis(100), receiver)
125         .await
126         .unwrap_err();
127     // Make sure the receiver has been dropped by the runtime.
128     assert!(sender.send(()).is_err());
129 }
130