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
150
151
152
153
154
155
156
157
use std::cmp::min;
use std::io::{self, Read, Write};
use std::sync::{Arc, Mutex};
use netbuf::RangeArgument;
use futures::{Poll, Async};
use tokio_io::{AsyncRead, AsyncWrite};
pub struct Mock;
impl Read for Mock {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::WouldBlock, "No read"))
}
}
impl Write for Mock {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::WouldBlock, "No write"))
}
fn flush(&mut self) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::WouldBlock, "No flush"))
}
}
impl AsyncRead for Mock {}
impl AsyncWrite for Mock {
fn shutdown(&mut self) -> Poll<(), io::Error> {
Ok(Async::NotReady)
}
}
#[derive(Clone)]
pub struct MockData {
input: Arc<Mutex<Vec<u8>>>,
output: Arc<Mutex<Vec<u8>>>,
}
impl MockData {
pub fn new() -> MockData {
MockData {
input: Arc::new(Mutex::new(Vec::new())),
output: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn add_input<D: AsRef<[u8]>>(&self, data: D) {
self.input.lock().unwrap().extend(data.as_ref())
}
pub fn output<T: Into<RangeArgument>>(&self, range: T) -> Vec<u8> {
let buf = self.output.lock().unwrap();
use netbuf::RangeArgument::*;
match range.into() {
RangeTo(x) => buf[..x].to_vec(),
RangeFrom(x) => buf[x..].to_vec(),
Range(x, y) => buf[x..y].to_vec(),
}
}
pub fn get_output(&self, num: usize) -> Vec<u8> {
let mut buf = self.output.lock().unwrap();
return buf.drain(..num).collect();
}
}
impl Read for MockData {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut inp = self.input.lock().unwrap();
let bytes = min(buf.len(), inp.len());
if bytes == 0 {
return Err(io::ErrorKind::WouldBlock.into());
}
buf[..bytes].copy_from_slice(&inp[..bytes]);
inp.drain(..bytes);
return Ok(bytes);
}
}
impl Write for MockData {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut out = self.output.lock().unwrap();
out.extend(buf);
return Ok(buf.len());
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl AsyncRead for MockData {}
impl AsyncWrite for MockData {
fn shutdown(&mut self) -> Poll<(), io::Error> {
if self.output.lock().unwrap().len() > 0 {
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}
}
#[cfg(test)]
mod test {
use {IoBuf, Mock, MockData};
#[test]
fn mock() {
let mut buf = IoBuf::new(Mock);
buf.read().ok();
assert_eq!(&buf.in_buf[..], b"");
buf.out_buf.extend(b"hello");
assert_eq!(&buf.out_buf[..], b"hello");
buf.flush().ok();
assert_eq!(&buf.out_buf[..], b"hello");
}
#[test]
fn mock_data() {
let data = MockData::new();
let mut buf = IoBuf::new(data.clone());
buf.read().ok();
assert_eq!(&buf.in_buf[..], b"");
data.add_input("test me");
buf.read().ok();
assert_eq!(&buf.in_buf[..], b"test me");
buf.out_buf.extend(b"hello");
assert_eq!(&buf.out_buf[..], b"hello");
buf.flush().ok();
assert_eq!(&buf.out_buf[..], b"");
assert_eq!(&data.output(..), b"hello");
}
}