summaryrefslogtreecommitdiffstats
path: root/tokio-buf/tests/stream.rs
blob: c7a394fc5463f650c8ce2e570657ba66e3a52ec4 (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::sync::mpsc;
use futures::Async::*;
use std::io::Cursor;
use tokio_buf::{util, BufStream};
use tokio_mock_task::MockTask;

mod support;

type Buf = Cursor<&'static [u8]>;

#[test]
fn empty_stream() {
    let (_, rx) = mpsc::unbounded::<Buf>();
    let mut bs = util::stream(rx);
    assert_none!(bs.poll_buf());
}

#[test]
fn full_stream() {
    let (tx, rx) = mpsc::unbounded();
    let mut bs = util::stream(rx);
    let mut task = MockTask::new();

    tx.unbounded_send(buf(b"one")).unwrap();

    assert_buf_eq!(bs.poll_buf(), "one");
    task.enter(|| assert_not_ready!(bs.poll_buf()));

    tx.unbounded_send(buf(b"two")).unwrap();

    assert!(task.is_notified());
    assert_buf_eq!(bs.poll_buf(), "two");
    task.enter(|| assert_not_ready!(bs.poll_buf()));

    drop(tx);

    assert!(task.is_notified());
    assert_none!(bs.poll_buf());
}

fn buf(data: &'static [u8]) -> Buf {
    Cursor::new(data)
}