tracing_mock/layer.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
//! An implementation of the [`Layer`] trait which validates that
//! the `tracing` data it receives matches the expected output for a test.
//!
//!
//! The [`MockLayer`] is the central component in these tools. The
//! `MockLayer` has expectations set on it which are later
//! validated as the code under test is run.
//!
//! ```
//! use tracing_mock::{expect, layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//!
//! let (layer, handle) = layer::mock()
//! // Expect a single event with a specified message
//! .event(expect::event().with_fields(expect::msg("droids")))
//! .run_with_handle();
//!
//! // Use `set_default` to apply the `MockSubscriber` until the end
//! // of the current scope (when the guard `_subscriber` is dropped).
//! let _subscriber = tracing_subscriber::registry()
//! .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
//! .set_default();
//!
//! // These *are* the droids we are looking for
//! tracing::info!("droids");
//!
//! // Use the handle to check the assertions. This line will panic if an
//! // assertion is not met.
//! handle.assert_finished();
//! ```
//!
//! A more complex example may consider multiple spans and events with
//! their respective fields:
//!
//! ```
//! use tracing_mock::{expect, layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//!
//! let span = expect::span()
//! .named("my_span");
//! let (layer, handle) = layer::mock()
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(expect::msg("say hello")))
//! // Exit a matching span
//! .exit(&span)
//! // Expect no further messages to be recorded
//! .only()
//! // Return the layer and handle
//! .run_with_handle();
//!
//! // Use `set_default` to apply the `MockLayers` until the end
//! // of the current scope (when the guard `_subscriber` is dropped).
//! let _subscriber = tracing_subscriber::registry()
//! .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
//! .set_default();
//!
//! {
//! let span = tracing::trace_span!(
//! "my_span",
//! greeting = "hello world",
//! );
//!
//! let _guard = span.enter();
//! tracing::info!("say hello");
//! }
//!
//! // Use the handle to check the assertions. This line will panic if an
//! // assertion is not met.
//! handle.assert_finished();
//! ```
//!
//! If we modify the previous example so that we **don't** enter the
//! span before recording an event, the test will fail:
//!
//! ```should_panic
//! use tracing_mock::{expect, layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//!
//! let span = expect::span()
//! .named("my_span");
//! let (layer, handle) = layer::mock()
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(expect::msg("say hello")))
//! // Exit a matching span
//! .exit(&span)
//! // Expect no further messages to be recorded
//! .only()
//! // Return the subscriber and handle
//! .run_with_handle();
//!
//! // Use `set_default` to apply the `MockSubscriber` until the end
//! // of the current scope (when the guard `_subscriber` is dropped).
//! let _subscriber = tracing_subscriber::registry()
//! .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
//! .set_default();
//!
//! {
//! let span = tracing::trace_span!(
//! "my_span",
//! greeting = "hello world",
//! );
//!
//! // Don't enter the span.
//! // let _guard = span.enter();
//! tracing::info!("say hello");
//! }
//!
//! // Use the handle to check the assertions. This line will panic if an
//! // assertion is not met.
//! handle.assert_finished();
//! ```
//!
//! [`Layer`]: trait@tracing_subscriber::layer::Layer
use std::{
collections::VecDeque,
fmt,
sync::{Arc, Mutex},
};
use tracing_core::{
span::{Attributes, Id, Record},
Event, Subscriber,
};
use tracing_subscriber::{
layer::{Context, Layer},
registry::{LookupSpan, SpanRef},
};
use crate::{
ancestry::{get_ancestry, ActualAncestry, HasAncestry},
event::ExpectedEvent,
expect::Expect,
span::{ActualSpan, ExpectedSpan, NewSpan},
subscriber::MockHandle,
};
/// Create a [`MockLayerBuilder`] used to construct a
/// [`MockLayer`].
///
/// For additional information and examples, see the [`layer`]
/// module and [`MockLayerBuilder`] documentation.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .named("my_span");
/// let (layer, handle) = layer::mock()
/// // Enter a matching span
/// .enter(&span)
/// // Record an event with message "collect parting message"
/// .event(expect::event().with_fields(expect::msg("say hello")))
/// // Exit a matching span
/// .exit(&span)
/// // Expect no further messages to be recorded
/// .only()
/// // Return the subscriber and handle
/// .run_with_handle();
///
/// // Use `set_default` to apply the `MockSubscriber` until the end
/// // of the current scope (when the guard `_subscriber` is dropped).
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// {
/// let span = tracing::trace_span!(
/// "my_span",
/// greeting = "hello world",
/// );
///
/// let _guard = span.enter();
/// tracing::info!("say hello");
/// }
///
/// // Use the handle to check the assertions. This line will panic if an
/// // assertion is not met.
/// handle.assert_finished();
/// ```
///
/// [`layer`]: mod@crate::layer
#[must_use]
pub fn mock() -> MockLayerBuilder {
MockLayerBuilder {
expected: Default::default(),
name: std::thread::current()
.name()
.map(String::from)
.unwrap_or_default(),
}
}
/// Create a [`MockLayerBuilder`] with a name already set.
///
/// This constructor is equivalent to calling
/// [`MockLayerBuilder::named`] in the following way"
/// `layer::mock().named(name)`.
///
/// For additional information and examples, see the [`layer`]
/// module and [`MockLayerBuilder`] documentation.
///
/// # Examples
///
/// The example from [`MockLayerBuilder::named`] could be rewritten as:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer_1, handle_1) = layer::named("subscriber-1")
/// .event(expect::event())
/// .run_with_handle();
///
/// let (layer_2, handle_2) = layer::named("subscriber-2")
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(
/// layer_2.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true))
/// )
/// .set_default();
/// {
/// let _subscriber = tracing_subscriber::registry()
/// .with(
/// layer_1
/// .with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true))
/// )
/// .set_default();
///
/// tracing::info!("a");
/// }
///
/// handle_1.assert_finished();
/// handle_2.assert_finished();
/// ```
///
/// [`MockLayerBuilder::named`]: fn@crate::layer::MockLayerBuilder::named
/// [`layer`]: mod@crate::layer
#[must_use]
pub fn named(name: impl std::fmt::Display) -> MockLayerBuilder {
mock().named(name)
}
/// A builder for constructing [`MockLayer`]s.
///
/// The methods on this builder set expectations which are then
/// validated by the constructed [`MockLayer`].
///
/// For a detailed description and examples see the documentation
/// for the methods and the [`layer`] module.
///
/// [`layer`]: mod@crate::layer
#[derive(Debug)]
pub struct MockLayerBuilder {
expected: VecDeque<Expect>,
name: String,
}
/// A layer which validates the traces it receives.
///
/// A `MockLayer` is constructed with a
/// [`MockLayerBuilder`]. For a detailed description and examples,
/// see the documentation for that struct and for the [`layer`]
/// module.
///
/// [`layer`]: mod@crate::layer
pub struct MockLayer {
expected: Arc<Mutex<VecDeque<Expect>>>,
current: Mutex<Vec<Id>>,
name: String,
}
impl MockLayerBuilder {
/// Overrides the name printed by the mock layer's debugging output.
///
/// The debugging output is displayed if the test panics, or if the test is
/// run with `--nocapture`.
///
/// By default, the mock layer's name is the name of the test
/// (*technically*, the name of the thread where it was created, which is
/// the name of the test unless tests are run with `--test-threads=1`).
/// When a test has only one mock layer, this is sufficient. However,
/// some tests may include multiple layers, in order to test
/// interactions between multiple layers. In that case, it can be
/// helpful to give each layers a separate name to distinguish where the
/// debugging output comes from.
///
/// # Examples
///
/// In the following example, we create two layers, both
/// expecting to receive an event. As we only record a single
/// event, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{layer, expect};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer_1, handle_1) = layer::mock()
/// .named("layer-1")
/// .event(expect::event())
/// .run_with_handle();
///
/// let (layer_2, handle_2) = layer::mock()
/// .named("layer-2")
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(
/// layer_2.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true))
/// )
/// .set_default();
/// {
/// let _subscriber = tracing_subscriber::registry()
/// .with(
/// layer_1
/// .with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true))
/// )
/// .set_default();
///
/// tracing::info!("a");
/// }
///
/// handle_1.assert_finished();
/// handle_2.assert_finished();
/// ```
///
/// In the test output, we see that the layer which didn't
/// received the event was the one named `layer-2`, which is
/// correct as the layer named `layer-1` was the default
/// when the event was recorded:
///
/// ```text
/// [main::layer-2] more notifications expected: [
/// Event(
/// MockEvent,
/// ),
/// ]', tracing-mock/src/subscriber.rs:472:13
/// ```
pub fn named(mut self, name: impl fmt::Display) -> Self {
use std::fmt::Write;
if !self.name.is_empty() {
write!(&mut self.name, "::{}", name).unwrap();
} else {
self.name = name.to_string();
}
self
}
/// Adds an expectation that an event matching the [`ExpectedEvent`]
/// will be recorded next.
///
/// The `event` can be a default mock which will match any event
/// (`expect::event()`) or can include additional expectations.
/// See the [`ExpectedEvent`] documentation for more details.
///
/// If an event is recorded that doesn't match the `ExpectedEvent`,
/// or if something else (such as entering a span) is recorded
/// first, then the expectation will fail.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer, handle) = layer::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// tracing::info!("event");
///
/// handle.assert_finished();
/// ```
///
/// A span is entered before the event, causing the test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer, handle) = layer::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// tracing::info!("event");
///
/// handle.assert_finished();
/// ```
pub fn event(mut self, event: ExpectedEvent) -> Self {
self.expected.push_back(Expect::Event(event));
self
}
/// Adds an expectation that the creation of a span will be
/// recorded next.
///
/// This function accepts `Into<NewSpan>` instead of
/// [`ExpectedSpan`] directly. [`NewSpan`] can be used to test
/// span fields and the span ancestry.
///
/// The new span doesn't need to be entered for this expectation
/// to succeed.
///
/// If a span is recorded that doesn't match the `ExpectedSpan`,
/// or if something else (such as an event) is recorded first,
/// then the expectation will fail.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing")
/// .with_fields(expect::field("testing").with_value(&"yes"));
/// let (layer, handle) = layer::mock()
/// .new_span(span)
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// _ = tracing::info_span!("the span we're testing", testing = "yes");
///
/// handle.assert_finished();
/// ```
///
/// An event is recorded before the span is created, causing the
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing")
/// .with_fields(expect::field("testing").with_value(&"yes"));
/// let (layer, handle) = layer::mock()
/// .new_span(span)
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// tracing::info!("an event");
/// _ = tracing::info_span!("the span we're testing", testing = "yes");
///
/// handle.assert_finished();
/// ```
///
/// [`ExpectedSpan`]: struct@crate::span::ExpectedSpan
/// [`NewSpan`]: struct@crate::span::NewSpan
pub fn new_span<I>(mut self, new_span: I) -> Self
where
I: Into<NewSpan>,
{
self.expected.push_back(Expect::NewSpan(new_span.into()));
self
}
/// Adds an expectation that entering a span matching the
/// [`ExpectedSpan`] will be recorded next.
///
/// This expectation is generally accompanied by a call to
/// [`exit`], since an entered span will typically be exited. If used
/// together with [`only`], this is likely necessary, because the span
/// will be dropped before the test completes (except in rare cases,
/// such as if [`std::mem::forget`] is used).
///
/// If the span that is entered doesn't match the [`ExpectedSpan`],
/// or if something else (such as an event) is recorded first,
/// then the expectation will fail.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing");
/// let (layer, handle) = layer::mock()
/// .enter(&span)
/// .exit(&span)
/// .only()
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// {
/// let span = tracing::info_span!("the span we're testing");
/// let _entered = span.enter();
/// }
///
/// handle.assert_finished();
/// ```
///
/// An event is recorded before the span is entered, causing the
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing");
/// let (layer, handle) = layer::mock()
/// .enter(&span)
/// .exit(&span)
/// .only()
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// {
/// tracing::info!("an event");
/// let span = tracing::info_span!("the span we're testing");
/// let _entered = span.enter();
/// }
///
/// handle.assert_finished();
/// ```
///
/// [`exit`]: fn@Self::exit
/// [`only`]: fn@Self::only
pub fn enter<S>(mut self, span: S) -> Self
where
S: Into<ExpectedSpan>,
{
self.expected.push_back(Expect::Enter(span.into()));
self
}
/// Adds an expectation that exiting a span matching the
/// [`ExpectedSpan`] will be recorded next.
///
/// As a span may be entered and exited multiple times,
/// this is different from the span being closed. In
/// general [`enter`] and `exit` should be paired.
///
/// If the span that is exited doesn't match the [`ExpectedSpan`],
/// or if something else (such as an event) is recorded first,
/// then the expectation will fail.
///
/// **Note**: Ensure that the guard returned by [`Span::enter`]
/// is dropped before calling [`MockHandle::assert_finished`].
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing");
/// let (layer, handle) = layer::mock()
/// .enter(&span)
/// .exit(&span)
/// .only()
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
/// {
/// let span = tracing::info_span!("the span we're testing");
/// let _entered = span.enter();
/// }
///
/// handle.assert_finished();
/// ```
///
/// An event is recorded before the span is exited, causing the
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
/// .named("the span we're testing");
/// let (layer, handle) = layer::mock()
/// .enter(&span)
/// .exit(&span)
/// .only()
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// {
/// let span = tracing::info_span!("the span we're testing");
/// let _entered = span.enter();
/// tracing::info!("an event");
/// }
///
/// handle.assert_finished();
/// ```
///
/// [`enter`]: fn@Self::enter
/// [`MockHandle::assert_finished`]: fn@crate::subscriber::MockHandle::assert_finished
/// [`Span::enter`]: fn@tracing::Span::enter
pub fn exit<S>(mut self, span: S) -> Self
where
S: Into<ExpectedSpan>,
{
self.expected.push_back(Expect::Exit(span.into()));
self
}
/// Expects that no further traces are received.
///
/// The call to `only` should appear immediately before the final
/// call to [`run`] or [`run_with_handle`], as any expectations which
/// are added after `only` will not be considered.
///
/// # Examples
///
/// Consider this simple test. It passes even though we only
/// expect a single event, but receive three:
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer, handle) = layer::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// tracing::info!("a");
/// tracing::info!("b");
/// tracing::info!("c");
///
/// handle.assert_finished();
/// ```
///
/// After including `only`, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer, handle) = layer::mock()
/// .event(expect::event())
/// .only()
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// tracing::info!("a");
/// tracing::info!("b");
/// tracing::info!("c");
///
/// handle.assert_finished();
/// ```
///
/// [`run`]: fn@Self::run
/// [`run_with_handle`]: fn@Self::run_with_handle
pub fn only(mut self) -> Self {
self.expected.push_back(Expect::Nothing);
self
}
/// Consume this builder and return a [`MockLayer`] which can
/// be set as the default subscriber.
///
/// This function is similar to [`run_with_handle`], but it doesn't
/// return a [`MockHandle`]. This is useful if the desired
/// assertions can be checked externally to the subscriber.
///
/// # Examples
///
/// The following test is used within the `tracing-subscriber`
/// codebase:
///
/// ```
/// use tracing::Subscriber;
/// use tracing_mock::layer;
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let unfiltered = layer::named("unfiltered").run().boxed();
/// let info = layer::named("info")
/// .run()
/// .with_filter(tracing_core::LevelFilter::INFO)
/// .boxed();
/// let debug = layer::named("debug")
/// .run()
/// .with_filter(tracing_core::LevelFilter::DEBUG)
/// .boxed();
///
/// let subscriber = tracing_subscriber::registry().with(vec![unfiltered, info, debug]);
///
/// assert_eq!(subscriber.max_level_hint(), None);
/// ```
///
/// [`MockHandle`]: struct@crate::subscriber::MockHandle
/// [`run_with_handle`]: fn@Self::run_with_handle
pub fn run(self) -> MockLayer {
MockLayer {
expected: Arc::new(Mutex::new(self.expected)),
name: self.name,
current: Mutex::new(Vec::new()),
}
}
/// Consume this builder and return a [`MockLayer`] which can
/// be set as the default subscriber and a [`MockHandle`] which can
/// be used to validate the provided expectations.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, layer};
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
///
/// let (layer, handle) = layer::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// let _subscriber = tracing_subscriber::registry()
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
/// .set_default();
///
/// tracing::info!("event");
///
/// handle.assert_finished();
/// ```
///
/// [`MockHandle`]: struct@crate::subscriber::MockHandle
/// [`MockLayer`]: struct@crate::layer::MockLayer
pub fn run_with_handle(self) -> (MockLayer, MockHandle) {
let expected = Arc::new(Mutex::new(self.expected));
let handle = MockHandle::new(expected.clone(), self.name.clone());
let subscriber = MockLayer {
expected,
name: self.name,
current: Mutex::new(Vec::new()),
};
(subscriber, handle)
}
}
impl<'a, S> From<&SpanRef<'a, S>> for ActualSpan
where
S: LookupSpan<'a>,
{
fn from(span_ref: &SpanRef<'a, S>) -> Self {
Self::new(span_ref.id(), Some(span_ref.metadata()))
}
}
impl MockLayer {
fn check_event_scope<C>(
&self,
current_scope: Option<tracing_subscriber::registry::Scope<'_, C>>,
expected_scope: &mut [ExpectedSpan],
) where
C: for<'lookup> tracing_subscriber::registry::LookupSpan<'lookup>,
{
let mut current_scope = current_scope.into_iter().flatten();
let mut i = 0;
for (expected, actual) in expected_scope.iter_mut().zip(&mut current_scope) {
println!(
"[{}] event_scope[{}] actual={} ({:?}); expected={}",
self.name,
i,
actual.name(),
actual.id(),
expected
);
expected.check(
&(&actual).into(),
format_args!("the {}th span in the event's scope to be", i),
&self.name,
);
i += 1;
}
let remaining_expected = &expected_scope[i..];
assert!(
remaining_expected.is_empty(),
"\n[{}] did not observe all expected spans in event scope!\n[{}] missing: {:#?}",
self.name,
self.name,
remaining_expected,
);
assert!(
current_scope.next().is_none(),
"\n[{}] did not expect all spans in the actual event scope!",
self.name,
);
}
}
impl<C> Layer<C> for MockLayer
where
C: Subscriber + for<'a> LookupSpan<'a>,
{
fn register_callsite(
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing_core::Interest {
println!("[{}] register_callsite {:#?}", self.name, metadata);
tracing_core::Interest::always()
}
fn on_record(&self, _: &Id, _: &Record<'_>, _: Context<'_, C>) {
unimplemented!(
"so far, we don't have any tests that need an `on_record` \
implementation.\nif you just wrote one that does, feel free to \
implement it!"
);
}
fn on_event(&self, event: &Event<'_>, cx: Context<'_, C>) {
let name = event.metadata().name();
println!(
"[{}] event: {}; level: {}; target: {}",
self.name,
name,
event.metadata().level(),
event.metadata().target(),
);
match self.expected.lock().unwrap().pop_front() {
None => {}
Some(Expect::Event(mut expected)) => {
expected.check(event, || context_get_ancestry(event, &cx), &self.name);
if let Some(expected_scope) = expected.scope_mut() {
self.check_event_scope(cx.event_scope(event), expected_scope);
}
}
Some(ex) => ex.bad(&self.name, format_args!("observed event {:#?}", event)),
}
}
fn on_follows_from(&self, _span: &Id, _follows: &Id, _: Context<'_, C>) {
unimplemented!(
"so far, we don't have any tests that need an `on_follows_from` \
implementation.\nif you just wrote one that does, feel free to \
implement it!"
);
}
fn on_new_span(&self, span: &Attributes<'_>, id: &Id, cx: Context<'_, C>) {
let meta = span.metadata();
println!(
"[{}] new_span: name={:?}; target={:?}; id={:?};",
self.name,
meta.name(),
meta.target(),
id
);
let mut expected = self.expected.lock().unwrap();
let was_expected = matches!(expected.front(), Some(Expect::NewSpan(_)));
if was_expected {
if let Expect::NewSpan(mut expected) = expected.pop_front().unwrap() {
expected.check(span, || context_get_ancestry(span, &cx), &self.name);
}
}
}
fn on_enter(&self, id: &Id, cx: Context<'_, C>) {
let span = cx
.span(id)
.unwrap_or_else(|| panic!("[{}] no span for ID {:?}", self.name, id));
println!("[{}] enter: {}; id={:?};", self.name, span.name(), id);
match self.expected.lock().unwrap().pop_front() {
None => {}
Some(Expect::Enter(ref expected_span)) => {
expected_span.check(&(&span).into(), "to enter", &self.name);
}
Some(ex) => ex.bad(&self.name, format_args!("entered span {:?}", span.name())),
}
self.current.lock().unwrap().push(id.clone());
}
fn on_exit(&self, id: &Id, cx: Context<'_, C>) {
if std::thread::panicking() {
// `exit()` can be called in `drop` impls, so we must guard against
// double panics.
println!("[{}] exit {:?} while panicking", self.name, id);
return;
}
let span = cx
.span(id)
.unwrap_or_else(|| panic!("[{}] no span for ID {:?}", self.name, id));
println!("[{}] exit: {}; id={:?};", self.name, span.name(), id);
match self.expected.lock().unwrap().pop_front() {
None => {}
Some(Expect::Exit(ref expected_span)) => {
expected_span.check(&(&span).into(), "to exit", &self.name);
let curr = self.current.lock().unwrap().pop();
assert_eq!(
Some(id),
curr.as_ref(),
"[{}] exited span {:?}, but the current span was {:?}",
self.name,
span.name(),
curr.as_ref().and_then(|id| cx.span(id)).map(|s| s.name())
);
}
Some(ex) => ex.bad(&self.name, format_args!("exited span {:?}", span.name())),
};
}
fn on_close(&self, id: Id, cx: Context<'_, C>) {
if std::thread::panicking() {
// `try_close` can be called in `drop` impls, so we must guard against
// double panics.
println!("[{}] close {:?} while panicking", self.name, id);
return;
}
let span = cx.span(&id);
let name = span.as_ref().map(|span| {
println!("[{}] close_span: {}; id={:?};", self.name, span.name(), id,);
span.name()
});
if name.is_none() {
println!("[{}] drop_span: id={:?}", self.name, id);
}
if let Ok(mut expected) = self.expected.try_lock() {
let was_expected = match expected.front() {
Some(Expect::DropSpan(ref expected_span)) => {
// Don't assert if this function was called while panicking,
// as failing the assertion can cause a double panic.
if !::std::thread::panicking() {
if let Some(ref span) = span {
expected_span.check(&span.into(), "to close a span", &self.name);
}
}
true
}
Some(Expect::Event(_)) => {
if !::std::thread::panicking() {
panic!(
"[{}] expected an event, but dropped span {} (id={:?}) instead",
self.name,
name.unwrap_or("<unknown name>"),
id
);
}
true
}
_ => false,
};
if was_expected {
expected.pop_front();
}
}
}
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, C>) {
panic!("well-behaved subscribers should never do this to us, lol");
}
}
fn context_get_ancestry<C>(item: impl HasAncestry, ctx: &Context<'_, C>) -> ActualAncestry
where
C: Subscriber + for<'a> LookupSpan<'a>,
{
get_ancestry(
item,
|| ctx.lookup_current().map(|s| s.id()),
|span_id| ctx.span(span_id).map(|span| (&span).into()),
)
}
impl fmt::Debug for MockLayer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct("ExpectSubscriber");
s.field("name", &self.name);
if let Ok(expected) = self.expected.try_lock() {
s.field("expected", &expected);
} else {
s.field("expected", &format_args!("<locked>"));
}
if let Ok(current) = self.current.try_lock() {
s.field("current", &format_args!("{:?}", ¤t));
} else {
s.field("current", &format_args!("<locked>"));
}
s.finish()
}
}