diff --git a/src/client.rs b/src/client.rs index 728f773..3d41232 100644 --- a/src/client.rs +++ b/src/client.rs @@ -443,8 +443,10 @@ impl Client { let mut line_buffer: Vec = Vec::new(); while line_buffer.len() < 2 || (line_buffer[line_buffer.len()-1] != LF && line_buffer[line_buffer.len()-2] != CR) { let byte_buffer: &mut [u8] = &mut [0]; - try!(self.stream.read(byte_buffer)); - line_buffer.push(byte_buffer[0]); + let n = try!(self.stream.read(byte_buffer)); + if n > 0 { + line_buffer.push(byte_buffer[0]); + } } if self.debug { @@ -490,6 +492,7 @@ mod tests { assert!(expected_response == actual_response, "expected response doesn't equal actual"); } + #[test] fn read_greeting() { let greeting = "* OK Dovecot ready.\r\n"; @@ -498,6 +501,16 @@ mod tests { client.read_greeting().unwrap(); } + #[test] + fn readline_delay_read() { + let greeting = "* OK Dovecot ready.\r\n"; + let expected_response: String = greeting.to_string(); + let mock_stream = MockStream::new_read_delay(greeting.as_bytes().to_vec()); + let mut client = Client::new(mock_stream); + let actual_response = String::from_utf8(client.readline().unwrap()).unwrap(); + assert_eq!(expected_response, actual_response); + } + #[test] #[should_panic] fn readline_err() { diff --git a/src/mock_stream.rs b/src/mock_stream.rs index 20f96a8..3c1c086 100644 --- a/src/mock_stream.rs +++ b/src/mock_stream.rs @@ -5,7 +5,8 @@ pub struct MockStream { read_buf: Vec, read_pos: usize, pub written_buf: Vec, - err_on_read: bool + err_on_read: bool, + read_delay: usize } impl MockStream { @@ -14,7 +15,8 @@ impl MockStream { read_buf: read_buf, read_pos: 0, written_buf: Vec::new(), - err_on_read: false + err_on_read: false, + read_delay: 0 } } @@ -23,13 +25,28 @@ impl MockStream { read_buf: Vec::new(), read_pos: 0, written_buf: Vec::new(), - err_on_read: true + err_on_read: true, + read_delay: 0 + } + } + + pub fn new_read_delay(read_buf: Vec) -> MockStream { + MockStream{ + read_buf: read_buf, + read_pos: 0, + written_buf: Vec::new(), + err_on_read: false, + read_delay: 1, } } } impl Read for MockStream { fn read(&mut self, buf: &mut[u8]) -> Result { + if self.read_delay > 0 { + self.read_delay -= 1; + return Ok(0) + } if self.err_on_read { return Err(Error::new(ErrorKind::Other, "MockStream Error")) }