1 mod common;
2 
3 use common::*;
4 use tracing_core::{
5     dispatcher::{self, Dispatch},
6     subscriber::NoSubscriber,
7 };
8 
9 /// This test reproduces the following issues:
10 /// - https://github.com/tokio-rs/tracing/issues/2587
11 /// - https://github.com/tokio-rs/tracing/issues/2411
12 /// - https://github.com/tokio-rs/tracing/issues/2436
13 #[test]
local_dispatch_before_init()14 fn local_dispatch_before_init() {
15     dispatcher::get_default(|current| assert!(dbg!(current).is::<NoSubscriber>()));
16 
17     // Temporarily override the default dispatcher with a scoped dispatcher.
18     // Using a scoped dispatcher makes the thread local state attempt to cache
19     // the scoped default.
20     #[cfg(feature = "std")]
21     {
22         dispatcher::with_default(&Dispatch::new(TestSubscriberB), || {
23             dispatcher::get_default(|current| {
24                 assert!(
25                     dbg!(current).is::<TestSubscriberB>(),
26                     "overriden subscriber not set",
27                 );
28             })
29         })
30     }
31 
32     dispatcher::get_default(|current| assert!(current.is::<NoSubscriber>()));
33 
34     dispatcher::set_global_default(Dispatch::new(TestSubscriberA))
35         .expect("set global dispatch failed");
36 
37     dispatcher::get_default(|current| {
38         assert!(
39             dbg!(current).is::<TestSubscriberA>(),
40             "default subscriber not set"
41         );
42     });
43 }
44