Skip to content

Commit

Permalink
test(maitake): properly capture tracing messages (#445)
Browse files Browse the repository at this point in the history
The "init tracing" test helper returned a guard that the caller
is supposed to hold, but none (or not all?) of the tests that called it
actually held it.

This makes a single return type (because `rustc` was Big Mad when I used
`Box<dyn Drop>`) based on the features, and marks it as must use.

This change allows for capturing (or viewing) of tracing output during
timer wheel tests.

Closes #444
  • Loading branch information
jamesmunns authored and hawkw committed Jun 20, 2023
1 parent d2694a8 commit cb0de4e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
3 changes: 2 additions & 1 deletion maitake/src/time/timer/tests/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use loom::model;

#[cfg(not(loom))]
fn model(f: impl Fn() + Send + Sync + 'static) {
crate::util::test::trace_init_with_default("info,maitake::time=trace");
let guard = crate::util::test::trace_init_with_default("info,maitake::time=trace");
f();
drop(guard);
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion maitake/src/time/timer/tests/wheel_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use crate::scheduler::Scheduler;
use crate::util::test::TestGuard;
use std::collections::BTreeMap;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Expand All @@ -14,6 +15,9 @@ struct SleepGroupTest {
now: Ticks,
groups: BTreeMap<Ticks, SleepGroup>,
next_id: usize,

// Hold on to this so the subscriber doesn't go away
_guard: TestGuard,
}

struct SleepGroup {
Expand All @@ -26,13 +30,14 @@ struct SleepGroup {

impl SleepGroupTest {
fn new(timer: &'static Timer) -> Self {
crate::util::test::trace_init_with_default("info,maitake::time=trace");
let _guard = crate::util::test::trace_init_with_default("info,maitake::time=trace");
Self {
scheduler: Scheduler::new(),
timer,
now: 0,
groups: BTreeMap::new(),
next_id: 0,
_guard,
}
}

Expand Down
42 changes: 37 additions & 5 deletions maitake/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,34 @@ pub(crate) fn expect_display<T, E: core::fmt::Display>(result: Result<T, E>, msg

#[cfg(test)]
pub(crate) mod test {
/// A guard that represents the tracing default subscriber guard
///
/// *should* be held until the end of the test, to ensure that tracing messages
/// actually make it to the fmt subscriber for the entire test.
///
/// Exists to abstract over tracing 01/02 guard type differences.
#[must_use]
pub struct TestGuard {
#[cfg(not(loom))]
_x2: tracing_02::collect::DefaultGuard,
#[cfg(loom)]
_x1: tracing_01::subscriber::DefaultGuard,
}

pub(crate) fn trace_init() -> impl Drop {
/// Initialize tracing with a default filter directive
///
/// Returns a [TestGuard] that must be held for the duration of test to ensure
/// tracing messages are correctly output
pub(crate) fn trace_init() -> TestGuard {
trace_init_with_default("maitake=debug,cordyceps=debug")
}

/// Initialize tracing with the given filter directive
///
/// Returns a [TestGuard] that must be held for the duration of test to ensure
/// tracing messages are correctly output
#[cfg(not(loom))]
pub(crate) fn trace_init_with_default(default: &str) -> impl Drop {
pub(crate) fn trace_init_with_default(default: &str) -> TestGuard {
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
let env = std::env::var("RUST_LOG").unwrap_or_default();
let builder = EnvFilter::builder().with_default_directive(LevelFilter::INFO.into());
Expand All @@ -115,11 +136,18 @@ pub(crate) mod test {
.with_test_writer()
.without_time()
.finish();
tracing_02::collect::set_default(collector)

TestGuard {
_x2: tracing_02::collect::set_default(collector),
}
}

/// Initialize tracing with the given filter directive
///
/// Returns a [TestGuard] that must be held for the duration of test to ensure
/// tracing messages are correctly output
#[cfg(loom)]
pub(crate) fn trace_init_with_default(default: &str) -> impl Drop {
pub(crate) fn trace_init_with_default(default: &str) -> TestGuard {
use tracing_subscriber_03::filter::{EnvFilter, LevelFilter};
let env = std::env::var("LOOM_LOG").unwrap_or_default();
let builder = EnvFilter::builder().with_default_directive(LevelFilter::INFO.into());
Expand All @@ -138,7 +166,11 @@ pub(crate) mod test {
.with_test_writer()
.without_time()
.finish();
tracing_01::subscriber::set_default(collector)

use tracing_subscriber_03::util::SubscriberInitExt;
TestGuard {
_x1: collector.set_default(),
}
}

#[allow(dead_code)]
Expand Down

0 comments on commit cb0de4e

Please sign in to comment.