Skip to content

Commit 5a30de9

Browse files
hawkwkaffarell
authored andcommitted
mock: move layer mock from tracing-subscriber tests (tokio-rs#2369)
The `tracing-subscriber` module `tests::support` included functionality to mock a layer (via the `Layer` trait). This code depends on some items from `tracing_mock::collector` which should otherwise not be public. This change moves the mocking functionality inside `tracing-mock` behind a feature flag. Allowing the `Expect` enum and `MockHandle::new` from `tracing_mock::collector` to be made `pub(crate)` instead of `pub`. Since it's now used from two different modules, the `Expect` enum has been moved to its own module. This requires a lot of modifications to imports so that we're not doing wildcard imports from another crate (i.e. in `tracing-subscriber` importing wildcards from `tracing-mock`). This PR is based on @hds' PR tokio-rs#2369, but modified to track renamings. I also deleted all the doc comments temporarily because updating them was a lot of work and I need to get a release of `tracing-subscriber` out first. Closes: tokio-rs#2359
1 parent 2cbe91b commit 5a30de9

15 files changed

+113
-102
lines changed

tracing-mock/src/expectation.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::{
2+
event::MockEvent,
3+
field,
4+
span::{MockSpan, NewSpan},
5+
};
6+
7+
#[derive(Debug, Eq, PartialEq)]
8+
pub(crate) enum Expect {
9+
Event(MockEvent),
10+
FollowsFrom {
11+
consequence: MockSpan,
12+
cause: MockSpan,
13+
},
14+
Enter(MockSpan),
15+
Exit(MockSpan),
16+
CloneSpan(MockSpan),
17+
DropSpan(MockSpan),
18+
Visit(MockSpan, field::Expect),
19+
NewSpan(NewSpan),
20+
Nothing,
21+
}

tracing-subscriber/tests/support.rs renamed to tracing-mock/src/layer.rs

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#![allow(missing_docs, dead_code)]
2-
pub use tracing_mock::{event, field, span, subscriber};
3-
1+
use crate::{
2+
event::MockEvent,
3+
expectation::Expect,
4+
span::{MockSpan, NewSpan},
5+
subscriber::MockHandle,
6+
};
47
use tracing_core::{
58
span::{Attributes, Id, Record},
69
Event, Subscriber,
710
};
8-
use tracing_mock::{
9-
event::MockEvent,
10-
span::{MockSpan, NewSpan},
11-
subscriber::{Expect, MockHandle},
12-
};
1311
use tracing_subscriber::{
1412
layer::{Context, Layer},
1513
registry::{LookupSpan, SpanRef},
@@ -21,49 +19,34 @@ use std::{
2119
sync::{Arc, Mutex},
2220
};
2321

24-
pub mod layer {
25-
use super::ExpectLayerBuilder;
26-
27-
pub fn mock() -> ExpectLayerBuilder {
28-
ExpectLayerBuilder {
29-
expected: Default::default(),
30-
name: std::thread::current()
31-
.name()
32-
.map(String::from)
33-
.unwrap_or_default(),
34-
}
22+
#[must_use]
23+
pub fn mock() -> MockLayerBuilder {
24+
MockLayerBuilder {
25+
expected: Default::default(),
26+
name: std::thread::current()
27+
.name()
28+
.map(String::from)
29+
.unwrap_or_default(),
3530
}
31+
}
3632

37-
pub fn named(name: impl std::fmt::Display) -> ExpectLayerBuilder {
38-
mock().named(name)
39-
}
33+
#[must_use]
34+
pub fn named(name: impl std::fmt::Display) -> MockLayerBuilder {
35+
mock().named(name)
4036
}
4137

42-
pub struct ExpectLayerBuilder {
38+
pub struct MockLayerBuilder {
4339
expected: VecDeque<Expect>,
4440
name: String,
4541
}
4642

47-
pub struct ExpectLayer {
43+
pub struct MockLayer {
4844
expected: Arc<Mutex<VecDeque<Expect>>>,
4945
current: Mutex<Vec<Id>>,
5046
name: String,
5147
}
5248

53-
impl ExpectLayerBuilder {
54-
/// Overrides the name printed by the mock subscriber's debugging output.
55-
///
56-
/// The debugging output is displayed if the test panics, or if the test is
57-
/// run with `--nocapture`.
58-
///
59-
/// By default, the mock subscriber's name is the name of the test
60-
/// (*technically*, the name of the thread where it was created, which is
61-
/// the name of the test unless tests are run with `--test-threads=1`).
62-
/// When a test has only one mock subscriber, this is sufficient. However,
63-
/// some tests may include multiple subscribers, in order to test
64-
/// interactions between multiple subscribers. In that case, it can be
65-
/// helpful to give each subscriber a separate name to distinguish where the
66-
/// debugging output comes from.
49+
impl MockLayerBuilder {
6750
pub fn named(mut self, name: impl fmt::Display) -> Self {
6851
use std::fmt::Write;
6952
if !self.name.is_empty() {
@@ -74,63 +57,55 @@ impl ExpectLayerBuilder {
7457
self
7558
}
7659

77-
pub fn enter(mut self, span: MockSpan) -> Self {
78-
self.expected.push_back(Expect::Enter(span));
79-
self
80-
}
81-
8260
pub fn event(mut self, event: MockEvent) -> Self {
8361
self.expected.push_back(Expect::Event(event));
8462
self
8563
}
8664

87-
pub fn exit(mut self, span: MockSpan) -> Self {
88-
self.expected.push_back(Expect::Exit(span));
65+
pub fn new_span<I>(mut self, new_span: I) -> Self
66+
where
67+
I: Into<NewSpan>,
68+
{
69+
self.expected.push_back(Expect::NewSpan(new_span.into()));
8970
self
9071
}
9172

92-
pub fn done(mut self) -> Self {
93-
self.expected.push_back(Expect::Nothing);
73+
pub fn enter(mut self, span: MockSpan) -> Self {
74+
self.expected.push_back(Expect::Enter(span));
9475
self
9576
}
9677

97-
pub fn record<I>(mut self, span: MockSpan, fields: I) -> Self
98-
where
99-
I: Into<field::Expect>,
100-
{
101-
self.expected.push_back(Expect::Visit(span, fields.into()));
78+
pub fn exit(mut self, span: MockSpan) -> Self {
79+
self.expected.push_back(Expect::Exit(span));
10280
self
10381
}
10482

105-
pub fn new_span<I>(mut self, new_span: I) -> Self
106-
where
107-
I: Into<NewSpan>,
108-
{
109-
self.expected.push_back(Expect::NewSpan(new_span.into()));
83+
pub fn done(mut self) -> Self {
84+
self.expected.push_back(Expect::Nothing);
11085
self
11186
}
11287

113-
pub fn run(self) -> ExpectLayer {
114-
ExpectLayer {
88+
pub fn run(self) -> MockLayer {
89+
MockLayer {
11590
expected: Arc::new(Mutex::new(self.expected)),
11691
name: self.name,
11792
current: Mutex::new(Vec::new()),
11893
}
11994
}
12095

121-
pub fn run_with_handle(self) -> (ExpectLayer, MockHandle) {
96+
pub fn run_with_handle(self) -> (MockLayer, MockHandle) {
12297
let expected = Arc::new(Mutex::new(self.expected));
12398
let handle = MockHandle::new(expected.clone(), self.name.clone());
124-
let layer = ExpectLayer {
99+
let subscriber = MockLayer {
125100
expected,
126101
name: self.name,
127102
current: Mutex::new(Vec::new()),
128103
};
129-
(layer, handle)
104+
(subscriber, handle)
130105
}
131106
}
132107

133-
impl ExpectLayer {
108+
impl MockLayer {
134109
fn check_span_ref<'spans, S>(
135110
&self,
136111
expected: &MockSpan,
@@ -191,9 +166,9 @@ impl ExpectLayer {
191166
}
192167
}
193168

194-
impl<S> Layer<S> for ExpectLayer
169+
impl<C> Layer<C> for MockLayer
195170
where
196-
S: Subscriber + for<'a> LookupSpan<'a>,
171+
C: Subscriber + for<'a> LookupSpan<'a>,
197172
{
198173
fn register_callsite(
199174
&self,
@@ -203,15 +178,15 @@ where
203178
tracing_core::Interest::always()
204179
}
205180

206-
fn on_record(&self, _: &Id, _: &Record<'_>, _: Context<'_, S>) {
181+
fn on_record(&self, _: &Id, _: &Record<'_>, _: Context<'_, C>) {
207182
unimplemented!(
208183
"so far, we don't have any tests that need an `on_record` \
209184
implementation.\nif you just wrote one that does, feel free to \
210185
implement it!"
211186
);
212187
}
213188

214-
fn on_event(&self, event: &Event<'_>, cx: Context<'_, S>) {
189+
fn on_event(&self, event: &Event<'_>, cx: Context<'_, C>) {
215190
let name = event.metadata().name();
216191
println!(
217192
"[{}] event: {}; level: {}; target: {}",
@@ -262,11 +237,15 @@ where
262237
}
263238
}
264239

265-
fn on_follows_from(&self, _span: &Id, _follows: &Id, _: Context<'_, S>) {
266-
// TODO: it should be possible to expect spans to follow from other spans
240+
fn on_follows_from(&self, _span: &Id, _follows: &Id, _: Context<'_, C>) {
241+
unimplemented!(
242+
"so far, we don't have any tests that need an `on_follows_from` \
243+
implementation.\nif you just wrote one that does, feel free to \
244+
implement it!"
245+
);
267246
}
268247

269-
fn on_new_span(&self, span: &Attributes<'_>, id: &Id, cx: Context<'_, S>) {
248+
fn on_new_span(&self, span: &Attributes<'_>, id: &Id, cx: Context<'_, C>) {
270249
let meta = span.metadata();
271250
println!(
272251
"[{}] new_span: name={:?}; target={:?}; id={:?};",
@@ -290,7 +269,7 @@ where
290269
}
291270
}
292271

293-
fn on_enter(&self, id: &Id, cx: Context<'_, S>) {
272+
fn on_enter(&self, id: &Id, cx: Context<'_, C>) {
294273
let span = cx
295274
.span(id)
296275
.unwrap_or_else(|| panic!("[{}] no span for ID {:?}", self.name, id));
@@ -305,7 +284,7 @@ where
305284
self.current.lock().unwrap().push(id.clone());
306285
}
307286

308-
fn on_exit(&self, id: &Id, cx: Context<'_, S>) {
287+
fn on_exit(&self, id: &Id, cx: Context<'_, C>) {
309288
if std::thread::panicking() {
310289
// `exit()` can be called in `drop` impls, so we must guard against
311290
// double panics.
@@ -334,7 +313,7 @@ where
334313
};
335314
}
336315

337-
fn on_close(&self, id: Id, cx: Context<'_, S>) {
316+
fn on_close(&self, id: Id, cx: Context<'_, C>) {
338317
if std::thread::panicking() {
339318
// `try_close` can be called in `drop` impls, so we must guard against
340319
// double panics.
@@ -380,14 +359,14 @@ where
380359
}
381360
}
382361

383-
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>) {
362+
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, C>) {
384363
panic!("well-behaved subscribers should never do this to us, lol");
385364
}
386365
}
387366

388-
impl fmt::Debug for ExpectLayer {
367+
impl fmt::Debug for MockLayer {
389368
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
390-
let mut s = f.debug_struct("ExpectLayer");
369+
let mut s = f.debug_struct("MockLayer");
391370
s.field("name", &self.name);
392371

393372
if let Ok(expected) = self.expected.try_lock() {

tracing-mock/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub mod span;
99
#[cfg(feature = "tracing-subscriber")]
1010
pub mod subscriber;
1111

12+
#[cfg(feature = "tracing-subscriber")]
13+
pub mod layer;
14+
1215
#[derive(Debug, Eq, PartialEq)]
1316
pub enum Parent {
1417
ContextualRoot,

tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![cfg(feature = "registry")]
2-
mod support;
3-
use self::support::*;
42
use tracing::Level;
3+
use tracing_mock::{
4+
event,
5+
layer::{self, MockLayer},
6+
subscriber,
7+
};
58
use tracing_subscriber::{filter::LevelFilter, prelude::*};
69

710
#[test]
@@ -102,7 +105,7 @@ fn filter() -> LevelFilter {
102105
LevelFilter::INFO
103106
}
104107

105-
fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
108+
fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) {
106109
layer::named(name)
107110
.event(event::mock().at_level(Level::TRACE))
108111
.event(event::mock().at_level(Level::DEBUG))
@@ -113,7 +116,7 @@ fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
113116
.run_with_handle()
114117
}
115118

116-
fn filtered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
119+
fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) {
117120
layer::named(name)
118121
.event(event::mock().at_level(Level::INFO))
119122
.event(event::mock().at_level(Level::WARN))

tracing-subscriber/tests/env_filter/per_layer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! `Layer` filter).
33
#![cfg(feature = "registry")]
44
use super::*;
5+
use tracing_mock::{event, field, layer, span};
56

67
#[test]
78
fn level_filter_event() {

tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![cfg(feature = "registry")]
2-
mod support;
3-
use self::support::*;
42
use tracing::{Level, Metadata, Subscriber};
3+
use tracing_mock::{
4+
event,
5+
layer::{self, MockLayer},
6+
subscriber,
7+
};
58
use tracing_subscriber::{filter::DynFilterFn, layer::Context, prelude::*};
69

710
#[test]
@@ -110,7 +113,7 @@ fn filter<S>() -> DynFilterFn<S> {
110113
.with_max_level_hint(Level::INFO)
111114
}
112115

113-
fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
116+
fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) {
114117
layer::named(name)
115118
.event(event::mock().at_level(Level::TRACE))
116119
.event(event::mock().at_level(Level::DEBUG))
@@ -121,7 +124,7 @@ fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
121124
.run_with_handle()
122125
}
123126

124-
fn filtered(name: &str) -> (ExpectLayer, subscriber::MockHandle) {
127+
fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) {
125128
layer::named(name)
126129
.event(event::mock().at_level(Level::INFO))
127130
.event(event::mock().at_level(Level::WARN))

tracing-subscriber/tests/layer_filter_interests_are_cached.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#![cfg(feature = "registry")]
2-
mod support;
3-
use self::support::*;
4-
52
use std::{
63
collections::HashMap,
74
sync::{Arc, Mutex},
85
};
96
use tracing::{Level, Subscriber};
7+
use tracing_mock::{event, layer};
108
use tracing_subscriber::{filter, prelude::*};
119

1210
#[test]

tracing-subscriber/tests/layer_filters/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::*;
2+
use tracing_mock::layer::MockLayer;
23
use tracing_subscriber::{filter, prelude::*, Layer};
34

4-
fn layer() -> (ExpectLayer, subscriber::MockHandle) {
5+
fn layer() -> (MockLayer, subscriber::MockHandle) {
56
layer::mock().done().run_with_handle()
67
}
78

0 commit comments

Comments
 (0)