Skip to content

Commit

Permalink
add test reproducing #2587, #2436, and #2411
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed May 11, 2023
1 parent 3de7f8c commit e42ccc5
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tracing-core/tests/local_dispatch_before_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mod common;

use common::*;
use tracing_core::{subscriber::NoSubscriber, dispatcher::{Dispatch, self}};

/// This test reproduces the following issues:
/// - https://github.com/tokio-rs/tracing/issues/2587
/// - https://github.com/tokio-rs/tracing/issues/2411
/// - https://github.com/tokio-rs/tracing/issues/2436
#[test]
fn local_dispatch_before_init() {
dispatcher::get_default(|current| assert!(dbg!(current).is::<NoSubscriber>()));

// Temporarily override the default dispatcher with a scoped dispatcher.
// Using a scoped dispatcher makes the thread local state attempt to cache
// the scoped default.
#[cfg(feature = "std")] {
dispatcher::with_default(&Dispatch::new(TestSubscriberB), || {
dispatcher::get_default(|current| {
assert!(
dbg!(current).is::<TestSubscriberB>(),
"overriden subscriber not set",
);
})
})
}

dispatcher::get_default(|current| assert!(current.is::<NoSubscriber>()));

dispatcher::set_global_default(Dispatch::new(TestSubscriberA))
.expect("set global dispatch failed");

dispatcher::get_default(|current| {
assert!(
dbg!(current).is::<TestSubscriberA>(),
"default subscriber not set"
);
});

}

0 comments on commit e42ccc5

Please sign in to comment.