summaryrefslogtreecommitdiffstats
path: root/vendor/futures/tests/stream_select_next_some.rs
blob: 8252ad7b543c6321c826c868eba3b489d2354d84 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use futures::executor::block_on;
use futures::future::{self, FusedFuture, FutureExt};
use futures::select;
use futures::stream::{FuturesUnordered, StreamExt};
use futures::task::{Context, Poll};
use futures_test::future::FutureTestExt;
use futures_test::task::new_count_waker;

#[test]
fn is_terminated() {
    let (waker, counter) = new_count_waker();
    let mut cx = Context::from_waker(&waker);

    let mut tasks = FuturesUnordered::new();

    let mut select_next_some = tasks.select_next_some();
    assert_eq!(select_next_some.is_terminated(), false);
    assert_eq!(select_next_some.poll_unpin(&mut cx), Poll::Pending);
    assert_eq!(counter, 1);
    assert_eq!(select_next_some.is_terminated(), true);
    drop(select_next_some);

    tasks.push(future::ready(1));

    let mut select_next_some = tasks.select_next_some();
    assert_eq!(select_next_some.is_terminated(), false);
    assert_eq!(select_next_some.poll_unpin(&mut cx), Poll::Ready(1));
    assert_eq!(select_next_some.is_terminated(), false);
    assert_eq!(select_next_some.poll_unpin(&mut cx), Poll::Pending);
    assert_eq!(select_next_some.is_terminated(), true);
}

#[test]
fn select() {
    // Checks that even though `async_tasks` will yield a `None` and return
    // `is_terminated() == true` during the first poll, it manages to toggle
    // back to having items after a future is pushed into it during the second
    // poll (after pending_once completes).
    block_on(async {
        let mut fut = future::ready(1).pending_once();
        let mut async_tasks = FuturesUnordered::new();
        let mut total = 0;
        loop {
            select! {
                num = fut => {
                    total += num;
                    async_tasks.push(async { 5 });
                },
                num = async_tasks.select_next_some() => {
                    total += num;
                }
                complete => break,
            }
        }
        assert_eq!(total, 6);
    });
}

// Check that `select!` macro does not fail when importing from `futures_util`.
#[test]
fn futures_util_select() {
    use futures_util::select;

    // Checks that even though `async_tasks` will yield a `None` and return
    // `is_terminated() == true` during the first poll, it manages to toggle
    // back to having items after a future is pushed into it during the second
    // poll (after pending_once completes).
    block_on(async {
        let mut fut = future::ready(1).pending_once();
        let mut async_tasks = FuturesUnordered::new();
        let mut total = 0;
        loop {
            select! {
                num = fut => {
                    total += num;
                    async_tasks.push(async { 5 });
                },
                num = async_tasks.select_next_some() => {
                    total += num;
                }
                complete => break,
            }
        }
        assert_eq!(total, 6);
    });
}