Skip to content

Commit

Permalink
mock: document public APIs in event module (tokio-rs#2426)
Browse files Browse the repository at this point in the history
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 `event` module needs documentation and examples.

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

The following pattern was applied to the description of most methods:
- Short description of expectation
- Additional clarification (where needed)
- Description of cases that cause the expectation to fail
- Examples
  - Successful validation
  - Unsuccesful validation

Two changes were also made in the text provided to the user when an
assertion fails for `with_explicit_parent` or `with_contextual_parent`.

One small API changes is also included:

The method `in_scope` has been placed behind the `tracing-subscriber`
feature flag as it currently only works with the `MockSubscriber`, not
with the `MockCollector`. If the feature flag is active and it is used
to set a non-empty scope, the `MockCollector` will panic with
`unimplemented` during validation.

Refs: tokio-rs#539
  • Loading branch information
hds authored and kaffarell committed May 22, 2024
1 parent c6546d3 commit ae937bd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions tracing-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let (collector, handle) = collector::mock()
.only()
.run_with_handle();

with_default(collector, || {
with_default(subscriber, || {
yak_shaving();
});

Expand Down Expand Up @@ -118,7 +118,7 @@ fn yak_shaving(number_of_yaks: u32) {
let yak_count: u32 = 3;
let span = expect::span().named("yak_shaving");

let (collector, handle) = collector::mock()
let (subscriber, handle) = subscriber::mock()
.new_span(
span.clone()
.with_fields(expect::field("number_of_yaks").with_value(&yak_count).only()),
Expand All @@ -144,7 +144,7 @@ let (collector, handle) = collector::mock()
.only()
.run_with_handle();

with_default(collector, || {
with_default(subscriber, || {
yak_shaving(yak_count);
});

Expand Down
10 changes: 5 additions & 5 deletions tracing-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ impl Parent {
parent_name: Option<&str>,
provided_parent: Option<tracing_core::span::Id>,
ctx: impl std::fmt::Display,
collector_name: &str,
subscriber_name: &str,
) {
match self {
Parent::ExplicitRoot => {
assert!(
provided_parent.is_none(),
"[{}] expected {} to be an explicit root, but its parent was actually {:?} (name: {:?})",
collector_name,
subscriber_name,
ctx,
provided_parent,
parent_name,
Expand All @@ -51,7 +51,7 @@ impl Parent {
Some(expected_parent.as_ref()),
parent_name,
"[{}] expected {} to have explicit parent {}, but its parent was actually {:?} (name: {:?})",
collector_name,
subscriber_name,
ctx,
expected_parent,
provided_parent,
Expand All @@ -70,7 +70,7 @@ impl Parent {
assert!(
parent_name.is_none(),
"[{}] expected {} to be contextual a root, but we were inside span {:?}",
collector_name,
subscriber_name,
ctx,
parent_name,
);
Expand All @@ -87,7 +87,7 @@ impl Parent {
Some(expected_parent.as_ref()),
parent_name,
"[{}] expected {} to have contextual parent {:?}, but got {:?}",
collector_name,
subscriber_name,
ctx,
expected_parent,
parent_name,
Expand Down

0 comments on commit ae937bd

Please sign in to comment.