Files
tokio/tokio-trace/tests/support/event.rs
T
Eliza Weismanandcsmoe 847fb59b17 trace-core: Introduce callsite classification in metadata (#1046)
## Motivation

To ease the implementation of `Subscriber::register_callsite`, a field
should be added to `Metadata` to indicate if this callsite is an event or
a span.

## Solution

A new struct, `Kind`, is added to the `metadata` module in
`tokio-trace-core`, and a `Kind` field is added to the `Metadata`
struct. Macros which construct `metadata` now require a `Kind`.

`Kind` is represented as a struct with a private inner enum to allow new
`Kind`s to be added without breaking changes. However, the _addition_ of
the kind field _is_ a breaking change. While this could be done in a
backward-compatible way, it would permit the construction of metadata
with unknown kinds, and since the next `tokio-trace-core` release will
be a breaking change, I opted to make the breaking change instead.

New API tests for the `callsite!` and `metadata!` macros have been added
to guard against future API breakage.

Fixes: #986
Closes: #1008

Co-Authored-By: csmoe <[email protected]>
2019-04-11 11:56:42 -07:00

94 lines
2.2 KiB
Rust

#![allow(missing_docs)]
use super::{field, metadata};
use std::fmt;
/// A mock event.
///
/// This is intended for use with the mock subscriber API in the
/// `subscriber` module.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct MockEvent {
pub fields: Option<field::Expect>,
metadata: metadata::Expect,
}
pub fn mock() -> MockEvent {
MockEvent {
..Default::default()
}
}
impl MockEvent {
pub fn named<I>(self, name: I) -> Self
where
I: Into<String>,
{
Self {
metadata: metadata::Expect {
name: Some(name.into()),
..self.metadata
},
..self
}
}
pub fn with_fields<I>(self, fields: I) -> Self
where
I: Into<field::Expect>,
{
Self {
fields: Some(fields.into()),
..self
}
}
pub fn at_level(self, level: tokio_trace::Level) -> Self {
Self {
metadata: metadata::Expect {
level: Some(level),
..self.metadata
},
..self
}
}
pub fn with_target<I>(self, target: I) -> Self
where
I: Into<String>,
{
Self {
metadata: metadata::Expect {
target: Some(target.into()),
..self.metadata
},
..self
}
}
pub(in support) fn check(self, event: &tokio_trace::Event) {
let meta = event.metadata();
let name = meta.name();
self.metadata.check(meta, format_args!("event {}", name));
assert!(meta.is_event(), "expected an event but got {:?}", event);
if let Some(mut expected_fields) = self.fields {
let mut checker = expected_fields.checker(format!("{}", name));
event.record(&mut checker);
checker.finish();
}
}
}
impl fmt::Display for MockEvent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "an event")?;
if let Some(ref name) = self.metadata.name {
write!(f, " named {:?}", name)?;
}
if let Some(ref fields) = self.fields {
write!(f, " with {}", fields)?
}
Ok(())
}
}