From 1df838a49647295e3c470586e210897b487aa5d1 Mon Sep 17 00:00:00 2001 From: Finomnis Date: Fri, 20 Oct 2023 14:25:06 +0200 Subject: [PATCH] Add tests for error_collector --- src/subsystem/error_collector.rs | 73 ++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/subsystem/error_collector.rs b/src/subsystem/error_collector.rs index 93208b0..b7da95c 100644 --- a/src/subsystem/error_collector.rs +++ b/src/subsystem/error_collector.rs @@ -41,3 +41,76 @@ impl Drop for ErrorCollector { } } } + +#[cfg(test)] +mod tests { + + use tracing_test::traced_test; + + use super::*; + + #[test] + #[traced_test] + fn normal() { + let (sender, receiver) = mpsc::unbounded_channel(); + let mut error_collector = ErrorCollector::::new(receiver); + + sender + .send(SubsystemError::Panicked(Arc::from("ABC"))) + .unwrap(); + sender + .send(SubsystemError::Panicked(Arc::from("def"))) + .unwrap(); + + let received = error_collector.finish(); + assert_eq!( + received.iter().map(|e| e.name()).collect::>(), + vec!["ABC", "def"] + ); + } + + #[test] + #[traced_test] + fn double_finish() { + let (sender, receiver) = mpsc::unbounded_channel(); + let mut error_collector = ErrorCollector::::new(receiver); + + sender + .send(SubsystemError::Panicked(Arc::from("ABC"))) + .unwrap(); + sender + .send(SubsystemError::Panicked(Arc::from("def"))) + .unwrap(); + + let received = error_collector.finish(); + assert_eq!( + received.iter().map(|e| e.name()).collect::>(), + vec!["ABC", "def"] + ); + + let received = error_collector.finish(); + assert_eq!( + received.iter().map(|e| e.name()).collect::>(), + vec!["ABC", "def"] + ); + } + + #[test] + #[traced_test] + fn no_finish() { + let (sender, receiver) = mpsc::unbounded_channel(); + let error_collector = ErrorCollector::::new(receiver); + + sender + .send(SubsystemError::Panicked(Arc::from("ABC"))) + .unwrap(); + sender + .send(SubsystemError::Panicked(Arc::from("def"))) + .unwrap(); + + drop(error_collector); + + assert!(logs_contain("An error got dropped: Panicked(\"ABC\")")); + assert!(logs_contain("An error got dropped: Panicked(\"def\")")); + } +}