scoped_clobbers_default.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #![cfg(feature = "std")]
  2. use tracing_mock::*;
  3. #[test]
  4. fn scoped_clobbers_global() {
  5. // Reproduces https://github.com/tokio-rs/tracing/issues/2050
  6. let (scoped, scoped_handle) = subscriber::mock()
  7. .event(event::msg("before global"))
  8. .event(event::msg("before drop"))
  9. .done()
  10. .run_with_handle();
  11. let (global, global_handle) = subscriber::mock()
  12. .event(event::msg("after drop"))
  13. .done()
  14. .run_with_handle();
  15. // Set a scoped default subscriber, returning a guard.
  16. let guard = tracing::subscriber::set_default(scoped);
  17. tracing::info!("before global");
  18. // Now, set the global default.
  19. tracing::subscriber::set_global_default(global)
  20. .expect("global default should not already be set");
  21. // This event should still be collected by the scoped default.
  22. tracing::info!("before drop");
  23. // Drop the guard. Now, the global default subscriber should be used.
  24. drop(guard);
  25. tracing::info!("after drop");
  26. scoped_handle.assert_finished();
  27. global_handle.assert_finished();
  28. }