|
91 | 91 | L: Layer<S>, |
92 | 92 | S: Subscriber, |
93 | 93 | { |
| 94 | + fn on_register_dispatch(&self, subscriber: &Dispatch) { |
| 95 | + self.layer.on_register_dispatch(subscriber); |
| 96 | + self.inner.on_register_dispatch(subscriber); |
| 97 | + } |
| 98 | + |
94 | 99 | fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { |
95 | 100 | self.pick_interest(self.layer.register_callsite(metadata), || { |
96 | 101 | self.inner.register_callsite(metadata) |
@@ -552,3 +557,76 @@ where |
552 | 557 | .finish() |
553 | 558 | } |
554 | 559 | } |
| 560 | + |
| 561 | +#[cfg(all(test, feature = "std"))] |
| 562 | +mod tests { |
| 563 | + use super::Layered; |
| 564 | + use crate::Layer; |
| 565 | + use std::sync::{Arc, Mutex}; |
| 566 | + use tracing::{Dispatch, Metadata, Subscriber}; |
| 567 | + use tracing_core::span::{Attributes, Id, Record}; |
| 568 | + |
| 569 | + #[test] |
| 570 | + fn on_register_dispatch_is_called() { |
| 571 | + // Test that the on_register_dispatch method is called when a Layered |
| 572 | + // becomes a Dispatch. |
| 573 | + let dispatch_called_1 = Arc::new(Mutex::new(false)); |
| 574 | + let dispatch_called_2 = Arc::new(Mutex::new(false)); |
| 575 | + |
| 576 | + struct TestLayer { |
| 577 | + dispatch_called: Arc<Mutex<bool>>, |
| 578 | + } |
| 579 | + |
| 580 | + impl<S: Subscriber> Layer<S> for TestLayer { |
| 581 | + fn on_register_dispatch(&self, _dispatch: &Dispatch) { |
| 582 | + *self.dispatch_called.lock().unwrap() = true; |
| 583 | + } |
| 584 | + } |
| 585 | + |
| 586 | + impl Subscriber for TestLayer { |
| 587 | + fn on_register_dispatch(&self, _subscriber: &Dispatch) { |
| 588 | + *self.dispatch_called.lock().unwrap() = true; |
| 589 | + } |
| 590 | + |
| 591 | + fn enabled(&self, _metadata: &Metadata<'_>) -> bool { |
| 592 | + true |
| 593 | + } |
| 594 | + |
| 595 | + fn new_span(&self, _span: &Attributes<'_>) -> Id { |
| 596 | + Id::from_u64(1) |
| 597 | + } |
| 598 | + |
| 599 | + fn record(&self, _span: &Id, _values: &Record<'_>) {} |
| 600 | + |
| 601 | + fn record_follows_from(&self, _span: &Id, _follows: &Id) {} |
| 602 | + |
| 603 | + fn event(&self, _event: &tracing::Event<'_>) {} |
| 604 | + |
| 605 | + fn enter(&self, _span: &Id) {} |
| 606 | + |
| 607 | + fn exit(&self, _span: &Id) {} |
| 608 | + } |
| 609 | + |
| 610 | + let subscriber = Layered::new( |
| 611 | + TestLayer { |
| 612 | + dispatch_called: dispatch_called_1.clone(), |
| 613 | + }, |
| 614 | + TestLayer { |
| 615 | + dispatch_called: dispatch_called_2.clone(), |
| 616 | + }, |
| 617 | + false, |
| 618 | + ); |
| 619 | + |
| 620 | + tracing::subscriber::with_default(subscriber, || {}); |
| 621 | + |
| 622 | + // Verify that on_register_dispatch was called |
| 623 | + assert!( |
| 624 | + *dispatch_called_1.lock().unwrap(), |
| 625 | + "on_register_dispatch should have been called for Layered.layer" |
| 626 | + ); |
| 627 | + assert!( |
| 628 | + *dispatch_called_2.lock().unwrap(), |
| 629 | + "on_register_dispatch should have been called for Layered.inner" |
| 630 | + ); |
| 631 | + } |
| 632 | +} |
0 commit comments