1#![cfg(feature = "registry")]
2use tracing::{Level, Metadata, Subscriber};
3use tracing_mock::{
4 expect, layer,
5 layer::MockLayer,
6 subscriber::{self},
7};
8use tracing_subscriber::{filter::DynFilterFn, layer::Context, prelude::*};
9
10#[test]
11fn layer_filters() {
12 let (unfiltered, unfiltered_handle) = unfiltered("unfiltered");
13 let (filtered, filtered_handle) = filtered("filtered");
14
15 let subscriber = tracing_subscriber::registry()
16 .with(unfiltered)
17 .with(filtered.with_filter(filter()));
18 assert_eq!(subscriber.max_level_hint(), None);
19 let _subscriber = subscriber.set_default();
20
21 events();
22
23 unfiltered_handle.assert_finished();
24 filtered_handle.assert_finished();
25}
26
27#[test]
28fn layered_layer_filters() {
29 let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1");
30 let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2");
31 let unfiltered = unfiltered1.and_then(unfiltered2);
32
33 let (filtered1, filtered1_handle) = filtered("filtered_1");
34 let (filtered2, filtered2_handle) = filtered("filtered_2");
35 let filtered = filtered1
36 .with_filter(filter())
37 .and_then(filtered2.with_filter(filter()));
38
39 let subscriber = tracing_subscriber::registry()
40 .with(unfiltered)
41 .with(filtered);
42 assert_eq!(subscriber.max_level_hint(), None);
43 let _subscriber = subscriber.set_default();
44
45 events();
46
47 unfiltered1_handle.assert_finished();
48 unfiltered2_handle.assert_finished();
49 filtered1_handle.assert_finished();
50 filtered2_handle.assert_finished();
51}
52
53#[test]
54fn out_of_order() {
55 let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1");
56 let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2");
57
58 let (filtered1, filtered1_handle) = filtered("filtered_1");
59 let (filtered2, filtered2_handle) = filtered("filtered_2");
60
61 let subscriber = tracing_subscriber::registry()
62 .with(unfiltered1)
63 .with(filtered1.with_filter(filter()))
64 .with(unfiltered2)
65 .with(filtered2.with_filter(filter()));
66 assert_eq!(subscriber.max_level_hint(), None);
67 let _subscriber = subscriber.set_default();
68
69 events();
70
71 unfiltered1_handle.assert_finished();
72 unfiltered2_handle.assert_finished();
73 filtered1_handle.assert_finished();
74 filtered2_handle.assert_finished();
75}
76
77#[test]
78fn mixed_layered() {
79 let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1");
80 let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2");
81 let (filtered1, filtered1_handle) = filtered("filtered_1");
82 let (filtered2, filtered2_handle) = filtered("filtered_2");
83
84 let layered1 = filtered1.with_filter(filter()).and_then(unfiltered1);
85 let layered2 = unfiltered2.and_then(filtered2.with_filter(filter()));
86
87 let subscriber = tracing_subscriber::registry().with(layered1).with(layered2);
88 assert_eq!(subscriber.max_level_hint(), None);
89 let _subscriber = subscriber.set_default();
90
91 events();
92
93 unfiltered1_handle.assert_finished();
94 unfiltered2_handle.assert_finished();
95 filtered1_handle.assert_finished();
96 filtered2_handle.assert_finished();
97}
98
99fn events() {
100 tracing::trace!("hello trace");
101 tracing::debug!("hello debug");
102 tracing::info!("hello info");
103 tracing::warn!("hello warn");
104 tracing::error!("hello error");
105}
106
107fn filter<S>() -> DynFilterFn<S> {
108 DynFilterFn::new(
109 (|metadata: &Metadata<'_>, _: &tracing_subscriber::layer::Context<'_, S>| {
110 metadata.level() <= &Level::INFO
111 }) as fn(&Metadata<'_>, &Context<'_, S>) -> bool,
112 )
113 .with_max_level_hint(Level::INFO)
114}
115
116fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) {
117 layer::named(name)
118 .event(expect::event().at_level(Level::TRACE))
119 .event(expect::event().at_level(Level::DEBUG))
120 .event(expect::event().at_level(Level::INFO))
121 .event(expect::event().at_level(Level::WARN))
122 .event(expect::event().at_level(Level::ERROR))
123 .only()
124 .run_with_handle()
125}
126
127fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) {
128 layer::named(name)
129 .event(expect::event().at_level(Level::INFO))
130 .event(expect::event().at_level(Level::WARN))
131 .event(expect::event().at_level(Level::ERROR))
132 .only()
133 .run_with_handle()
134}
135