1 // These tests require the thread-local scoped dispatcher, which only works when
2 // we have a standard library. The behaviour being tested should be the same
3 // with the standard lib disabled.
4 #![cfg(feature = "std")]
5
6 use std::thread;
7
8 use tracing::{
9 field::{debug, display},
10 subscriber::with_default,
11 Level, Span,
12 };
13 use tracing_mock::*;
14
15 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
16 #[test]
handles_to_the_same_span_are_equal()17 fn handles_to_the_same_span_are_equal() {
18 // Create a mock subscriber that will return `true` on calls to
19 // `Subscriber::enabled`, so that the spans will be constructed. We
20 // won't enter any spans in this test, so the subscriber won't actually
21 // expect to see any spans.
22 with_default(subscriber::mock().run(), || {
23 let foo1 = tracing::span!(Level::TRACE, "foo");
24 let foo2 = foo1.clone();
25 // Two handles that point to the same span are equal.
26 assert_eq!(foo1, foo2);
27 });
28 }
29
30 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
31 #[test]
handles_to_different_spans_are_not_equal()32 fn handles_to_different_spans_are_not_equal() {
33 with_default(subscriber::mock().run(), || {
34 // Even though these spans have the same name and fields, they will have
35 // differing metadata, since they were created on different lines.
36 let foo1 = tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false);
37 let foo2 = tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false);
38
39 assert_ne!(foo1, foo2);
40 });
41 }
42
43 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
44 #[test]
handles_to_different_spans_with_the_same_metadata_are_not_equal()45 fn handles_to_different_spans_with_the_same_metadata_are_not_equal() {
46 // Every time time this function is called, it will return a _new
47 // instance_ of a span with the same metadata, name, and fields.
48 fn make_span() -> Span {
49 tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false)
50 }
51
52 with_default(subscriber::mock().run(), || {
53 let foo1 = make_span();
54 let foo2 = make_span();
55
56 assert_ne!(foo1, foo2);
57 // assert_ne!(foo1.data(), foo2.data());
58 });
59 }
60
61 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
62 #[test]
spans_always_go_to_the_subscriber_that_tagged_them()63 fn spans_always_go_to_the_subscriber_that_tagged_them() {
64 let subscriber1 = subscriber::mock()
65 .enter(span::mock().named("foo"))
66 .exit(span::mock().named("foo"))
67 .enter(span::mock().named("foo"))
68 .exit(span::mock().named("foo"))
69 .drop_span(span::mock().named("foo"))
70 .done()
71 .run();
72 let subscriber2 = subscriber::mock().run();
73
74 let foo = with_default(subscriber1, || {
75 let foo = tracing::span!(Level::TRACE, "foo");
76 foo.in_scope(|| {});
77 foo
78 });
79 // Even though we enter subscriber 2's context, the subscriber that
80 // tagged the span should see the enter/exit.
81 with_default(subscriber2, move || foo.in_scope(|| {}));
82 }
83
84 // This gets exempt from testing in wasm because of: `thread::spawn` which is
85 // not yet possible to do in WASM. There is work going on see:
86 // <https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html>
87 //
88 // But for now since it's not possible we don't need to test for it :)
89 #[test]
spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads()90 fn spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads() {
91 let subscriber1 = subscriber::mock()
92 .enter(span::mock().named("foo"))
93 .exit(span::mock().named("foo"))
94 .enter(span::mock().named("foo"))
95 .exit(span::mock().named("foo"))
96 .drop_span(span::mock().named("foo"))
97 .done()
98 .run();
99 let foo = with_default(subscriber1, || {
100 let foo = tracing::span!(Level::TRACE, "foo");
101 foo.in_scope(|| {});
102 foo
103 });
104
105 // Even though we enter subscriber 2's context, the subscriber that
106 // tagged the span should see the enter/exit.
107 thread::spawn(move || {
108 with_default(subscriber::mock().run(), || {
109 foo.in_scope(|| {});
110 })
111 })
112 .join()
113 .unwrap();
114 }
115
116 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
117 #[test]
dropping_a_span_calls_drop_span()118 fn dropping_a_span_calls_drop_span() {
119 let (subscriber, handle) = subscriber::mock()
120 .enter(span::mock().named("foo"))
121 .exit(span::mock().named("foo"))
122 .drop_span(span::mock().named("foo"))
123 .done()
124 .run_with_handle();
125 with_default(subscriber, || {
126 let span = tracing::span!(Level::TRACE, "foo");
127 span.in_scope(|| {});
128 drop(span);
129 });
130
131 handle.assert_finished();
132 }
133
134 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
135 #[test]
span_closes_after_event()136 fn span_closes_after_event() {
137 let (subscriber, handle) = subscriber::mock()
138 .enter(span::mock().named("foo"))
139 .event(event::mock())
140 .exit(span::mock().named("foo"))
141 .drop_span(span::mock().named("foo"))
142 .done()
143 .run_with_handle();
144 with_default(subscriber, || {
145 tracing::span!(Level::TRACE, "foo").in_scope(|| {
146 tracing::event!(Level::DEBUG, {}, "my tracing::event!");
147 });
148 });
149
150 handle.assert_finished();
151 }
152
153 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
154 #[test]
new_span_after_event()155 fn new_span_after_event() {
156 let (subscriber, handle) = subscriber::mock()
157 .enter(span::mock().named("foo"))
158 .event(event::mock())
159 .exit(span::mock().named("foo"))
160 .drop_span(span::mock().named("foo"))
161 .enter(span::mock().named("bar"))
162 .exit(span::mock().named("bar"))
163 .drop_span(span::mock().named("bar"))
164 .done()
165 .run_with_handle();
166 with_default(subscriber, || {
167 tracing::span!(Level::TRACE, "foo").in_scope(|| {
168 tracing::event!(Level::DEBUG, {}, "my tracing::event!");
169 });
170 tracing::span!(Level::TRACE, "bar").in_scope(|| {});
171 });
172
173 handle.assert_finished();
174 }
175
176 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
177 #[test]
event_outside_of_span()178 fn event_outside_of_span() {
179 let (subscriber, handle) = subscriber::mock()
180 .event(event::mock())
181 .enter(span::mock().named("foo"))
182 .exit(span::mock().named("foo"))
183 .drop_span(span::mock().named("foo"))
184 .done()
185 .run_with_handle();
186 with_default(subscriber, || {
187 tracing::debug!("my tracing::event!");
188 tracing::span!(Level::TRACE, "foo").in_scope(|| {});
189 });
190
191 handle.assert_finished();
192 }
193
194 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
195 #[test]
cloning_a_span_calls_clone_span()196 fn cloning_a_span_calls_clone_span() {
197 let (subscriber, handle) = subscriber::mock()
198 .clone_span(span::mock().named("foo"))
199 .run_with_handle();
200 with_default(subscriber, || {
201 let span = tracing::span!(Level::TRACE, "foo");
202 // Allow the "redundant" `.clone` since it is used to call into the `.clone_span` hook.
203 #[allow(clippy::redundant_clone)]
204 let _span2 = span.clone();
205 });
206
207 handle.assert_finished();
208 }
209
210 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
211 #[test]
drop_span_when_exiting_dispatchers_context()212 fn drop_span_when_exiting_dispatchers_context() {
213 let (subscriber, handle) = subscriber::mock()
214 .clone_span(span::mock().named("foo"))
215 .drop_span(span::mock().named("foo"))
216 .drop_span(span::mock().named("foo"))
217 .run_with_handle();
218 with_default(subscriber, || {
219 let span = tracing::span!(Level::TRACE, "foo");
220 let _span2 = span.clone();
221 drop(span);
222 });
223
224 handle.assert_finished();
225 }
226
227 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
228 #[test]
clone_and_drop_span_always_go_to_the_subscriber_that_tagged_the_span()229 fn clone_and_drop_span_always_go_to_the_subscriber_that_tagged_the_span() {
230 let (subscriber1, handle1) = subscriber::mock()
231 .enter(span::mock().named("foo"))
232 .exit(span::mock().named("foo"))
233 .clone_span(span::mock().named("foo"))
234 .enter(span::mock().named("foo"))
235 .exit(span::mock().named("foo"))
236 .drop_span(span::mock().named("foo"))
237 .drop_span(span::mock().named("foo"))
238 .run_with_handle();
239 let subscriber2 = subscriber::mock().done().run();
240
241 let foo = with_default(subscriber1, || {
242 let foo = tracing::span!(Level::TRACE, "foo");
243 foo.in_scope(|| {});
244 foo
245 });
246 // Even though we enter subscriber 2's context, the subscriber that
247 // tagged the span should see the enter/exit.
248 with_default(subscriber2, move || {
249 let foo2 = foo.clone();
250 foo.in_scope(|| {});
251 drop(foo);
252 drop(foo2);
253 });
254
255 handle1.assert_finished();
256 }
257
258 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
259 #[test]
span_closes_when_exited()260 fn span_closes_when_exited() {
261 let (subscriber, handle) = subscriber::mock()
262 .enter(span::mock().named("foo"))
263 .exit(span::mock().named("foo"))
264 .drop_span(span::mock().named("foo"))
265 .done()
266 .run_with_handle();
267 with_default(subscriber, || {
268 let foo = tracing::span!(Level::TRACE, "foo");
269
270 foo.in_scope(|| {});
271
272 drop(foo);
273 });
274
275 handle.assert_finished();
276 }
277
278 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
279 #[test]
enter()280 fn enter() {
281 let (subscriber, handle) = subscriber::mock()
282 .enter(span::mock().named("foo"))
283 .event(event::mock())
284 .exit(span::mock().named("foo"))
285 .drop_span(span::mock().named("foo"))
286 .done()
287 .run_with_handle();
288 with_default(subscriber, || {
289 let foo = tracing::span!(Level::TRACE, "foo");
290 let _enter = foo.enter();
291 tracing::debug!("dropping guard...");
292 });
293
294 handle.assert_finished();
295 }
296
297 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
298 #[test]
entered()299 fn entered() {
300 let (subscriber, handle) = subscriber::mock()
301 .enter(span::mock().named("foo"))
302 .event(event::mock())
303 .exit(span::mock().named("foo"))
304 .drop_span(span::mock().named("foo"))
305 .done()
306 .run_with_handle();
307 with_default(subscriber, || {
308 let _span = tracing::span!(Level::TRACE, "foo").entered();
309 tracing::debug!("dropping guard...");
310 });
311
312 handle.assert_finished();
313 }
314
315 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
316 #[test]
entered_api()317 fn entered_api() {
318 let (subscriber, handle) = subscriber::mock()
319 .enter(span::mock().named("foo"))
320 .event(event::mock())
321 .exit(span::mock().named("foo"))
322 .drop_span(span::mock().named("foo"))
323 .done()
324 .run_with_handle();
325 with_default(subscriber, || {
326 let span = tracing::span!(Level::TRACE, "foo").entered();
327 let _derefs_to_span = span.id();
328 tracing::debug!("exiting span...");
329 let _: Span = span.exit();
330 });
331
332 handle.assert_finished();
333 }
334
335 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
336 #[test]
moved_field()337 fn moved_field() {
338 let (subscriber, handle) = subscriber::mock()
339 .new_span(
340 span::mock().named("foo").with_field(
341 field::mock("bar")
342 .with_value(&display("hello from my span"))
343 .only(),
344 ),
345 )
346 .enter(span::mock().named("foo"))
347 .exit(span::mock().named("foo"))
348 .drop_span(span::mock().named("foo"))
349 .done()
350 .run_with_handle();
351 with_default(subscriber, || {
352 let from = "my span";
353 let span = tracing::span!(
354 Level::TRACE,
355 "foo",
356 bar = display(format!("hello from {}", from))
357 );
358 span.in_scope(|| {});
359 });
360
361 handle.assert_finished();
362 }
363
364 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
365 #[test]
dotted_field_name()366 fn dotted_field_name() {
367 let (subscriber, handle) = subscriber::mock()
368 .new_span(
369 span::mock()
370 .named("foo")
371 .with_field(field::mock("fields.bar").with_value(&true).only()),
372 )
373 .done()
374 .run_with_handle();
375 with_default(subscriber, || {
376 tracing::span!(Level::TRACE, "foo", fields.bar = true);
377 });
378
379 handle.assert_finished();
380 }
381
382 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
383 #[test]
borrowed_field()384 fn borrowed_field() {
385 let (subscriber, handle) = subscriber::mock()
386 .new_span(
387 span::mock().named("foo").with_field(
388 field::mock("bar")
389 .with_value(&display("hello from my span"))
390 .only(),
391 ),
392 )
393 .enter(span::mock().named("foo"))
394 .exit(span::mock().named("foo"))
395 .drop_span(span::mock().named("foo"))
396 .done()
397 .run_with_handle();
398
399 with_default(subscriber, || {
400 let from = "my span";
401 let mut message = format!("hello from {}", from);
402 let span = tracing::span!(Level::TRACE, "foo", bar = display(&message));
403 span.in_scope(|| {
404 message.insert_str(10, " inside");
405 });
406 });
407
408 handle.assert_finished();
409 }
410
411 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
412 #[test]
413 // If emitting log instrumentation, this gets moved anyway, breaking the test.
414 #[cfg(not(feature = "log"))]
move_field_out_of_struct()415 fn move_field_out_of_struct() {
416 use tracing::field::debug;
417
418 #[derive(Debug)]
419 struct Position {
420 x: f32,
421 y: f32,
422 }
423
424 let pos = Position {
425 x: 3.234,
426 y: -1.223,
427 };
428 let (subscriber, handle) = subscriber::mock()
429 .new_span(
430 span::mock().named("foo").with_field(
431 field::mock("x")
432 .with_value(&debug(3.234))
433 .and(field::mock("y").with_value(&debug(-1.223)))
434 .only(),
435 ),
436 )
437 .new_span(
438 span::mock()
439 .named("bar")
440 .with_field(field::mock("position").with_value(&debug(&pos)).only()),
441 )
442 .run_with_handle();
443
444 with_default(subscriber, || {
445 let pos = Position {
446 x: 3.234,
447 y: -1.223,
448 };
449 let foo = tracing::span!(Level::TRACE, "foo", x = debug(pos.x), y = debug(pos.y));
450 let bar = tracing::span!(Level::TRACE, "bar", position = debug(pos));
451 foo.in_scope(|| {});
452 bar.in_scope(|| {});
453 });
454
455 handle.assert_finished();
456 }
457
458 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
459 #[test]
float_values()460 fn float_values() {
461 let (subscriber, handle) = subscriber::mock()
462 .new_span(
463 span::mock().named("foo").with_field(
464 field::mock("x")
465 .with_value(&3.234)
466 .and(field::mock("y").with_value(&-1.223))
467 .only(),
468 ),
469 )
470 .run_with_handle();
471
472 with_default(subscriber, || {
473 let foo = tracing::span!(Level::TRACE, "foo", x = 3.234, y = -1.223);
474 foo.in_scope(|| {});
475 });
476
477 handle.assert_finished();
478 }
479
480 // TODO(#1138): determine a new syntax for uninitialized span fields, and
481 // re-enable these.
482 /*
483 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
484 #[test]
485 fn add_field_after_new_span() {
486 let (subscriber, handle) = subscriber::mock()
487 .new_span(
488 span::mock()
489 .named("foo")
490 .with_field(field::mock("bar").with_value(&5)
491 .and(field::mock("baz").with_value).only()),
492 )
493 .record(
494 span::mock().named("foo"),
495 field::mock("baz").with_value(&true).only(),
496 )
497 .enter(span::mock().named("foo"))
498 .exit(span::mock().named("foo"))
499 .drop_span(span::mock().named("foo"))
500 .done()
501 .run_with_handle();
502
503 with_default(subscriber, || {
504 let span = tracing::span!(Level::TRACE, "foo", bar = 5, baz = false);
505 span.record("baz", &true);
506 span.in_scope(|| {})
507 });
508
509 handle.assert_finished();
510 }
511
512 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
513 #[test]
514 fn add_fields_only_after_new_span() {
515 let (subscriber, handle) = subscriber::mock()
516 .new_span(span::mock().named("foo"))
517 .record(
518 span::mock().named("foo"),
519 field::mock("bar").with_value(&5).only(),
520 )
521 .record(
522 span::mock().named("foo"),
523 field::mock("baz").with_value(&true).only(),
524 )
525 .enter(span::mock().named("foo"))
526 .exit(span::mock().named("foo"))
527 .drop_span(span::mock().named("foo"))
528 .done()
529 .run_with_handle();
530
531 with_default(subscriber, || {
532 let span = tracing::span!(Level::TRACE, "foo", bar = _, baz = _);
533 span.record("bar", &5);
534 span.record("baz", &true);
535 span.in_scope(|| {})
536 });
537
538 handle.assert_finished();
539 }
540 */
541
542 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
543 #[test]
record_new_value_for_field()544 fn record_new_value_for_field() {
545 let (subscriber, handle) = subscriber::mock()
546 .new_span(
547 span::mock().named("foo").with_field(
548 field::mock("bar")
549 .with_value(&5)
550 .and(field::mock("baz").with_value(&false))
551 .only(),
552 ),
553 )
554 .record(
555 span::mock().named("foo"),
556 field::mock("baz").with_value(&true).only(),
557 )
558 .enter(span::mock().named("foo"))
559 .exit(span::mock().named("foo"))
560 .drop_span(span::mock().named("foo"))
561 .done()
562 .run_with_handle();
563
564 with_default(subscriber, || {
565 let span = tracing::span!(Level::TRACE, "foo", bar = 5, baz = false);
566 span.record("baz", &true);
567 span.in_scope(|| {})
568 });
569
570 handle.assert_finished();
571 }
572
573 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
574 #[test]
record_new_values_for_fields()575 fn record_new_values_for_fields() {
576 let (subscriber, handle) = subscriber::mock()
577 .new_span(
578 span::mock().named("foo").with_field(
579 field::mock("bar")
580 .with_value(&4)
581 .and(field::mock("baz").with_value(&false))
582 .only(),
583 ),
584 )
585 .record(
586 span::mock().named("foo"),
587 field::mock("bar").with_value(&5).only(),
588 )
589 .record(
590 span::mock().named("foo"),
591 field::mock("baz").with_value(&true).only(),
592 )
593 .enter(span::mock().named("foo"))
594 .exit(span::mock().named("foo"))
595 .drop_span(span::mock().named("foo"))
596 .done()
597 .run_with_handle();
598
599 with_default(subscriber, || {
600 let span = tracing::span!(Level::TRACE, "foo", bar = 4, baz = false);
601 span.record("bar", &5);
602 span.record("baz", &true);
603 span.in_scope(|| {})
604 });
605
606 handle.assert_finished();
607 }
608
609 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
610 #[test]
new_span_with_target_and_log_level()611 fn new_span_with_target_and_log_level() {
612 let (subscriber, handle) = subscriber::mock()
613 .new_span(
614 span::mock()
615 .named("foo")
616 .with_target("app_span")
617 .at_level(Level::DEBUG),
618 )
619 .done()
620 .run_with_handle();
621
622 with_default(subscriber, || {
623 tracing::span!(target: "app_span", Level::DEBUG, "foo");
624 });
625
626 handle.assert_finished();
627 }
628
629 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
630 #[test]
explicit_root_span_is_root()631 fn explicit_root_span_is_root() {
632 let (subscriber, handle) = subscriber::mock()
633 .new_span(span::mock().named("foo").with_explicit_parent(None))
634 .done()
635 .run_with_handle();
636
637 with_default(subscriber, || {
638 tracing::span!(parent: None, Level::TRACE, "foo");
639 });
640
641 handle.assert_finished();
642 }
643
644 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
645 #[test]
explicit_root_span_is_root_regardless_of_ctx()646 fn explicit_root_span_is_root_regardless_of_ctx() {
647 let (subscriber, handle) = subscriber::mock()
648 .new_span(span::mock().named("foo"))
649 .enter(span::mock().named("foo"))
650 .new_span(span::mock().named("bar").with_explicit_parent(None))
651 .exit(span::mock().named("foo"))
652 .done()
653 .run_with_handle();
654
655 with_default(subscriber, || {
656 tracing::span!(Level::TRACE, "foo").in_scope(|| {
657 tracing::span!(parent: None, Level::TRACE, "bar");
658 })
659 });
660
661 handle.assert_finished();
662 }
663
664 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
665 #[test]
explicit_child()666 fn explicit_child() {
667 let (subscriber, handle) = subscriber::mock()
668 .new_span(span::mock().named("foo"))
669 .new_span(span::mock().named("bar").with_explicit_parent(Some("foo")))
670 .done()
671 .run_with_handle();
672
673 with_default(subscriber, || {
674 let foo = tracing::span!(Level::TRACE, "foo");
675 tracing::span!(parent: foo.id(), Level::TRACE, "bar");
676 });
677
678 handle.assert_finished();
679 }
680
681 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
682 #[test]
explicit_child_at_levels()683 fn explicit_child_at_levels() {
684 let (subscriber, handle) = subscriber::mock()
685 .new_span(span::mock().named("foo"))
686 .new_span(span::mock().named("a").with_explicit_parent(Some("foo")))
687 .new_span(span::mock().named("b").with_explicit_parent(Some("foo")))
688 .new_span(span::mock().named("c").with_explicit_parent(Some("foo")))
689 .new_span(span::mock().named("d").with_explicit_parent(Some("foo")))
690 .new_span(span::mock().named("e").with_explicit_parent(Some("foo")))
691 .done()
692 .run_with_handle();
693
694 with_default(subscriber, || {
695 let foo = tracing::span!(Level::TRACE, "foo");
696 tracing::trace_span!(parent: foo.id(), "a");
697 tracing::debug_span!(parent: foo.id(), "b");
698 tracing::info_span!(parent: foo.id(), "c");
699 tracing::warn_span!(parent: foo.id(), "d");
700 tracing::error_span!(parent: foo.id(), "e");
701 });
702
703 handle.assert_finished();
704 }
705
706 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
707 #[test]
explicit_child_regardless_of_ctx()708 fn explicit_child_regardless_of_ctx() {
709 let (subscriber, handle) = subscriber::mock()
710 .new_span(span::mock().named("foo"))
711 .new_span(span::mock().named("bar"))
712 .enter(span::mock().named("bar"))
713 .new_span(span::mock().named("baz").with_explicit_parent(Some("foo")))
714 .exit(span::mock().named("bar"))
715 .done()
716 .run_with_handle();
717
718 with_default(subscriber, || {
719 let foo = tracing::span!(Level::TRACE, "foo");
720 tracing::span!(Level::TRACE, "bar")
721 .in_scope(|| tracing::span!(parent: foo.id(), Level::TRACE, "baz"))
722 });
723
724 handle.assert_finished();
725 }
726
727 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
728 #[test]
contextual_root()729 fn contextual_root() {
730 let (subscriber, handle) = subscriber::mock()
731 .new_span(span::mock().named("foo").with_contextual_parent(None))
732 .done()
733 .run_with_handle();
734
735 with_default(subscriber, || {
736 tracing::span!(Level::TRACE, "foo");
737 });
738
739 handle.assert_finished();
740 }
741
742 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
743 #[test]
contextual_child()744 fn contextual_child() {
745 let (subscriber, handle) = subscriber::mock()
746 .new_span(span::mock().named("foo"))
747 .enter(span::mock().named("foo"))
748 .new_span(
749 span::mock()
750 .named("bar")
751 .with_contextual_parent(Some("foo")),
752 )
753 .exit(span::mock().named("foo"))
754 .done()
755 .run_with_handle();
756
757 with_default(subscriber, || {
758 tracing::span!(Level::TRACE, "foo").in_scope(|| {
759 tracing::span!(Level::TRACE, "bar");
760 })
761 });
762
763 handle.assert_finished();
764 }
765
766 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
767 #[test]
display_shorthand()768 fn display_shorthand() {
769 let (subscriber, handle) = subscriber::mock()
770 .new_span(
771 span::mock().named("my_span").with_field(
772 field::mock("my_field")
773 .with_value(&display("hello world"))
774 .only(),
775 ),
776 )
777 .done()
778 .run_with_handle();
779 with_default(subscriber, || {
780 tracing::span!(Level::TRACE, "my_span", my_field = %"hello world");
781 });
782
783 handle.assert_finished();
784 }
785
786 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
787 #[test]
debug_shorthand()788 fn debug_shorthand() {
789 let (subscriber, handle) = subscriber::mock()
790 .new_span(
791 span::mock().named("my_span").with_field(
792 field::mock("my_field")
793 .with_value(&debug("hello world"))
794 .only(),
795 ),
796 )
797 .done()
798 .run_with_handle();
799 with_default(subscriber, || {
800 tracing::span!(Level::TRACE, "my_span", my_field = ?"hello world");
801 });
802
803 handle.assert_finished();
804 }
805
806 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
807 #[test]
both_shorthands()808 fn both_shorthands() {
809 let (subscriber, handle) = subscriber::mock()
810 .new_span(
811 span::mock().named("my_span").with_field(
812 field::mock("display_field")
813 .with_value(&display("hello world"))
814 .and(field::mock("debug_field").with_value(&debug("hello world")))
815 .only(),
816 ),
817 )
818 .done()
819 .run_with_handle();
820 with_default(subscriber, || {
821 tracing::span!(Level::TRACE, "my_span", display_field = %"hello world", debug_field = ?"hello world");
822 });
823
824 handle.assert_finished();
825 }
826