1 #![allow(clippy::blacklisted_name)]
2 use tokio::sync::oneshot;
3 use tokio_test::{assert_pending, assert_ready, task};
4 
5 #[tokio::test]
sync_one_lit_expr_comma()6 async fn sync_one_lit_expr_comma() {
7     let foo = tokio::try_join!(async { ok(1) },);
8 
9     assert_eq!(foo, Ok((1,)));
10 }
11 
12 #[tokio::test]
sync_one_lit_expr_no_comma()13 async fn sync_one_lit_expr_no_comma() {
14     let foo = tokio::try_join!(async { ok(1) });
15 
16     assert_eq!(foo, Ok((1,)));
17 }
18 
19 #[tokio::test]
sync_two_lit_expr_comma()20 async fn sync_two_lit_expr_comma() {
21     let foo = tokio::try_join!(async { ok(1) }, async { ok(2) },);
22 
23     assert_eq!(foo, Ok((1, 2)));
24 }
25 
26 #[tokio::test]
sync_two_lit_expr_no_comma()27 async fn sync_two_lit_expr_no_comma() {
28     let foo = tokio::try_join!(async { ok(1) }, async { ok(2) });
29 
30     assert_eq!(foo, Ok((1, 2)));
31 }
32 
33 #[tokio::test]
two_await()34 async fn two_await() {
35     let (tx1, rx1) = oneshot::channel::<&str>();
36     let (tx2, rx2) = oneshot::channel::<u32>();
37 
38     let mut join =
39         task::spawn(async { tokio::try_join!(async { rx1.await }, async { rx2.await }) });
40 
41     assert_pending!(join.poll());
42 
43     tx2.send(123).unwrap();
44     assert!(join.is_woken());
45     assert_pending!(join.poll());
46 
47     tx1.send("hello").unwrap();
48     assert!(join.is_woken());
49     let res: Result<(&str, u32), _> = assert_ready!(join.poll());
50 
51     assert_eq!(Ok(("hello", 123)), res);
52 }
53 
54 #[tokio::test]
err_abort_early()55 async fn err_abort_early() {
56     let (tx1, rx1) = oneshot::channel::<&str>();
57     let (tx2, rx2) = oneshot::channel::<u32>();
58     let (_tx3, rx3) = oneshot::channel::<u32>();
59 
60     let mut join = task::spawn(async {
61         tokio::try_join!(async { rx1.await }, async { rx2.await }, async {
62             rx3.await
63         })
64     });
65 
66     assert_pending!(join.poll());
67 
68     tx2.send(123).unwrap();
69     assert!(join.is_woken());
70     assert_pending!(join.poll());
71 
72     drop(tx1);
73     assert!(join.is_woken());
74 
75     let res = assert_ready!(join.poll());
76 
77     assert!(res.is_err());
78 }
79 
80 #[test]
join_size()81 fn join_size() {
82     use futures::future;
83     use std::mem;
84 
85     let fut = async {
86         let ready = future::ready(ok(0i32));
87         tokio::try_join!(ready)
88     };
89     assert_eq!(mem::size_of_val(&fut), 16);
90 
91     let fut = async {
92         let ready1 = future::ready(ok(0i32));
93         let ready2 = future::ready(ok(0i32));
94         tokio::try_join!(ready1, ready2)
95     };
96     assert_eq!(mem::size_of_val(&fut), 28);
97 }
98 
ok<T>(val: T) -> Result<T, ()>99 fn ok<T>(val: T) -> Result<T, ()> {
100     Ok(val)
101 }
102