summaryrefslogtreecommitdiffstats
path: root/tokio-io/tests/async_read.rs
blob: 604e99fd4571813ce0cae37fd7d7ff8295eb16f0 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
extern crate bytes;
extern crate futures;
extern crate tokio_io;

use bytes::{BufMut, BytesMut};
use futures::Async;
use tokio_io::AsyncRead;

use std::io::{self, Read};

#[test]
fn read_buf_success() {
    struct R;

    impl Read for R {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            buf[0..11].copy_from_slice(b"hello world");
            Ok(11)
        }
    }

    impl AsyncRead for R {}

    let mut buf = BytesMut::with_capacity(65);

    let n = match R.read_buf(&mut buf).unwrap() {
        Async::Ready(n) => n,
        _ => panic!(),
    };

    assert_eq!(11, n);
    assert_eq!(buf[..], b"hello world"[..]);
}

#[test]
fn read_buf_error() {
    struct R;

    impl Read for R {
        fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::Other, "other"))
        }
    }

    impl AsyncRead for R {}

    let mut buf = BytesMut::with_capacity(65);

    let err = R.read_buf(&mut buf).unwrap_err();
    assert_eq!(err.kind(), io::ErrorKind::Other);
}

#[test]
fn read_buf_no_capacity() {
    struct R;

    impl Read for R {
        fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
            unimplemented!();
        }
    }

    impl AsyncRead for R {}

    // Can't create BytesMut w/ zero capacity, so fill it up
    let mut buf = BytesMut::with_capacity(64);
    buf.put(&[0; 64][..]);

    let n = match R.read_buf(&mut buf).unwrap() {
        Async::Ready(n) => n,
        _ => panic!(),
    };

    assert_eq!(0, n);
}

#[test]
fn read_buf_no_uninitialized() {
    struct R;

    impl Read for R {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            for b in buf {
                assert_eq!(0, *b);
            }

            Ok(0)
        }
    }

    impl AsyncRead for R {}

    // Can't create BytesMut w/ zero capacity, so fill it up
    let mut buf = BytesMut::with_capacity(64);

    let n = match R.read_buf(&mut buf).unwrap() {
        Async::Ready(n) => n,
        _ => panic!(),
    };

    assert_eq!(0, n);
}

#[test]
fn read_buf_uninitialized_ok() {
    struct R;

    impl Read for R {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            assert_eq!(buf[0..11], b"hello world"[..]);
            Ok(0)
        }
    }

    impl AsyncRead for R {
        unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
            false
        }
    }

    // Can't create BytesMut w/ zero capacity, so fill it up
    let mut buf = BytesMut::with_capacity(64);
    unsafe {
        buf.bytes_mut()[0..11].copy_from_slice(b"hello world");
    }

    let n = match R.read_buf(&mut buf).unwrap() {
        Async::Ready(n) => n,
        _ => panic!(),
    };

    assert_eq!(0, n);
}

#[test]
fn read_buf_translate_wouldblock_to_not_ready() {
    struct R;

    impl Read for R {
        fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
        }
    }

    impl AsyncRead for R {}

    let mut buf = BytesMut::with_capacity(65);
    assert!(!R.read_buf(&mut buf).unwrap().is_ready());
}