Skip to content

Commit 8d11971

Browse files
Hocurilink2xt
andauthored
Print chats when a test failed (#3937)
* Print chats after a test failed again E.g. ``` ========== Chats of bob: ========== Single#Chat#10: [email protected] [[email protected]] -------------------------------------------------------------------------------- Msg#10: (Contact#Contact#10): hellooo [FRESH] Msg#11: (Contact#Contact#10): hellooo without mailing list [FRESH] -------------------------------------------------------------------------------- ========== Chats of alice: ========== Single#Chat#10: [email protected] [[email protected]] -------------------------------------------------------------------------------- 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 <[email protected]>
1 parent 07f2e28 commit 8d11971

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Diff for: src/test_utils.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use chat::ChatItem;
1414
use once_cell::sync::Lazy;
1515
use rand::Rng;
1616
use tempfile::{tempdir, TempDir};
17+
use tokio::runtime::Handle;
1718
use tokio::sync::RwLock;
1819
use tokio::task;
1920

@@ -263,7 +264,6 @@ impl TestContext {
263264
Self::builder().configure_fiona().build().await
264265
}
265266

266-
#[allow(dead_code)]
267267
/// Print current chat state.
268268
pub async fn print_chats(&self) {
269269
println!("\n========== Chats of {}: ==========", self.name());
@@ -702,6 +702,19 @@ impl Deref for TestContext {
702702
}
703703
}
704704

705+
impl Drop for TestContext {
706+
fn drop(&mut self) {
707+
task::block_in_place(move || {
708+
if let Ok(handle) = Handle::try_current() {
709+
// Print the chats if runtime still exists.
710+
handle.block_on(async move {
711+
self.print_chats().await;
712+
});
713+
}
714+
});
715+
}
716+
}
717+
705718
pub enum LogEvent {
706719
/// Logged event.
707720
Event(Event),
@@ -1079,4 +1092,12 @@ mod tests {
10791092
bob.ctx.emit_event(EventType::Info("there".into()));
10801093
// panic!("Both fail");
10811094
}
1095+
1096+
/// Checks that dropping the `TestContext` after the runtime does not panic,
1097+
/// e.g. that `TestContext::drop` does not assume the runtime still exists.
1098+
#[test]
1099+
fn test_new_test_context() {
1100+
let runtime = tokio::runtime::Runtime::new().expect("unable to create tokio runtime");
1101+
runtime.block_on(TestContext::new());
1102+
}
10821103
}

0 commit comments

Comments
 (0)