test: Added read_error() and write_error() (#2337)

Enable testing of edge cases caused by io errors.
This commit is contained in:
Benjamin Halsted
2020-04-02 17:10:12 -04:00
committed by GitHub
parent 215d7d4c5f
commit cf4cbc142b
2 changed files with 111 additions and 2 deletions
+47
View File
@@ -1,5 +1,6 @@
#![warn(rust_2018_idioms)]
use std::io;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_test::io::Builder;
@@ -16,6 +17,31 @@ async fn read() {
assert_eq!(&buf[..n], b"world!");
}
#[tokio::test]
async fn read_error() {
let error = io::Error::new(io::ErrorKind::Other, "cruel");
let mut mock = Builder::new()
.read(b"hello ")
.read_error(error)
.read(b"world!")
.build();
let mut buf = [0; 256];
let n = mock.read(&mut buf).await.expect("read 1");
assert_eq!(&buf[..n], b"hello ");
match mock.read(&mut buf).await {
Err(error) => {
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!("cruel", format!("{}", error));
}
Ok(_) => panic!("error not received"),
}
let n = mock.read(&mut buf).await.expect("read 1");
assert_eq!(&buf[..n], b"world!");
}
#[tokio::test]
async fn write() {
let mut mock = Builder::new().write(b"hello ").write(b"world!").build();
@@ -23,3 +49,24 @@ async fn write() {
mock.write_all(b"hello ").await.expect("write 1");
mock.write_all(b"world!").await.expect("write 2");
}
#[tokio::test]
async fn write_error() {
let error = io::Error::new(io::ErrorKind::Other, "cruel");
let mut mock = Builder::new()
.write(b"hello ")
.write_error(error)
.write(b"world!")
.build();
mock.write_all(b"hello ").await.expect("write 1");
match mock.write_all(b"whoa").await {
Err(error) => {
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!("cruel", format!("{}", error));
}
Ok(_) => panic!("error not received"),
}
mock.write_all(b"world!").await.expect("write 2");
}