Print chats when a test failed (#3937)

* 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 <link2xt@testrun.org>
This commit is contained in:
Hocuri
2023-01-09 22:46:32 +01:00
committed by GitHub
parent 07f2e28eca
commit 8d119713bc

View File

@@ -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());
}
}