-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable logs in tests * Add tests for error_collector * Switch to codecov.io * Move tests to separate files, to ignore them in coverage * Update coverage badge
- Loading branch information
Showing
16 changed files
with
591 additions
and
533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::BoxedError; | ||
|
||
use super::*; | ||
|
||
fn examine_report(report: miette::Report) { | ||
println!("{}", report); | ||
println!("{:?}", report); | ||
// Convert to std::error::Error | ||
let boxed_error: BoxedError = report.into(); | ||
println!("{}", boxed_error); | ||
println!("{:?}", boxed_error); | ||
} | ||
|
||
#[test] | ||
fn errors_can_be_converted_to_diagnostic() { | ||
examine_report(GracefulShutdownError::ShutdownTimeout::<BoxedError>(Box::new([])).into()); | ||
examine_report(GracefulShutdownError::SubsystemsFailed::<BoxedError>(Box::new([])).into()); | ||
examine_report(SubsystemJoinError::SubsystemsFailed::<BoxedError>(Arc::new([])).into()); | ||
examine_report(SubsystemError::Panicked::<BoxedError>("".into()).into()); | ||
examine_report( | ||
SubsystemError::Failed::<BoxedError>("".into(), SubsystemFailure("".into())).into(), | ||
); | ||
examine_report(CancelledByShutdown.into()); | ||
} | ||
|
||
#[test] | ||
fn extract_related_from_graceful_shutdown_error() { | ||
let related = || { | ||
Box::new([ | ||
SubsystemError::Failed("a".into(), SubsystemFailure(String::from("A").into())), | ||
SubsystemError::Panicked("b".into()), | ||
]) | ||
}; | ||
|
||
let matches_related = |data: &[SubsystemError<BoxedError>]| { | ||
let mut iter = data.iter(); | ||
|
||
let elem = iter.next().unwrap(); | ||
assert_eq!(elem.name(), "a"); | ||
assert!(matches!(elem, SubsystemError::Failed(_, _))); | ||
|
||
let elem = iter.next().unwrap(); | ||
assert_eq!(elem.name(), "b"); | ||
assert!(matches!(elem, SubsystemError::Panicked(_))); | ||
|
||
assert!(iter.next().is_none()); | ||
}; | ||
|
||
matches_related(GracefulShutdownError::ShutdownTimeout(related()).get_subsystem_errors()); | ||
matches_related(GracefulShutdownError::SubsystemsFailed(related()).get_subsystem_errors()); | ||
matches_related(&GracefulShutdownError::ShutdownTimeout(related()).into_subsystem_errors()); | ||
matches_related(&GracefulShutdownError::SubsystemsFailed(related()).into_subsystem_errors()); | ||
} | ||
|
||
#[test] | ||
fn extract_contained_error_from_convert_subsystem_failure() { | ||
let msg = "MyFailure".to_string(); | ||
let failure = SubsystemFailure(msg.clone()); | ||
|
||
assert_eq!(&msg, failure.get_error()); | ||
assert_eq!(msg, *failure); | ||
assert_eq!(msg, failure.into_error()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
use tracing_test::traced_test; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
#[traced_test] | ||
fn finished_callback() { | ||
let alive_guard = AliveGuard::new(); | ||
|
||
let counter = Arc::new(AtomicU32::new(0)); | ||
let counter2 = Arc::clone(&counter); | ||
|
||
alive_guard.on_finished(move || { | ||
counter2.fetch_add(1, Ordering::Relaxed); | ||
}); | ||
|
||
drop(alive_guard); | ||
|
||
assert_eq!(counter.load(Ordering::Relaxed), 1); | ||
} | ||
|
||
#[test] | ||
#[traced_test] | ||
fn cancel_callback() { | ||
let alive_guard = AliveGuard::new(); | ||
|
||
let counter = Arc::new(AtomicU32::new(0)); | ||
let counter2 = Arc::clone(&counter); | ||
|
||
alive_guard.on_finished(|| {}); | ||
alive_guard.on_cancel(move || { | ||
counter2.fetch_add(1, Ordering::Relaxed); | ||
}); | ||
|
||
drop(alive_guard); | ||
|
||
assert_eq!(counter.load(Ordering::Relaxed), 1); | ||
} | ||
|
||
#[test] | ||
#[traced_test] | ||
fn both_callbacks() { | ||
let alive_guard = AliveGuard::new(); | ||
|
||
let counter = Arc::new(AtomicU32::new(0)); | ||
let counter2 = Arc::clone(&counter); | ||
let counter3 = Arc::clone(&counter); | ||
|
||
alive_guard.on_finished(move || { | ||
counter2.fetch_add(1, Ordering::Relaxed); | ||
}); | ||
alive_guard.on_cancel(move || { | ||
counter3.fetch_add(1, Ordering::Relaxed); | ||
}); | ||
|
||
drop(alive_guard); | ||
|
||
assert_eq!(counter.load(Ordering::Relaxed), 2); | ||
} | ||
|
||
#[test] | ||
#[traced_test] | ||
fn no_callback() { | ||
let alive_guard = AliveGuard::new(); | ||
drop(alive_guard); | ||
|
||
assert!(logs_contain("No `finished` callback was registered in AliveGuard! This should not happen, please report this at https://github.com/Finomnis/tokio-graceful-shutdown/issues.")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ impl<ErrType: ErrTypeTraits> Drop for ErrorCollector<ErrType> { | |
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
Oops, something went wrong.