File: local_dispatch_before_init.rs

package info (click to toggle)
rust-tracing-core 0.1.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: makefile: 4
file content (43 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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"
        );
    });
}