mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
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:
@@ -14,6 +14,7 @@ use chat::ChatItem;
|
|||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
|
use tokio::runtime::Handle;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
@@ -263,7 +264,6 @@ impl TestContext {
|
|||||||
Self::builder().configure_fiona().build().await
|
Self::builder().configure_fiona().build().await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
/// Print current chat state.
|
/// Print current chat state.
|
||||||
pub async fn print_chats(&self) {
|
pub async fn print_chats(&self) {
|
||||||
println!("\n========== Chats of {}: ==========", self.name());
|
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 {
|
pub enum LogEvent {
|
||||||
/// Logged event.
|
/// Logged event.
|
||||||
Event(Event),
|
Event(Event),
|
||||||
@@ -1079,4 +1092,12 @@ mod tests {
|
|||||||
bob.ctx.emit_event(EventType::Info("there".into()));
|
bob.ctx.emit_event(EventType::Info("there".into()));
|
||||||
// panic!("Both fail");
|
// 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user