Skip to content

Commit

Permalink
Add tests for error_collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Oct 20, 2023
1 parent 2e0e050 commit 1df838a
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/subsystem/error_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,76 @@ impl<ErrType: ErrTypeTraits> Drop for ErrorCollector<ErrType> {
}
}
}

#[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::<String>::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<_>>(),
vec!["ABC", "def"]
);
}

#[test]
#[traced_test]
fn double_finish() {
let (sender, receiver) = mpsc::unbounded_channel();
let mut error_collector = ErrorCollector::<String>::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<_>>(),
vec!["ABC", "def"]
);

let received = error_collector.finish();
assert_eq!(
received.iter().map(|e| e.name()).collect::<Vec<_>>(),
vec!["ABC", "def"]
);
}

#[test]
#[traced_test]
fn no_finish() {
let (sender, receiver) = mpsc::unbounded_channel();
let error_collector = ErrorCollector::<String>::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\")"));
}
}

0 comments on commit 1df838a

Please sign in to comment.