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
+ } ;
4
7
use tracing_core:: {
5
8
span:: { Attributes , Id , Record } ,
6
9
Event , Subscriber ,
7
10
} ;
8
- use tracing_mock:: {
9
- event:: MockEvent ,
10
- span:: { MockSpan , NewSpan } ,
11
- subscriber:: { Expect , MockHandle } ,
12
- } ;
13
11
use tracing_subscriber:: {
14
12
layer:: { Context , Layer } ,
15
13
registry:: { LookupSpan , SpanRef } ,
@@ -21,49 +19,34 @@ use std::{
21
19
sync:: { Arc , Mutex } ,
22
20
} ;
23
21
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 ( ) ,
35
30
}
31
+ }
36
32
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 )
40
36
}
41
37
42
- pub struct ExpectLayerBuilder {
38
+ pub struct MockLayerBuilder {
43
39
expected : VecDeque < Expect > ,
44
40
name : String ,
45
41
}
46
42
47
- pub struct ExpectLayer {
43
+ pub struct MockLayer {
48
44
expected : Arc < Mutex < VecDeque < Expect > > > ,
49
45
current : Mutex < Vec < Id > > ,
50
46
name : String ,
51
47
}
52
48
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 {
67
50
pub fn named ( mut self , name : impl fmt:: Display ) -> Self {
68
51
use std:: fmt:: Write ;
69
52
if !self . name . is_empty ( ) {
@@ -74,63 +57,55 @@ impl ExpectLayerBuilder {
74
57
self
75
58
}
76
59
77
- pub fn enter ( mut self , span : MockSpan ) -> Self {
78
- self . expected . push_back ( Expect :: Enter ( span) ) ;
79
- self
80
- }
81
-
82
60
pub fn event ( mut self , event : MockEvent ) -> Self {
83
61
self . expected . push_back ( Expect :: Event ( event) ) ;
84
62
self
85
63
}
86
64
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 ( ) ) ) ;
89
70
self
90
71
}
91
72
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 ) ) ;
94
75
self
95
76
}
96
77
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) ) ;
102
80
self
103
81
}
104
82
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 ) ;
110
85
self
111
86
}
112
87
113
- pub fn run ( self ) -> ExpectLayer {
114
- ExpectLayer {
88
+ pub fn run ( self ) -> MockLayer {
89
+ MockLayer {
115
90
expected : Arc :: new ( Mutex :: new ( self . expected ) ) ,
116
91
name : self . name ,
117
92
current : Mutex :: new ( Vec :: new ( ) ) ,
118
93
}
119
94
}
120
95
121
- pub fn run_with_handle ( self ) -> ( ExpectLayer , MockHandle ) {
96
+ pub fn run_with_handle ( self ) -> ( MockLayer , MockHandle ) {
122
97
let expected = Arc :: new ( Mutex :: new ( self . expected ) ) ;
123
98
let handle = MockHandle :: new ( expected. clone ( ) , self . name . clone ( ) ) ;
124
- let layer = ExpectLayer {
99
+ let subscriber = MockLayer {
125
100
expected,
126
101
name : self . name ,
127
102
current : Mutex :: new ( Vec :: new ( ) ) ,
128
103
} ;
129
- ( layer , handle)
104
+ ( subscriber , handle)
130
105
}
131
106
}
132
107
133
- impl ExpectLayer {
108
+ impl MockLayer {
134
109
fn check_span_ref < ' spans , S > (
135
110
& self ,
136
111
expected : & MockSpan ,
@@ -191,9 +166,9 @@ impl ExpectLayer {
191
166
}
192
167
}
193
168
194
- impl < S > Layer < S > for ExpectLayer
169
+ impl < C > Layer < C > for MockLayer
195
170
where
196
- S : Subscriber + for < ' a > LookupSpan < ' a > ,
171
+ C : Subscriber + for < ' a > LookupSpan < ' a > ,
197
172
{
198
173
fn register_callsite (
199
174
& self ,
@@ -203,15 +178,15 @@ where
203
178
tracing_core:: Interest :: always ( )
204
179
}
205
180
206
- fn on_record ( & self , _: & Id , _: & Record < ' _ > , _: Context < ' _ , S > ) {
181
+ fn on_record ( & self , _: & Id , _: & Record < ' _ > , _: Context < ' _ , C > ) {
207
182
unimplemented ! (
208
183
"so far, we don't have any tests that need an `on_record` \
209
184
implementation.\n if you just wrote one that does, feel free to \
210
185
implement it!"
211
186
) ;
212
187
}
213
188
214
- fn on_event ( & self , event : & Event < ' _ > , cx : Context < ' _ , S > ) {
189
+ fn on_event ( & self , event : & Event < ' _ > , cx : Context < ' _ , C > ) {
215
190
let name = event. metadata ( ) . name ( ) ;
216
191
println ! (
217
192
"[{}] event: {}; level: {}; target: {}" ,
@@ -262,11 +237,15 @@ where
262
237
}
263
238
}
264
239
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.\n if you just wrote one that does, feel free to \
244
+ implement it!"
245
+ ) ;
267
246
}
268
247
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 > ) {
270
249
let meta = span. metadata ( ) ;
271
250
println ! (
272
251
"[{}] new_span: name={:?}; target={:?}; id={:?};" ,
@@ -290,7 +269,7 @@ where
290
269
}
291
270
}
292
271
293
- fn on_enter ( & self , id : & Id , cx : Context < ' _ , S > ) {
272
+ fn on_enter ( & self , id : & Id , cx : Context < ' _ , C > ) {
294
273
let span = cx
295
274
. span ( id)
296
275
. unwrap_or_else ( || panic ! ( "[{}] no span for ID {:?}" , self . name, id) ) ;
@@ -305,7 +284,7 @@ where
305
284
self . current . lock ( ) . unwrap ( ) . push ( id. clone ( ) ) ;
306
285
}
307
286
308
- fn on_exit ( & self , id : & Id , cx : Context < ' _ , S > ) {
287
+ fn on_exit ( & self , id : & Id , cx : Context < ' _ , C > ) {
309
288
if std:: thread:: panicking ( ) {
310
289
// `exit()` can be called in `drop` impls, so we must guard against
311
290
// double panics.
@@ -334,7 +313,7 @@ where
334
313
} ;
335
314
}
336
315
337
- fn on_close ( & self , id : Id , cx : Context < ' _ , S > ) {
316
+ fn on_close ( & self , id : Id , cx : Context < ' _ , C > ) {
338
317
if std:: thread:: panicking ( ) {
339
318
// `try_close` can be called in `drop` impls, so we must guard against
340
319
// double panics.
@@ -380,14 +359,14 @@ where
380
359
}
381
360
}
382
361
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 > ) {
384
363
panic ! ( "well-behaved subscribers should never do this to us, lol" ) ;
385
364
}
386
365
}
387
366
388
- impl fmt:: Debug for ExpectLayer {
367
+ impl fmt:: Debug for MockLayer {
389
368
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 " ) ;
391
370
s. field ( "name" , & self . name ) ;
392
371
393
372
if let Ok ( expected) = self . expected . try_lock ( ) {
0 commit comments