aboutsummaryrefslogtreecommitdiff
path: root/tests/local_dispatch_before_init.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/local_dispatch_before_init.rs')
-rw-r--r--tests/local_dispatch_before_init.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/local_dispatch_before_init.rs b/tests/local_dispatch_before_init.rs
new file mode 100644
index 0000000..71b67f8
--- /dev/null
+++ b/tests/local_dispatch_before_init.rs
@@ -0,0 +1,43 @@
+mod common;
+
+use common::*;
+use tracing_core::{
+ dispatcher::{self, Dispatch},
+ subscriber::NoSubscriber,
+};
+
+/// 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"
+ );
+ });
+}