1 | use futures::channel::oneshot; |
2 | use futures::future::{FutureExt, TryFutureExt}; |
3 | use futures_test::future::FutureTestExt; |
4 | use std::sync::mpsc; |
5 | use std::thread; |
6 | |
7 | #[test] |
8 | fn oneshot_send1() { |
9 | let (tx1, rx1) = oneshot::channel::<i32>(); |
10 | let (tx2, rx2) = mpsc::channel(); |
11 | |
12 | let t = thread::spawn(|| tx1.send(1).unwrap()); |
13 | rx1.map_ok(move |x| tx2.send(x)).run_in_background(); |
14 | assert_eq!(1, rx2.recv().unwrap()); |
15 | t.join().unwrap(); |
16 | } |
17 | |
18 | #[test] |
19 | fn oneshot_send2() { |
20 | let (tx1, rx1) = oneshot::channel::<i32>(); |
21 | let (tx2, rx2) = mpsc::channel(); |
22 | |
23 | thread::spawn(|| tx1.send(1).unwrap()).join().unwrap(); |
24 | rx1.map_ok(move |x| tx2.send(x).unwrap()).run_in_background(); |
25 | assert_eq!(1, rx2.recv().unwrap()); |
26 | } |
27 | |
28 | #[test] |
29 | fn oneshot_send3() { |
30 | let (tx1, rx1) = oneshot::channel::<i32>(); |
31 | let (tx2, rx2) = mpsc::channel(); |
32 | |
33 | rx1.map_ok(move |x| tx2.send(x).unwrap()).run_in_background(); |
34 | thread::spawn(|| tx1.send(1).unwrap()).join().unwrap(); |
35 | assert_eq!(1, rx2.recv().unwrap()); |
36 | } |
37 | |
38 | #[test] |
39 | fn oneshot_drop_tx1() { |
40 | let (tx1, rx1) = oneshot::channel::<i32>(); |
41 | let (tx2, rx2) = mpsc::channel(); |
42 | |
43 | drop(tx1); |
44 | rx1.map(move |result| tx2.send(result).unwrap()).run_in_background(); |
45 | |
46 | assert_eq!(Err(oneshot::Canceled), rx2.recv().unwrap()); |
47 | } |
48 | |
49 | #[test] |
50 | fn oneshot_drop_tx2() { |
51 | let (tx1, rx1) = oneshot::channel::<i32>(); |
52 | let (tx2, rx2) = mpsc::channel(); |
53 | |
54 | let t = thread::spawn(|| drop(tx1)); |
55 | rx1.map(move |result| tx2.send(result).unwrap()).run_in_background(); |
56 | t.join().unwrap(); |
57 | |
58 | assert_eq!(Err(oneshot::Canceled), rx2.recv().unwrap()); |
59 | } |
60 | |
61 | #[test] |
62 | fn oneshot_drop_rx() { |
63 | let (tx, rx) = oneshot::channel::<i32>(); |
64 | drop(rx); |
65 | assert_eq!(Err(2), tx.send(2)); |
66 | } |
67 | |
68 | #[test] |
69 | fn oneshot_debug() { |
70 | let (tx, rx) = oneshot::channel::<i32>(); |
71 | assert_eq!(format!("{:?}" , tx), "Sender { complete: false }" ); |
72 | assert_eq!(format!("{:?}" , rx), "Receiver { complete: false }" ); |
73 | drop(rx); |
74 | assert_eq!(format!("{:?}" , tx), "Sender { complete: true }" ); |
75 | let (tx, rx) = oneshot::channel::<i32>(); |
76 | drop(tx); |
77 | assert_eq!(format!("{:?}" , rx), "Receiver { complete: true }" ); |
78 | } |
79 | |