1 mod util {
2 use std::future::Future;
3 use futures::executor::block_on;
4 use std::fmt::Debug;
5
assert_done<T, F>(actual_fut: F, expected: T) where T: PartialEq + Debug, F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,6 pub fn assert_done<T, F>(actual_fut: F, expected: T)
7 where
8 T: PartialEq + Debug,
9 F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,
10 {
11 let output = block_on(actual_fut());
12 assert_eq!(output, expected);
13 }
14 }
15
16 #[test]
collect_collects()17 fn collect_collects() {
18 use futures_util::future::{err, ok, try_join_all};
19
20 use util::assert_done;
21
22 assert_done(|| Box::new(try_join_all(vec![ok(1), ok(2)])), Ok::<_, usize>(vec![1, 2]));
23 assert_done(|| Box::new(try_join_all(vec![ok(1), err(2)])), Err(2));
24 assert_done(|| Box::new(try_join_all(vec![ok(1)])), Ok::<_, usize>(vec![1]));
25 // REVIEW: should this be implemented?
26 // assert_done(|| Box::new(try_join_all(Vec::<i32>::new())), Ok(vec![]));
27
28 // TODO: needs more tests
29 }
30
31 #[test]
try_join_all_iter_lifetime()32 fn try_join_all_iter_lifetime() {
33 use futures_util::future::{ok, try_join_all};
34 use std::future::Future;
35
36 use util::assert_done;
37
38 // In futures-rs version 0.1, this function would fail to typecheck due to an overly
39 // conservative type parameterization of `TryJoinAll`.
40 fn sizes(bufs: Vec<&[u8]>) -> Box<dyn Future<Output = Result<Vec<usize>, ()>> + Unpin> {
41 let iter = bufs.into_iter().map(|b| ok::<usize, ()>(b.len()));
42 Box::new(try_join_all(iter))
43 }
44
45 assert_done(|| sizes(vec![&[1,2,3], &[], &[0]]), Ok(vec![3_usize, 0, 1]));
46 }
47
48 #[test]
try_join_all_from_iter()49 fn try_join_all_from_iter() {
50 use futures_util::future::{ok, TryJoinAll};
51
52 use util::assert_done;
53
54 assert_done(
55 || Box::new(vec![ok(1), ok(2)].into_iter().collect::<TryJoinAll<_>>()),
56 Ok::<_, usize>(vec![1, 2]),
57 )
58 }
59