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