From e42ccc5d86ca0fe8f5ae915a961e1db000bd48d7 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 11 May 2023 10:56:40 -0700 Subject: [PATCH] add test reproducing #2587, #2436, and #2411 --- .../tests/local_dispatch_before_init.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tracing-core/tests/local_dispatch_before_init.rs diff --git a/tracing-core/tests/local_dispatch_before_init.rs b/tracing-core/tests/local_dispatch_before_init.rs new file mode 100644 index 0000000000..63e714244b --- /dev/null +++ b/tracing-core/tests/local_dispatch_before_init.rs @@ -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::())); + + // 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::(), + "overriden subscriber not set", + ); + }) + }) + } + + dispatcher::get_default(|current| assert!(current.is::())); + + dispatcher::set_global_default(Dispatch::new(TestSubscriberA)) + .expect("set global dispatch failed"); + + dispatcher::get_default(|current| { + assert!( + dbg!(current).is::(), + "default subscriber not set" + ); + }); + +}