Skip to content
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

Improve test coverage #65

Merged
merged 13 commits into from
Oct 20, 2023
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ miette = { version = "5.10.0", features = ["fancy"] }

# Logging
tracing-subscriber = "0.3.17"
tracing-test = "0.2.4"
tracing-test = { version = "0.2.4", features = [
"no-env-filter",
"llvm-cov-compat",
], branch = "fix_coverage", git = "https://github.com/Finomnis/tracing-test.git" }

# Tokio
tokio = { version = "1.32.0", features = ["full"] }
Expand Down
1 change: 1 addition & 0 deletions src/error_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum ErrorAction {
mod tests {
use super::*;
#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn derive_traits() {
let x = ErrorAction::CatchAndLocalShutdown;
#[allow(clippy::clone_on_copy)]
Expand Down
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod tests {

use super::*;

#[cfg_attr(coverage_nightly, coverage(off))]
fn examine_report(report: miette::Report) {
println!("{}", report);
println!("{:?}", report);
Expand All @@ -142,6 +143,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn errors_can_be_converted_to_diagnostic() {
examine_report(GracefulShutdownError::ShutdownTimeout::<BoxedError>(Box::new([])).into());
examine_report(GracefulShutdownError::SubsystemsFailed::<BoxedError>(Box::new([])).into());
Expand All @@ -154,6 +156,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn extract_related_from_graceful_shutdown_error() {
let related = || {
Box::new([
Expand Down Expand Up @@ -185,6 +188,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn extract_contained_error_from_convert_subsystem_failure() {
let msg = "MyFailure".to_string();
let failure = SubsystemFailure(msg.clone());
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
test(no_crate_inject, attr(deny(warnings))),
test(attr(allow(dead_code)))
)]
// Required for test coverage
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

type BoxedError = Box<dyn std::error::Error + Send + Sync + 'static>;

Expand Down
17 changes: 17 additions & 0 deletions src/runner/alive_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ impl Drop for Inner {
mod tests {

use std::sync::atomic::{AtomicU32, Ordering};
use tracing_test::traced_test;

use super::*;

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn finished_callback() {
let alive_guard = AliveGuard::new();

Expand All @@ -82,6 +85,8 @@ mod tests {
}

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn cancel_callback() {
let alive_guard = AliveGuard::new();

Expand All @@ -99,6 +104,8 @@ mod tests {
}

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn both_callbacks() {
let alive_guard = AliveGuard::new();

Expand All @@ -117,4 +124,14 @@ mod tests {

assert_eq!(counter.load(Ordering::Relaxed), 2);
}

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
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."));
}
}
76 changes: 76 additions & 0 deletions src/subsystem/error_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,79 @@ impl<ErrType: ErrTypeTraits> Drop for ErrorCollector<ErrType> {
}
}
}

#[cfg(test)]
mod tests {

use tracing_test::traced_test;

use super::*;

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
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]
#[cfg_attr(coverage_nightly, coverage(off))]
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]
#[cfg_attr(coverage_nightly, coverage(off))]
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\")"));
}
}
2 changes: 2 additions & 0 deletions src/subsystem/subsystem_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ mod tests {
use crate::subsystem::SubsystemBuilder;

#[tokio::test]
#[cfg_attr(coverage_nightly, coverage(off))]
async fn recursive_cancellation() {
let root_handle = root_handle::<BoxedError>(|_| {});

Expand All @@ -388,6 +389,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(coverage_nightly, coverage(off))]
async fn recursive_cancellation_2() {
let root_handle = root_handle(|_| {});

Expand Down
6 changes: 6 additions & 0 deletions src/utils/joiner_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ mod tests {

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn counters() {
let (root, _) = JoinerToken::<BoxedError>::new(|_| None);
assert_eq!(0, root.count());
Expand Down Expand Up @@ -221,6 +222,7 @@ mod tests {

#[test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn counters_weak() {
let (root, weak_root) = JoinerToken::<BoxedError>::new(|_| None);
assert_eq!(0, weak_root.count());
Expand Down Expand Up @@ -293,6 +295,7 @@ mod tests {

#[tokio::test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
async fn join() {
let (superroot, _) = JoinerToken::<BoxedError>::new(|_| None);

Expand Down Expand Up @@ -335,6 +338,7 @@ mod tests {

#[tokio::test]
#[traced_test]
#[cfg_attr(coverage_nightly, coverage(off))]
async fn join_through_ref() {
let (root, joiner) = JoinerToken::<BoxedError>::new(|_| None);

Expand Down Expand Up @@ -372,6 +376,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn debug_print() {
let (root, _) = JoinerToken::<BoxedError>::new(|_| None);
assert_eq!(format!("{:?}", root), "JoinerToken(children = 0)");
Expand All @@ -384,6 +389,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn debug_print_ref() {
let (root, root_ref) = JoinerToken::<BoxedError>::new(|_| None);
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions src/utils/remote_drop_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod tests {
use crate::{utils::JoinerToken, BoxedError};

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn insert_and_drop() {
let items = RemotelyDroppableItems::new();

Expand All @@ -109,6 +110,7 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn drop_token() {
let items = RemotelyDroppableItems::new();

Expand Down
3 changes: 3 additions & 0 deletions tests/cancel_on_shutdown.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Required for test coverage
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

use tokio::time::{sleep, Duration};
use tokio_graceful_shutdown::{
errors::CancelledByShutdown, FutureExt, SubsystemBuilder, SubsystemHandle, Toplevel,
Expand Down
3 changes: 3 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Required for test coverage
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

use anyhow::anyhow;
use tokio::time::{sleep, timeout, Duration};
use tokio_graceful_shutdown::{
Expand Down
23 changes: 23 additions & 0 deletions tests/integration_test_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Required for test coverage
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

use anyhow::anyhow;

Check warning

Code scanning / clippy

unused import: anyhow::anyhow Warning test

unused import: anyhow::anyhow
use tokio::time::{sleep, timeout, Duration};

Check warning

Code scanning / clippy

unused imports: Duration, sleep, timeout Warning test

unused imports: Duration, sleep, timeout

Check warning

Code scanning / clippy

unused imports: Duration, sleep, timeout Warning test

unused imports: Duration, sleep, timeout

Check warning

Code scanning / clippy

unused imports: Duration, sleep, timeout Warning test

unused imports: Duration, sleep, timeout
use tokio_graceful_shutdown::{
errors::{GracefulShutdownError, SubsystemError, SubsystemJoinError},

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel
ErrorAction, IntoSubsystem, SubsystemBuilder, SubsystemHandle, Toplevel,

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel

Check warning

Code scanning / clippy

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel Warning test

unused imports: ErrorAction, GracefulShutdownError, IntoSubsystem, SubsystemBuilder, SubsystemError, SubsystemHandle, SubsystemJoinError, Toplevel
};
use tracing_test::traced_test;

pub mod common;
use common::Event;

Check warning

Code scanning / clippy

unused import: common::Event Warning test

unused import: common::Event

use std::error::Error;

/// Wrapper function to simplify lambdas
type BoxedError = Box<dyn Error + Sync + Send>;

Check warning

Code scanning / clippy

type alias BoxedError is never used Warning test

type alias BoxedError is never used
type BoxedResult = Result<(), BoxedError>;

Check warning

Code scanning / clippy

type alias BoxedResult is never used Warning test

type alias BoxedResult is never used

#[tokio::test]
#[traced_test]
async fn dummy() {}
Loading