You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.2 KiB
87 lines
2.2 KiB
#![warn(rust_2018_idioms)]
|
|
|
|
use std::io;
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
use tokio_test::io::Builder;
|
|
|
|
#[tokio::test]
|
|
async fn read() {
|
|
let mut mock = Builder::new().read(b"hello ").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 ");
|
|
|
|
let n = mock.read(&mut buf).await.expect("read 2");
|
|
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();
|
|
|
|
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");
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[should_panic]
|
|
async fn mock_panics_read_data_left() {
|
|
use tokio_test::io::Builder;
|
|
Builder::new().read(b"read").build();
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[should_panic]
|
|
async fn mock_panics_write_data_left() {
|
|
use tokio_test::io::Builder;
|
|
Builder::new().write(b"write").build();
|
|
}
|