1 use tracing::{subscriber::with_default, Id, Level, Span};
2 use tracing_attributes::instrument;
3 use tracing_mock::*;
4
5 #[instrument(follows_from = causes, skip(causes))]
with_follows_from_sync(causes: impl IntoIterator<Item = impl Into<Option<Id>>>)6 fn with_follows_from_sync(causes: impl IntoIterator<Item = impl Into<Option<Id>>>) {}
7
8 #[instrument(follows_from = causes, skip(causes))]
with_follows_from_async(causes: impl IntoIterator<Item = impl Into<Option<Id>>>)9 async fn with_follows_from_async(causes: impl IntoIterator<Item = impl Into<Option<Id>>>) {}
10
11 #[instrument(follows_from = [&Span::current()])]
follows_from_current()12 fn follows_from_current() {}
13
14 #[test]
follows_from_sync_test()15 fn follows_from_sync_test() {
16 let cause_a = span::mock().named("cause_a");
17 let cause_b = span::mock().named("cause_b");
18 let cause_c = span::mock().named("cause_c");
19 let consequence = span::mock().named("with_follows_from_sync");
20
21 let (subscriber, handle) = subscriber::mock()
22 .new_span(cause_a.clone())
23 .new_span(cause_b.clone())
24 .new_span(cause_c.clone())
25 .new_span(consequence.clone())
26 .follows_from(consequence.clone(), cause_a)
27 .follows_from(consequence.clone(), cause_b)
28 .follows_from(consequence.clone(), cause_c)
29 .enter(consequence.clone())
30 .exit(consequence)
31 .done()
32 .run_with_handle();
33
34 with_default(subscriber, || {
35 let cause_a = tracing::span!(Level::TRACE, "cause_a");
36 let cause_b = tracing::span!(Level::TRACE, "cause_b");
37 let cause_c = tracing::span!(Level::TRACE, "cause_c");
38
39 with_follows_from_sync(&[cause_a, cause_b, cause_c])
40 });
41
42 handle.assert_finished();
43 }
44
45 #[test]
follows_from_async_test()46 fn follows_from_async_test() {
47 let cause_a = span::mock().named("cause_a");
48 let cause_b = span::mock().named("cause_b");
49 let cause_c = span::mock().named("cause_c");
50 let consequence = span::mock().named("with_follows_from_async");
51
52 let (subscriber, handle) = subscriber::mock()
53 .new_span(cause_a.clone())
54 .new_span(cause_b.clone())
55 .new_span(cause_c.clone())
56 .new_span(consequence.clone())
57 .follows_from(consequence.clone(), cause_a)
58 .follows_from(consequence.clone(), cause_b)
59 .follows_from(consequence.clone(), cause_c)
60 .enter(consequence.clone())
61 .exit(consequence)
62 .done()
63 .run_with_handle();
64
65 with_default(subscriber, || {
66 block_on_future(async {
67 let cause_a = tracing::span!(Level::TRACE, "cause_a");
68 let cause_b = tracing::span!(Level::TRACE, "cause_b");
69 let cause_c = tracing::span!(Level::TRACE, "cause_c");
70
71 with_follows_from_async(&[cause_a, cause_b, cause_c]).await
72 })
73 });
74
75 handle.assert_finished();
76 }
77
78 #[test]
follows_from_current_test()79 fn follows_from_current_test() {
80 let cause = span::mock().named("cause");
81 let consequence = span::mock().named("follows_from_current");
82
83 let (subscriber, handle) = subscriber::mock()
84 .new_span(cause.clone())
85 .enter(cause.clone())
86 .new_span(consequence.clone())
87 .follows_from(consequence.clone(), cause.clone())
88 .enter(consequence.clone())
89 .exit(consequence)
90 .exit(cause)
91 .done()
92 .run_with_handle();
93
94 with_default(subscriber, || {
95 tracing::span!(Level::TRACE, "cause").in_scope(follows_from_current)
96 });
97
98 handle.assert_finished();
99 }
100