1 #![cfg(all(feature = "env-filter", feature = "fmt"))]
2 use tracing::{self, subscriber::with_default, Span};
3 use tracing_subscriber::{filter::EnvFilter, FmtSubscriber};
4 
5 #[test]
duplicate_spans()6 fn duplicate_spans() {
7     let subscriber = FmtSubscriber::builder()
8         .with_env_filter(EnvFilter::new("[root]=debug"))
9         .finish();
10 
11     with_default(subscriber, || {
12         let root = tracing::debug_span!("root");
13         root.in_scope(|| {
14             // root:
15             assert_eq!(root, Span::current(), "Current span must be 'root'");
16             let leaf = tracing::debug_span!("leaf");
17             leaf.in_scope(|| {
18                 // root:leaf:
19                 assert_eq!(leaf, Span::current(), "Current span must be 'leaf'");
20                 root.in_scope(|| {
21                     // root:leaf:
22                     assert_eq!(
23                         leaf,
24                         Span::current(),
25                         "Current span must be 'leaf' after entering twice the 'root' span"
26                     );
27                 })
28             });
29             // root:
30             assert_eq!(
31                 root,
32                 Span::current(),
33                 "Current span must be root ('leaf' exited, nested 'root' exited)"
34             );
35 
36             root.in_scope(|| {
37                 assert_eq!(root, Span::current(), "Current span must be root");
38             });
39             // root:
40             assert_eq!(
41                 root,
42                 Span::current(),
43                 "Current span must still be root after exiting nested 'root'"
44             );
45         });
46     });
47 }
48