1 use futures::executor::block_on;
2 use futures::future::{join_all, ready, Future, JoinAll};
3 use std::fmt::Debug;
4
assert_done<T, F>(actual_fut: F, expected: T) where T: PartialEq + Debug, F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,5 fn assert_done<T, F>(actual_fut: F, expected: T)
6 where
7 T: PartialEq + Debug,
8 F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,
9 {
10 let output = block_on(actual_fut());
11 assert_eq!(output, expected);
12 }
13
14 #[test]
collect_collects()15 fn collect_collects() {
16 assert_done(|| Box::new(join_all(vec![ready(1), ready(2)])), vec![1, 2]);
17 assert_done(|| Box::new(join_all(vec![ready(1)])), vec![1]);
18 // REVIEW: should this be implemented?
19 // assert_done(|| Box::new(join_all(Vec::<i32>::new())), vec![]);
20
21 // TODO: needs more tests
22 }
23
24 #[test]
join_all_iter_lifetime()25 fn join_all_iter_lifetime() {
26 // In futures-rs version 0.1, this function would fail to typecheck due to an overly
27 // conservative type parameterization of `JoinAll`.
28 fn sizes(bufs: Vec<&[u8]>) -> Box<dyn Future<Output = Vec<usize>> + Unpin> {
29 let iter = bufs.into_iter().map(|b| ready::<usize>(b.len()));
30 Box::new(join_all(iter))
31 }
32
33 assert_done(|| sizes(vec![&[1, 2, 3], &[], &[0]]), vec![3_usize, 0, 1]);
34 }
35
36 #[test]
join_all_from_iter()37 fn join_all_from_iter() {
38 assert_done(
39 || Box::new(vec![ready(1), ready(2)].into_iter().collect::<JoinAll<_>>()),
40 vec![1, 2],
41 )
42 }
43