| 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 |
|
| 9 |
use tracing::{ |
| 10 |
debug, error, |
| 11 |
field::{debug, display}, |
| 12 |
info, |
| 13 |
subscriber::with_default, |
| 14 |
trace, warn, Level, |
| 15 |
}; |
| 16 |
use tracing_mock::*; |
| 17 |
|
| 18 |
macro_rules! event_without_message { |
| 19 |
($name:ident: $e:expr) => { |
| 20 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 21 |
#[test] |
| 22 |
fn $name() { |
| 23 |
let (subscriber, handle) = subscriber::mock() |
| 24 |
.event( |
| 25 |
event::mock().with_fields( |
| 26 |
field::mock("answer") |
| 27 |
.with_value(&42) |
| 28 |
.and( |
| 29 |
field::mock("to_question") |
| 30 |
.with_value(&"life, the universe, and everything"), |
| 31 |
) |
| 32 |
.only(), |
| 33 |
), |
| 34 |
) |
| 35 |
.done() |
| 36 |
.run_with_handle(); |
| 37 |
|
| 38 |
with_default(subscriber, || { |
| 39 |
info!( |
| 40 |
answer = $e, |
| 41 |
to_question = "life, the universe, and everything" |
| 42 |
); |
| 43 |
}); |
| 44 |
|
| 45 |
handle.assert_finished(); |
| 46 |
} |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
event_without_message! {event_without_message: 42} |
| 51 |
event_without_message! {wrapping_event_without_message: std::num::Wrapping(42)} |
| 52 |
event_without_message! {nonzeroi32_event_without_message: std::num::NonZeroI32::new(42).unwrap()} |
| 53 |
// needs API breakage |
| 54 |
//event_without_message!{nonzerou128_event_without_message: std::num::NonZeroU128::new(42).unwrap()} |
| 55 |
|
| 56 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 57 |
#[test] |
| 58 |
fn event_with_message() { |
| 59 |
let (subscriber, handle) = subscriber::mock() |
| 60 |
.event(event::msg(format_args!( |
| 61 |
"hello from my event! yak shaved = {:?}", |
| 62 |
true |
| 63 |
))) |
| 64 |
.done() |
| 65 |
.run_with_handle(); |
| 66 |
|
| 67 |
with_default(subscriber, || { |
| 68 |
debug!("hello from my event! yak shaved = {:?}", true); |
| 69 |
}); |
| 70 |
|
| 71 |
handle.assert_finished(); |
| 72 |
} |
| 73 |
|
| 74 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 75 |
#[test] |
| 76 |
fn message_without_delims() { |
| 77 |
let (subscriber, handle) = subscriber::mock() |
| 78 |
.event( |
| 79 |
event::mock().with_fields( |
| 80 |
field::mock("answer") |
| 81 |
.with_value(&42) |
| 82 |
.and(field::mock("question").with_value(&"life, the universe, and everything")) |
| 83 |
.and(field::msg(format_args!( |
| 84 |
"hello from my event! tricky? {:?}!", |
| 85 |
true |
| 86 |
))) |
| 87 |
.only(), |
| 88 |
), |
| 89 |
) |
| 90 |
.done() |
| 91 |
.run_with_handle(); |
| 92 |
|
| 93 |
with_default(subscriber, || { |
| 94 |
let question = "life, the universe, and everything"; |
| 95 |
debug!(answer = 42, question, "hello from {where}! tricky? {:?}!", true, where = "my event"); |
| 96 |
}); |
| 97 |
|
| 98 |
handle.assert_finished(); |
| 99 |
} |
| 100 |
|
| 101 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 102 |
#[test] |
| 103 |
fn string_message_without_delims() { |
| 104 |
let (subscriber, handle) = subscriber::mock() |
| 105 |
.event( |
| 106 |
event::mock().with_fields( |
| 107 |
field::mock("answer") |
| 108 |
.with_value(&42) |
| 109 |
.and(field::mock("question").with_value(&"life, the universe, and everything")) |
| 110 |
.and(field::msg(format_args!("hello from my event"))) |
| 111 |
.only(), |
| 112 |
), |
| 113 |
) |
| 114 |
.done() |
| 115 |
.run_with_handle(); |
| 116 |
|
| 117 |
with_default(subscriber, || { |
| 118 |
let question = "life, the universe, and everything"; |
| 119 |
debug!(answer = 42, question, "hello from my event"); |
| 120 |
}); |
| 121 |
|
| 122 |
handle.assert_finished(); |
| 123 |
} |
| 124 |
|
| 125 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 126 |
#[test] |
| 127 |
fn one_with_everything() { |
| 128 |
let (subscriber, handle) = subscriber::mock() |
| 129 |
.event( |
| 130 |
event::mock() |
| 131 |
.with_fields( |
| 132 |
field::mock("message") |
| 133 |
.with_value(&tracing::field::debug(format_args!( |
| 134 |
"{:#x} make me one with{what:.>20}", |
| 135 |
4_277_009_102u64, |
| 136 |
what = "everything" |
| 137 |
))) |
| 138 |
.and(field::mock("foo").with_value(&666)) |
| 139 |
.and(field::mock("bar").with_value(&false)) |
| 140 |
.and(field::mock("like_a_butterfly").with_value(&42.0)) |
| 141 |
.only(), |
| 142 |
) |
| 143 |
.at_level(Level::ERROR) |
| 144 |
.with_target("whatever"), |
| 145 |
) |
| 146 |
.done() |
| 147 |
.run_with_handle(); |
| 148 |
|
| 149 |
with_default(subscriber, || { |
| 150 |
tracing::event!( |
| 151 |
target: "whatever", |
| 152 |
Level::ERROR, |
| 153 |
{ foo = 666, bar = false, like_a_butterfly = 42.0 }, |
| 154 |
"{:#x} make me one with{what:.>20}", 4_277_009_102u64, what = "everything" |
| 155 |
); |
| 156 |
}); |
| 157 |
|
| 158 |
handle.assert_finished(); |
| 159 |
} |
| 160 |
|
| 161 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 162 |
#[test] |
| 163 |
fn moved_field() { |
| 164 |
let (subscriber, handle) = subscriber::mock() |
| 165 |
.event( |
| 166 |
event::mock().with_fields( |
| 167 |
field::mock("foo") |
| 168 |
.with_value(&display("hello from my event")) |
| 169 |
.only(), |
| 170 |
), |
| 171 |
) |
| 172 |
.done() |
| 173 |
.run_with_handle(); |
| 174 |
with_default(subscriber, || { |
| 175 |
let from = "my event"; |
| 176 |
tracing::event!(Level::INFO, foo = display(format!("hello from {}", from))) |
| 177 |
}); |
| 178 |
|
| 179 |
handle.assert_finished(); |
| 180 |
} |
| 181 |
|
| 182 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 183 |
#[test] |
| 184 |
fn dotted_field_name() { |
| 185 |
let (subscriber, handle) = subscriber::mock() |
| 186 |
.event( |
| 187 |
event::mock().with_fields( |
| 188 |
field::mock("foo.bar") |
| 189 |
.with_value(&true) |
| 190 |
.and(field::mock("foo.baz").with_value(&false)) |
| 191 |
.only(), |
| 192 |
), |
| 193 |
) |
| 194 |
.done() |
| 195 |
.run_with_handle(); |
| 196 |
with_default(subscriber, || { |
| 197 |
tracing::event!(Level::INFO, foo.bar = true, foo.baz = false); |
| 198 |
}); |
| 199 |
|
| 200 |
handle.assert_finished(); |
| 201 |
} |
| 202 |
|
| 203 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 204 |
#[test] |
| 205 |
fn borrowed_field() { |
| 206 |
let (subscriber, handle) = subscriber::mock() |
| 207 |
.event( |
| 208 |
event::mock().with_fields( |
| 209 |
field::mock("foo") |
| 210 |
.with_value(&display("hello from my event")) |
| 211 |
.only(), |
| 212 |
), |
| 213 |
) |
| 214 |
.done() |
| 215 |
.run_with_handle(); |
| 216 |
with_default(subscriber, || { |
| 217 |
let from = "my event"; |
| 218 |
let mut message = format!("hello from {}", from); |
| 219 |
tracing::event!(Level::INFO, foo = display(&message)); |
| 220 |
message.push_str(", which happened!"); |
| 221 |
}); |
| 222 |
|
| 223 |
handle.assert_finished(); |
| 224 |
} |
| 225 |
|
| 226 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 227 |
#[test] |
| 228 |
// If emitting log instrumentation, this gets moved anyway, breaking the test. |
| 229 |
#[cfg(not(feature = "log"))] |
| 230 |
fn move_field_out_of_struct() { |
| 231 |
use tracing::field::debug; |
| 232 |
|
| 233 |
#[derive(Debug)] |
| 234 |
struct Position { |
| 235 |
x: f32, |
| 236 |
y: f32, |
| 237 |
} |
| 238 |
|
| 239 |
let pos = Position { |
| 240 |
x: 3.234, |
| 241 |
y: -1.223, |
| 242 |
}; |
| 243 |
let (subscriber, handle) = subscriber::mock() |
| 244 |
.event( |
| 245 |
event::mock().with_fields( |
| 246 |
field::mock("x") |
| 247 |
.with_value(&debug(3.234)) |
| 248 |
.and(field::mock("y").with_value(&debug(-1.223))) |
| 249 |
.only(), |
| 250 |
), |
| 251 |
) |
| 252 |
.event(event::mock().with_fields(field::mock("position").with_value(&debug(&pos)))) |
| 253 |
.done() |
| 254 |
.run_with_handle(); |
| 255 |
|
| 256 |
with_default(subscriber, || { |
| 257 |
let pos = Position { |
| 258 |
x: 3.234, |
| 259 |
y: -1.223, |
| 260 |
}; |
| 261 |
debug!(x = debug(pos.x), y = debug(pos.y)); |
| 262 |
debug!(target: "app_events", { position = debug(pos) }, "New position"); |
| 263 |
}); |
| 264 |
handle.assert_finished(); |
| 265 |
} |
| 266 |
|
| 267 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 268 |
#[test] |
| 269 |
fn display_shorthand() { |
| 270 |
let (subscriber, handle) = subscriber::mock() |
| 271 |
.event( |
| 272 |
event::mock().with_fields( |
| 273 |
field::mock("my_field") |
| 274 |
.with_value(&display("hello world")) |
| 275 |
.only(), |
| 276 |
), |
| 277 |
) |
| 278 |
.done() |
| 279 |
.run_with_handle(); |
| 280 |
with_default(subscriber, || { |
| 281 |
tracing::event!(Level::TRACE, my_field = %"hello world"); |
| 282 |
}); |
| 283 |
|
| 284 |
handle.assert_finished(); |
| 285 |
} |
| 286 |
|
| 287 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 288 |
#[test] |
| 289 |
fn debug_shorthand() { |
| 290 |
let (subscriber, handle) = subscriber::mock() |
| 291 |
.event( |
| 292 |
event::mock().with_fields( |
| 293 |
field::mock("my_field") |
| 294 |
.with_value(&debug("hello world")) |
| 295 |
.only(), |
| 296 |
), |
| 297 |
) |
| 298 |
.done() |
| 299 |
.run_with_handle(); |
| 300 |
with_default(subscriber, || { |
| 301 |
tracing::event!(Level::TRACE, my_field = ?"hello world"); |
| 302 |
}); |
| 303 |
|
| 304 |
handle.assert_finished(); |
| 305 |
} |
| 306 |
|
| 307 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 308 |
#[test] |
| 309 |
fn both_shorthands() { |
| 310 |
let (subscriber, handle) = subscriber::mock() |
| 311 |
.event( |
| 312 |
event::mock().with_fields( |
| 313 |
field::mock("display_field") |
| 314 |
.with_value(&display("hello world")) |
| 315 |
.and(field::mock("debug_field").with_value(&debug("hello world"))) |
| 316 |
.only(), |
| 317 |
), |
| 318 |
) |
| 319 |
.done() |
| 320 |
.run_with_handle(); |
| 321 |
with_default(subscriber, || { |
| 322 |
tracing::event!(Level::TRACE, display_field = %"hello world", debug_field = ?"hello world"); |
| 323 |
}); |
| 324 |
|
| 325 |
handle.assert_finished(); |
| 326 |
} |
| 327 |
|
| 328 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 329 |
#[test] |
| 330 |
fn explicit_child() { |
| 331 |
let (subscriber, handle) = subscriber::mock() |
| 332 |
.new_span(span::mock().named("foo")) |
| 333 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 334 |
.done() |
| 335 |
.run_with_handle(); |
| 336 |
|
| 337 |
with_default(subscriber, || { |
| 338 |
let foo = tracing::span!(Level::TRACE, "foo"); |
| 339 |
tracing::event!(parent: foo.id(), Level::TRACE, "bar"); |
| 340 |
}); |
| 341 |
|
| 342 |
handle.assert_finished(); |
| 343 |
} |
| 344 |
|
| 345 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 346 |
#[test] |
| 347 |
fn explicit_child_at_levels() { |
| 348 |
let (subscriber, handle) = subscriber::mock() |
| 349 |
.new_span(span::mock().named("foo")) |
| 350 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 351 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 352 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 353 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 354 |
.event(event::mock().with_explicit_parent(Some("foo"))) |
| 355 |
.done() |
| 356 |
.run_with_handle(); |
| 357 |
|
| 358 |
with_default(subscriber, || { |
| 359 |
let foo = tracing::span!(Level::TRACE, "foo"); |
| 360 |
trace!(parent: foo.id(), "a"); |
| 361 |
debug!(parent: foo.id(), "b"); |
| 362 |
info!(parent: foo.id(), "c"); |
| 363 |
warn!(parent: foo.id(), "d"); |
| 364 |
error!(parent: foo.id(), "e"); |
| 365 |
}); |
| 366 |
|
| 367 |
handle.assert_finished(); |
| 368 |
} |
| 369 |
|
| 370 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 371 |
#[test] |
| 372 |
fn option_values() { |
| 373 |
let (subscriber, handle) = subscriber::mock() |
| 374 |
.event( |
| 375 |
event::mock().with_fields( |
| 376 |
field::mock("some_str") |
| 377 |
.with_value(&"yes") |
| 378 |
.and(field::mock("some_bool").with_value(&true)) |
| 379 |
.and(field::mock("some_u64").with_value(&42_u64)) |
| 380 |
.only(), |
| 381 |
), |
| 382 |
) |
| 383 |
.done() |
| 384 |
.run_with_handle(); |
| 385 |
|
| 386 |
with_default(subscriber, || { |
| 387 |
let some_str = Some("yes"); |
| 388 |
let none_str: Option<&'static str> = None; |
| 389 |
let some_bool = Some(true); |
| 390 |
let none_bool: Option<bool> = None; |
| 391 |
let some_u64 = Some(42_u64); |
| 392 |
let none_u64: Option<u64> = None; |
| 393 |
trace!( |
| 394 |
some_str = some_str, |
| 395 |
none_str = none_str, |
| 396 |
some_bool = some_bool, |
| 397 |
none_bool = none_bool, |
| 398 |
some_u64 = some_u64, |
| 399 |
none_u64 = none_u64 |
| 400 |
); |
| 401 |
}); |
| 402 |
|
| 403 |
handle.assert_finished(); |
| 404 |
} |
| 405 |
|
| 406 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 407 |
#[test] |
| 408 |
fn option_ref_values() { |
| 409 |
let (subscriber, handle) = subscriber::mock() |
| 410 |
.event( |
| 411 |
event::mock().with_fields( |
| 412 |
field::mock("some_str") |
| 413 |
.with_value(&"yes") |
| 414 |
.and(field::mock("some_bool").with_value(&true)) |
| 415 |
.and(field::mock("some_u64").with_value(&42_u64)) |
| 416 |
.only(), |
| 417 |
), |
| 418 |
) |
| 419 |
.done() |
| 420 |
.run_with_handle(); |
| 421 |
|
| 422 |
with_default(subscriber, || { |
| 423 |
let some_str = &Some("yes"); |
| 424 |
let none_str: &Option<&'static str> = &None; |
| 425 |
let some_bool = &Some(true); |
| 426 |
let none_bool: &Option<bool> = &None; |
| 427 |
let some_u64 = &Some(42_u64); |
| 428 |
let none_u64: &Option<u64> = &None; |
| 429 |
trace!( |
| 430 |
some_str = some_str, |
| 431 |
none_str = none_str, |
| 432 |
some_bool = some_bool, |
| 433 |
none_bool = none_bool, |
| 434 |
some_u64 = some_u64, |
| 435 |
none_u64 = none_u64 |
| 436 |
); |
| 437 |
}); |
| 438 |
|
| 439 |
handle.assert_finished(); |
| 440 |
} |
| 441 |
|
| 442 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 443 |
#[test] |
| 444 |
fn option_ref_mut_values() { |
| 445 |
let (subscriber, handle) = subscriber::mock() |
| 446 |
.event( |
| 447 |
event::mock().with_fields( |
| 448 |
field::mock("some_str") |
| 449 |
.with_value(&"yes") |
| 450 |
.and(field::mock("some_bool").with_value(&true)) |
| 451 |
.and(field::mock("some_u64").with_value(&42_u64)) |
| 452 |
.only(), |
| 453 |
), |
| 454 |
) |
| 455 |
.done() |
| 456 |
.run_with_handle(); |
| 457 |
|
| 458 |
with_default(subscriber, || { |
| 459 |
let some_str = &mut Some("yes"); |
| 460 |
let none_str: &mut Option<&'static str> = &mut None; |
| 461 |
let some_bool = &mut Some(true); |
| 462 |
let none_bool: &mut Option<bool> = &mut None; |
| 463 |
let some_u64 = &mut Some(42_u64); |
| 464 |
let none_u64: &mut Option<u64> = &mut None; |
| 465 |
trace!( |
| 466 |
some_str = some_str, |
| 467 |
none_str = none_str, |
| 468 |
some_bool = some_bool, |
| 469 |
none_bool = none_bool, |
| 470 |
some_u64 = some_u64, |
| 471 |
none_u64 = none_u64 |
| 472 |
); |
| 473 |
}); |
| 474 |
|
| 475 |
handle.assert_finished(); |
| 476 |
} |
| 477 |
|
| 478 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 479 |
#[test] |
| 480 |
fn string_field() { |
| 481 |
let (subscriber, handle) = subscriber::mock() |
| 482 |
.event(event::mock().with_fields(field::mock("my_string").with_value(&"hello").only())) |
| 483 |
.event( |
| 484 |
event::mock().with_fields(field::mock("my_string").with_value(&"hello world!").only()), |
| 485 |
) |
| 486 |
.done() |
| 487 |
.run_with_handle(); |
| 488 |
with_default(subscriber, || { |
| 489 |
let mut my_string = String::from("hello"); |
| 490 |
|
| 491 |
tracing::event!(Level::INFO, my_string); |
| 492 |
|
| 493 |
// the string is not moved by using it as a field! |
| 494 |
my_string.push_str(" world!"); |
| 495 |
|
| 496 |
tracing::event!(Level::INFO, my_string); |
| 497 |
}); |
| 498 |
|
| 499 |
handle.assert_finished(); |
| 500 |
} |