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