1 use tracing::{subscriber::with_default, Id, Level};
2 use tracing_attributes::instrument;
3 use tracing_mock::*;
4
5 #[instrument]
with_default_parent()6 fn with_default_parent() {}
7
8 #[instrument(parent = parent_span, skip(parent_span))]
with_explicit_parent<P>(parent_span: P) where P: Into<Option<Id>>,9 fn with_explicit_parent<P>(parent_span: P)
10 where
11 P: Into<Option<Id>>,
12 {
13 }
14
15 #[test]
default_parent_test()16 fn default_parent_test() {
17 let contextual_parent = expect::span().named("contextual_parent");
18 let child = expect::span().named("with_default_parent");
19
20 let (subscriber, handle) = subscriber::mock()
21 .new_span(
22 contextual_parent
23 .clone()
24 .with_ancestry(expect::is_contextual_root()),
25 )
26 .new_span(child.clone().with_ancestry(expect::is_contextual_root()))
27 .enter(child.clone())
28 .exit(child.clone())
29 .enter(contextual_parent.clone())
30 .new_span(
31 child
32 .clone()
33 .with_ancestry(expect::has_contextual_parent("contextual_parent")),
34 )
35 .enter(child.clone())
36 .exit(child)
37 .exit(contextual_parent)
38 .only()
39 .run_with_handle();
40
41 with_default(subscriber, || {
42 let contextual_parent = tracing::span!(Level::TRACE, "contextual_parent");
43
44 with_default_parent();
45
46 contextual_parent.in_scope(|| {
47 with_default_parent();
48 });
49 });
50
51 handle.assert_finished();
52 }
53
54 #[test]
explicit_parent_test()55 fn explicit_parent_test() {
56 let contextual_parent = expect::span().named("contextual_parent");
57 let explicit_parent = expect::span().named("explicit_parent");
58 let child = expect::span().named("with_explicit_parent");
59
60 let (subscriber, handle) = subscriber::mock()
61 .new_span(
62 contextual_parent
63 .clone()
64 .with_ancestry(expect::is_contextual_root()),
65 )
66 .new_span(explicit_parent.with_ancestry(expect::is_contextual_root()))
67 .enter(contextual_parent.clone())
68 .new_span(
69 child
70 .clone()
71 .with_ancestry(expect::has_explicit_parent("explicit_parent")),
72 )
73 .enter(child.clone())
74 .exit(child)
75 .exit(contextual_parent)
76 .only()
77 .run_with_handle();
78
79 with_default(subscriber, || {
80 let contextual_parent = tracing::span!(Level::INFO, "contextual_parent");
81 let explicit_parent = tracing::span!(Level::INFO, "explicit_parent");
82
83 contextual_parent.in_scope(|| {
84 with_explicit_parent(&explicit_parent);
85 });
86 });
87
88 handle.assert_finished();
89 }
90