diff options
author | Benjamin Halsted <benjamin_halsted@yahoo.com> | 2020-04-02 14:10:12 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-02 17:10:12 -0400 |
commit | cf4cbc142bd8198d2112cf671c120740fdc4e132 (patch) | |
tree | ff9b3f1d3f1427a3a724c988793a2400c7a523d6 /tokio-test/tests | |
parent | 215d7d4c5f3aa5b436183b8d8abfb9701f34a17d (diff) |
test: Added read_error() and write_error() (#2337)
Enable testing of edge cases caused by io errors.
Diffstat (limited to 'tokio-test/tests')
-rw-r--r-- | tokio-test/tests/io.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tokio-test/tests/io.rs b/tokio-test/tests/io.rs index 954bb469..948bc323 100644 --- a/tokio-test/tests/io.rs +++ b/tokio-test/tests/io.rs @@ -1,5 +1,6 @@ #![warn(rust_2018_idioms)] +use std::io; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio_test::io::Builder; @@ -17,9 +18,55 @@ async fn read() { } #[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(); 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"); +} |