From 8d119713bce74d14e19e40e6a315cbc36d677d67 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 9 Jan 2023 22:46:32 +0100 Subject: [PATCH] Print chats when a test failed (#3937) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Print chats after a test failed again E.g. ``` ========== Chats of bob: ========== Single#Chat#10: alice@example.org [alice@example.org] -------------------------------------------------------------------------------- Msg#10: (Contact#Contact#10): hellooo [FRESH] Msg#11: (Contact#Contact#10): hellooo without mailing list [FRESH] -------------------------------------------------------------------------------- ========== Chats of alice: ========== Single#Chat#10: bob@example.net [bob@example.net] -------------------------------------------------------------------------------- Msg#10: Me (Contact#Contact#Self): hellooo √ Msg#11: Me (Contact#Contact#Self): hellooo without mailing list √ -------------------------------------------------------------------------------- ``` I found this very useful sometimes, so, let's try to re-introduce it (it was removed in #3449) * Add failing test for TestContext::drop() * Do not panic in TestContext::drop() if runtime is dropped Co-authored-by: link2xt --- src/test_utils.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/test_utils.rs b/src/test_utils.rs index 8204602e8..cf19dafd8 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -14,6 +14,7 @@ use chat::ChatItem; use once_cell::sync::Lazy; use rand::Rng; use tempfile::{tempdir, TempDir}; +use tokio::runtime::Handle; use tokio::sync::RwLock; use tokio::task; @@ -263,7 +264,6 @@ impl TestContext { Self::builder().configure_fiona().build().await } - #[allow(dead_code)] /// Print current chat state. pub async fn print_chats(&self) { println!("\n========== Chats of {}: ==========", self.name()); @@ -702,6 +702,19 @@ impl Deref for TestContext { } } +impl Drop for TestContext { + fn drop(&mut self) { + task::block_in_place(move || { + if let Ok(handle) = Handle::try_current() { + // Print the chats if runtime still exists. + handle.block_on(async move { + self.print_chats().await; + }); + } + }); + } +} + pub enum LogEvent { /// Logged event. Event(Event), @@ -1079,4 +1092,12 @@ mod tests { bob.ctx.emit_event(EventType::Info("there".into())); // panic!("Both fail"); } + + /// Checks that dropping the `TestContext` after the runtime does not panic, + /// e.g. that `TestContext::drop` does not assume the runtime still exists. + #[test] + fn test_new_test_context() { + let runtime = tokio::runtime::Runtime::new().expect("unable to create tokio runtime"); + runtime.block_on(TestContext::new()); + } }