1 | use super::*; |
2 | use tracing_mock::layer::MockLayer; |
3 | use tracing_subscriber::{filter, prelude::*, Layer}; |
4 | |
5 | fn layer() -> (MockLayer, subscriber::MockHandle) { |
6 | layer::mock().only().run_with_handle() |
7 | } |
8 | |
9 | fn filter<S>() -> filter::DynFilterFn<S> { |
10 | // Use dynamic filter fn to disable interest caching and max-level hints, |
11 | // allowing us to put all of these tests in the same file. |
12 | filter::dynamic_filter_fn(|_, _| false) |
13 | } |
14 | |
15 | /// reproduces https://github.com/tokio-rs/tracing/issues/1563#issuecomment-921363629 |
16 | #[test] |
17 | fn box_works() { |
18 | let (layer, handle) = layer(); |
19 | let layer = Box::new(layer.with_filter(filter())); |
20 | |
21 | let _guard = tracing_subscriber::registry().with(layer).set_default(); |
22 | |
23 | for i in 0..2 { |
24 | tracing::info!(i); |
25 | } |
26 | |
27 | handle.assert_finished(); |
28 | } |
29 | |
30 | /// the same as `box_works` but with a type-erased `Box`. |
31 | #[test] |
32 | fn dyn_box_works() { |
33 | let (layer, handle) = layer(); |
34 | let layer: Box<dyn Layer<_> + Send + Sync + 'static> = Box::new(layer.with_filter(filter())); |
35 | |
36 | let _guard = tracing_subscriber::registry().with(layer).set_default(); |
37 | |
38 | for i in 0..2 { |
39 | tracing::info!(i); |
40 | } |
41 | |
42 | handle.assert_finished(); |
43 | } |
44 | |