summaryrefslogtreecommitdiffstats
path: root/third_party/rust/futures-channel/tests/mpsc-size_hint.rs
blob: d9cdaa31fa37f832a4e0a2649f8c30b683805249 (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
use futures::channel::mpsc;
use futures::stream::Stream;

#[test]
fn unbounded_size_hint() {
    let (tx, mut rx) = mpsc::unbounded::<u32>();
    assert_eq!((0, None), rx.size_hint());
    tx.unbounded_send(1).unwrap();
    assert_eq!((1, None), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((0, None), rx.size_hint());
    tx.unbounded_send(2).unwrap();
    tx.unbounded_send(3).unwrap();
    assert_eq!((2, None), rx.size_hint());
    drop(tx);
    assert_eq!((2, Some(2)), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((1, Some(1)), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((0, Some(0)), rx.size_hint());
}

#[test]
fn channel_size_hint() {
    let (mut tx, mut rx) = mpsc::channel::<u32>(10);
    assert_eq!((0, None), rx.size_hint());
    tx.try_send(1).unwrap();
    assert_eq!((1, None), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((0, None), rx.size_hint());
    tx.try_send(2).unwrap();
    tx.try_send(3).unwrap();
    assert_eq!((2, None), rx.size_hint());
    drop(tx);
    assert_eq!((2, Some(2)), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((1, Some(1)), rx.size_hint());
    rx.try_next().unwrap().unwrap();
    assert_eq!((0, Some(0)), rx.size_hint());
}