1 | #![cfg (feature = "macros" )] |
2 | |
3 | use futures::channel::oneshot; |
4 | use futures::executor::block_on; |
5 | use std::thread; |
6 | |
7 | #[cfg_attr (target_os = "wasi" , ignore = "WASI: std::thread::spawn not supported" )] |
8 | #[test] |
9 | fn join_with_select() { |
10 | block_on(async { |
11 | let (tx1, mut rx1) = oneshot::channel::<i32>(); |
12 | let (tx2, mut rx2) = oneshot::channel::<i32>(); |
13 | |
14 | thread::spawn(move || { |
15 | tx1.send(123).unwrap(); |
16 | tx2.send(456).unwrap(); |
17 | }); |
18 | |
19 | let mut a = None; |
20 | let mut b = None; |
21 | |
22 | while a.is_none() || b.is_none() { |
23 | tokio::select! { |
24 | v1 = (&mut rx1), if a.is_none() => a = Some(v1.unwrap()), |
25 | v2 = (&mut rx2), if b.is_none() => b = Some(v2.unwrap()), |
26 | } |
27 | } |
28 | |
29 | let (a, b) = (a.unwrap(), b.unwrap()); |
30 | |
31 | assert_eq!(a, 123); |
32 | assert_eq!(b, 456); |
33 | }); |
34 | } |
35 | |