test: implement Drop for Mock to panic w/ unconsumed data (#2704)

This commit is contained in:
Blas Rodriguez Irizar
2020-08-20 15:48:27 -04:00
committed by GitHub
parent c393236dfd
commit 138eef3526
2 changed files with 29 additions and 0 deletions
+15
View File
@@ -458,6 +458,21 @@ impl AsyncWrite for Mock {
}
}
/// Ensures that Mock isn't dropped with data "inside".
impl Drop for Mock {
fn drop(&mut self) {
// Avoid double panicking, since makes debugging much harder.
if std::thread::panicking() {
return;
}
self.inner.actions.iter().for_each(|a| match a {
Action::Read(data) => assert!(data.is_empty(), "There is still data left to read."),
Action::Write(data) => assert!(data.is_empty(), "There is still data left to write."),
_ => (),
})
}
}
/*
/// Returns `true` if called from the context of a futures-rs Task
fn is_task_ctx() -> bool {
+14
View File
@@ -70,3 +70,17 @@ async fn write_error() {
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();
}