dispatch.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #![cfg(feature = "std")]
  2. mod common;
  3. use common::*;
  4. use tracing_core::dispatcher::*;
  5. #[test]
  6. fn set_default_dispatch() {
  7. set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed");
  8. get_default(|current| {
  9. assert!(
  10. current.is::<TestSubscriberA>(),
  11. "global dispatch get failed"
  12. )
  13. });
  14. let guard = set_default(&Dispatch::new(TestSubscriberB));
  15. get_default(|current| assert!(current.is::<TestSubscriberB>(), "set_default get failed"));
  16. // Drop the guard, setting the dispatch back to the global dispatch
  17. drop(guard);
  18. get_default(|current| {
  19. assert!(
  20. current.is::<TestSubscriberA>(),
  21. "global dispatch get failed"
  22. )
  23. });
  24. }
  25. #[test]
  26. fn nested_set_default() {
  27. let _guard = set_default(&Dispatch::new(TestSubscriberA));
  28. get_default(|current| {
  29. assert!(
  30. current.is::<TestSubscriberA>(),
  31. "set_default for outer subscriber failed"
  32. )
  33. });
  34. let inner_guard = set_default(&Dispatch::new(TestSubscriberB));
  35. get_default(|current| {
  36. assert!(
  37. current.is::<TestSubscriberB>(),
  38. "set_default inner subscriber failed"
  39. )
  40. });
  41. drop(inner_guard);
  42. get_default(|current| {
  43. assert!(
  44. current.is::<TestSubscriberA>(),
  45. "set_default outer subscriber failed"
  46. )
  47. });
  48. }