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
158
159
160
161
use std::io;
use std::fmt;
use std::cmp::min;
use std::sync::{Arc, Mutex, MutexGuard};

use rotor::mio;
use rotor_stream::SocketError;

/// In memory stream
///
/// The struct pretends to be `mio::Evented` but it have `unimplemented` panic
/// when actually added to the loop. I.e. it should be used in tests which
/// use plain state machine, and not the event loop.
///
/// Clarification: it implements `Read`/`Write` but, it's not a pipe. I.e.
/// buffers for `Read` and `Write` are separate. You use `push_xxx` methods to
/// add data for the next `Read::read`.
///
/// You should clone the stream. Feed one to the application and second one
/// to the unit testing code.
#[derive(Clone)]
pub struct MemIo(Arc<Mutex<Bufs>>);

struct Bufs {
    input: Vec<u8>,
    input_closed: bool,
    output: Vec<u8>,
}

impl MemIo {
    /// Create a stream
    ///
    /// Stream start empty
    pub fn new() -> MemIo {
        MemIo(Arc::new(Mutex::new(Bufs {
            input: Vec::new(),
            input_closed: false,
            output: Vec::new(),
        })))
    }
    /// Push some bytes to an input buffer of an application
    pub fn push_bytes<T:AsRef<[u8]>>(&mut self, val: T) {
        let mut bufs = self.bufs();
        bufs.input.extend(val.as_ref());
        assert!(!bufs.input_closed);
    }
    /// Marks input as closed so application gets end-of-stream event on next
    /// read
    pub fn shutdown_input(&self) {
        self.bufs().input_closed = true;
    }
    /// Get output as a string
    ///
    /// This is created by `String::from_utf8_lossy` so kinda works for binary
    /// data too, but not exactly. If you need to compare some non-textual
    /// data use `output_bytes()`
    ///
    /// The data in the buffer are not discarded. Next call will return same
    /// data again.
    pub fn output_str(&self) -> String {
        // Unfortunately we can't return a slice, because of borrowing rules
        // but it's for unit tests, so we don't care performance
        String::from_utf8_lossy(&self.bufs().output).to_string()
    }
    /// Get data in the output buffer
    ///
    /// The data in the buffer are not discarded. Next call will return same
    /// data again.
    pub fn output_bytes(&self) -> Vec<u8> {
        // Unfortunately we can't return a slice, because of borrowing rules
        // but it's for unit tests, so we don't care performance
        self.bufs().output.clone()
    }
    fn bufs(&self) -> MutexGuard<Bufs> {
        self.0.lock().expect("Poisoned MemIo (mock stream)")
    }
}

impl SocketError for MemIo {
    fn take_socket_error(&self) -> io::Result<()> {
        Ok(())
    }
}

impl fmt::Debug for MemIo {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let bufs = self.bufs();
        fmt.debug_struct("MemIo")
        .field("input", &String::from_utf8_lossy(&bufs.input))
        .field("input_closed", &bufs.input_closed)
        .field("output", &String::from_utf8_lossy(&bufs.output))
        .finish()
    }
}

impl io::Read for MemIo {
    fn read(&mut self, val: &mut [u8]) -> io::Result<usize> {
        let mut bufs = self.bufs();
        let bytes = min(val.len(), bufs.input.len());
        if bytes > 0 {
            assert_eq!(io::copy(
                &mut io::Cursor::new(&bufs.input),
                &mut io::Cursor::new(val))
                .expect("copy always work"), bytes as u64);
            bufs.input.drain(..bytes);
            Ok(bytes)
        } else {
            if bufs.input_closed {
                Ok(0)
            } else {
                Err(io::Error::new(io::ErrorKind::WouldBlock,
                    "no data in mocked input buffer"))
            }
        }
    }
}
impl io::Write for MemIo {
    fn write(&mut self, val: &[u8]) -> io::Result<usize> {
        let mut bufs = self.bufs();
        io::copy(&mut io::Cursor::new(val), &mut bufs.output)
            .map(|x| x as usize)
    }
    fn flush(&mut self) -> io::Result<()> { Ok(()) }
}

impl mio::Evented for MemIo {
    fn register(&self, _selector: &mut mio::Selector,
        _token: mio::Token, _interest: mio::EventSet, _opts: mio::PollOpt)
        -> io::Result<()>
    { unreachable!("trying to poll on mock stream") }
    fn reregister(&self, _selector: &mut mio::Selector, _token: mio::Token,
        _interest: mio::EventSet, _opts: mio::PollOpt) -> io::Result<()>
    { unreachable!("trying to poll on mock stream") }
    fn deregister(&self, _selector: &mut mio::Selector) -> io::Result<()>
    { unreachable!("trying to poll on mock stream") }
}

#[cfg(test)]
mod self_test {
    use std::io::{Read, Write};
    use super::MemIo;

    #[test]
    fn input() {
        let mut s = MemIo::new();
        s.push_bytes("hello world");
        s.shutdown_input();
        let mut b = String::new();
        assert_eq!(s.read_to_string(&mut b).unwrap(), 11);
        assert_eq!(&b, "hello world");
    }

    #[test]
    fn output() {
        let mut s = MemIo::new();
        s.write(b"hello").expect("write failed");
        s.write(b"world").expect("write failed");
        assert_eq!(s.output_str(), "helloworld");
    }

}