Skip to content

Print chats when a test failed #3937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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());
}
}