summaryrefslogtreecommitdiffstats
path: root/tokio-test
diff options
context:
space:
mode:
authorBlas Rodriguez Irizar <rodrigblas@gmail.com>2020-08-20 21:48:27 +0200
committerGitHub <noreply@github.com>2020-08-20 15:48:27 -0400
commit138eef352671aadffca304d62c69cb2582e64f2a (patch)
tree884e7c38802a8e19a4d4d438183e384b1046a77b /tokio-test
parentc393236dfd12c13e82badd631d3a3a90481c6f95 (diff)
test: implement Drop for Mock to panic w/ unconsumed data (#2704)
Diffstat (limited to 'tokio-test')
-rw-r--r--tokio-test/src/io.rs15
-rw-r--r--tokio-test/tests/io.rs14
2 files changed, 29 insertions, 0 deletions
diff --git a/tokio-test/src/io.rs b/tokio-test/src/io.rs
index f1ce77aa..ac70a06f 100644
--- a/tokio-test/src/io.rs
+++ b/tokio-test/src/io.rs
@@ -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 {
diff --git a/tokio-test/tests/io.rs b/tokio-test/tests/io.rs
index 948bc323..f164abaf 100644
--- a/tokio-test/tests/io.rs
+++ b/tokio-test/tests/io.rs
@@ -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();
+}