tracing/tracing/tests/scoped_clobbers_default.rs
Hayden Stainsby 96c0e297f1 mock: complete API documentation including expect module (#2494)
There has been interest around publishing tracing-mock to crates.io
for some time. In order to make this possible, documentation and some
code clean up is needed.

The `expect` module, which contains constructor functions for many of
the other `tracing-mock` modules needs documentation and examples.

This change adds documentation to the `expect` module and all the public
APIs within it. This includes doctests on all the methods which serve as
examples.

The lint for `missing_docs` has been enabled for the entire
`tracing-mock` crate! This has been done together with all the
other lints that are enabled on the other crates in this project.

The `event::msg("message")` constructor was removed, in favor of
requiring an explicit construction via
`expect::event().with_fields(expect::msg("message"))`. This is
appropriate to reduce the API surface that would need to be supported in
the future and also because the `event::msg` constructor could be
overridden by a subsequent usage of `with_fields`. The shorthand
`expect::message()` was renamed to `expect::msg` to make this
change less burdensome.

The `span::named("name")` constructor was removed, in favor of requiring
an explicit construction via `expect::span.with_name("name")`. The
latter isn't much longer and since #3097, a string with the name can
be passed directly everywhere that an `ExpectedSpan` is required.

This change also sets the `missing_docs` lint to warn for the entire
`tracing-mock` crate, making it ready to publish (once backported).

Refs: #539
2024-11-20 15:57:49 +01:00

36 lines
1.2 KiB
Rust

#![cfg(feature = "std")]
use tracing_mock::{expect, subscriber};
#[test]
fn scoped_clobbers_global() {
// Reproduces https://github.com/tokio-rs/tracing/issues/2050
let (scoped, scoped_handle) = subscriber::mock()
.event(expect::event().with_fields(expect::msg("before global")))
.event(expect::event().with_fields(expect::msg("before drop")))
.only()
.run_with_handle();
let (global, global_handle) = subscriber::mock()
.event(expect::event().with_fields(expect::msg("after drop")))
.only()
.run_with_handle();
// Set a scoped default subscriber, returning a guard.
let guard = tracing::subscriber::set_default(scoped);
tracing::info!("before global");
// Now, set the global default.
tracing::subscriber::set_global_default(global)
.expect("global default should not already be set");
// This event should still be collected by the scoped default.
tracing::info!("before drop");
// Drop the guard. Now, the global default subscriber should be used.
drop(guard);
tracing::info!("after drop");
scoped_handle.assert_finished();
global_handle.assert_finished();
}