aboutsummaryrefslogtreecommitdiff
path: root/tests/join_all.rs
blob: 63967bf987a171c3a97c4d378697cbad942a788c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use futures_util::future::*;
use std::future::Future;
use futures::executor::block_on;
use std::fmt::Debug;

fn assert_done<T, F>(actual_fut: F, expected: T)
where
    T: PartialEq + Debug,
    F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,
{
    let output = block_on(actual_fut());
    assert_eq!(output, expected);
}

#[test]
fn collect_collects() {
    assert_done(|| Box::new(join_all(vec![ready(1), ready(2)])), vec![1, 2]);
    assert_done(|| Box::new(join_all(vec![ready(1)])), vec![1]);
    // REVIEW: should this be implemented?
    // assert_done(|| Box::new(join_all(Vec::<i32>::new())), vec![]);

    // TODO: needs more tests
}

#[test]
fn join_all_iter_lifetime() {
    // In futures-rs version 0.1, this function would fail to typecheck due to an overly
    // conservative type parameterization of `JoinAll`.
    fn sizes<'a>(bufs: Vec<&'a [u8]>) -> Box<dyn Future<Output = Vec<usize>> + Unpin> {
        let iter = bufs.into_iter().map(|b| ready::<usize>(b.len()));
        Box::new(join_all(iter))
    }

    assert_done(|| sizes(vec![&[1,2,3], &[], &[0]]), vec![3 as usize, 0, 1]);
}

#[test]
fn join_all_from_iter() {
    assert_done(
        || Box::new(vec![ready(1), ready(2)].into_iter().collect::<JoinAll<_>>()),
        vec![1, 2],
    )
}