| 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 |
// |
| 5 |
// The alternative would be for each of these tests to be defined in a separate |
| 6 |
// file, which is :( |
| 7 |
#![cfg(feature = "std")] |
| 8 |
use tracing::{ |
| 9 |
field::display, |
| 10 |
span::{Attributes, Id, Record}, |
| 11 |
subscriber::{with_default, Interest, Subscriber}, |
| 12 |
Event, Level, Metadata, |
| 13 |
}; |
| 14 |
|
| 15 |
use tracing_mock::*; |
| 16 |
|
| 17 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 18 |
#[test] |
| 19 |
fn event_macros_dont_infinite_loop() { |
| 20 |
// This test ensures that an event macro within a subscriber |
| 21 |
// won't cause an infinite loop of events. |
| 22 |
struct TestSubscriber; |
| 23 |
impl Subscriber for TestSubscriber { |
| 24 |
fn register_callsite(&self, _: &Metadata<'_>) -> Interest { |
| 25 |
// Always return sometimes so that `enabled` will be called |
| 26 |
// (which can loop). |
| 27 |
Interest::sometimes() |
| 28 |
} |
| 29 |
|
| 30 |
fn enabled(&self, meta: &Metadata<'_>) -> bool { |
| 31 |
assert!(meta.fields().iter().any(|f| f.name() == "foo")); |
| 32 |
tracing::event!(Level::TRACE, bar = false); |
| 33 |
true |
| 34 |
} |
| 35 |
|
| 36 |
fn new_span(&self, _: &Attributes<'_>) -> Id { |
| 37 |
Id::from_u64(0xAAAA) |
| 38 |
} |
| 39 |
|
| 40 |
fn record(&self, _: &Id, _: &Record<'_>) {} |
| 41 |
|
| 42 |
fn record_follows_from(&self, _: &Id, _: &Id) {} |
| 43 |
|
| 44 |
fn event(&self, event: &Event<'_>) { |
| 45 |
assert!(event.metadata().fields().iter().any(|f| f.name() == "foo")); |
| 46 |
tracing::event!(Level::TRACE, baz = false); |
| 47 |
} |
| 48 |
|
| 49 |
fn enter(&self, _: &Id) {} |
| 50 |
|
| 51 |
fn exit(&self, _: &Id) {} |
| 52 |
} |
| 53 |
|
| 54 |
with_default(TestSubscriber, || { |
| 55 |
tracing::event!(Level::TRACE, foo = false); |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 60 |
#[test] |
| 61 |
fn boxed_subscriber() { |
| 62 |
let (subscriber, handle) = subscriber::mock() |
| 63 |
.new_span( |
| 64 |
span::mock().named("foo").with_field( |
| 65 |
field::mock("bar") |
| 66 |
.with_value(&display("hello from my span")) |
| 67 |
.only(), |
| 68 |
), |
| 69 |
) |
| 70 |
.enter(span::mock().named("foo")) |
| 71 |
.exit(span::mock().named("foo")) |
| 72 |
.drop_span(span::mock().named("foo")) |
| 73 |
.done() |
| 74 |
.run_with_handle(); |
| 75 |
let subscriber: Box<dyn Subscriber + Send + Sync + 'static> = Box::new(subscriber); |
| 76 |
|
| 77 |
with_default(subscriber, || { |
| 78 |
let from = "my span"; |
| 79 |
let span = tracing::span!( |
| 80 |
Level::TRACE, |
| 81 |
"foo", |
| 82 |
bar = format_args!("hello from {}", from) |
| 83 |
); |
| 84 |
span.in_scope(|| {}); |
| 85 |
}); |
| 86 |
|
| 87 |
handle.assert_finished(); |
| 88 |
} |
| 89 |
|
| 90 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 91 |
#[test] |
| 92 |
fn arced_subscriber() { |
| 93 |
use std::sync::Arc; |
| 94 |
|
| 95 |
let (subscriber, handle) = subscriber::mock() |
| 96 |
.new_span( |
| 97 |
span::mock().named("foo").with_field( |
| 98 |
field::mock("bar") |
| 99 |
.with_value(&display("hello from my span")) |
| 100 |
.only(), |
| 101 |
), |
| 102 |
) |
| 103 |
.enter(span::mock().named("foo")) |
| 104 |
.exit(span::mock().named("foo")) |
| 105 |
.drop_span(span::mock().named("foo")) |
| 106 |
.event( |
| 107 |
event::mock() |
| 108 |
.with_fields(field::mock("message").with_value(&display("hello from my event"))), |
| 109 |
) |
| 110 |
.done() |
| 111 |
.run_with_handle(); |
| 112 |
let subscriber: Arc<dyn Subscriber + Send + Sync + 'static> = Arc::new(subscriber); |
| 113 |
|
| 114 |
// Test using a clone of the `Arc`ed subscriber |
| 115 |
with_default(subscriber.clone(), || { |
| 116 |
let from = "my span"; |
| 117 |
let span = tracing::span!( |
| 118 |
Level::TRACE, |
| 119 |
"foo", |
| 120 |
bar = format_args!("hello from {}", from) |
| 121 |
); |
| 122 |
span.in_scope(|| {}); |
| 123 |
}); |
| 124 |
|
| 125 |
with_default(subscriber, || { |
| 126 |
tracing::info!("hello from my event"); |
| 127 |
}); |
| 128 |
|
| 129 |
handle.assert_finished(); |
| 130 |
} |