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