Skip to content

Commit 91ca0e0

Browse files
hdsdavidbarsky
andauthored
test: add tracing-test crate for non-publishable test utils (#2466)
## Motivation There has been interest around publishing tracing-mock to crates.io for some time. In order to make this possible, it needs to be cleaned up. ## Solution There are some test utils in the `tracing-mock` crate which wouldn't make sense to publish. They provide test futures that are needed in multiple `tracing-*` crates, but would likely not be needed outside that context. This change moves that functionality into a separate `tracing-test` crate, which should never be published to crates.io. Refs: #539 Co-authored-by: David Barsky <[email protected]>
1 parent 011ff82 commit 91ca0e0

File tree

16 files changed

+203
-77
lines changed

16 files changed

+203
-77
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members = [
1313
"tracing-mock",
1414
"tracing-subscriber",
1515
"tracing-serde",
16+
"tracing-test",
1617
"tracing-appender",
1718
"tracing-journald",
1819
"examples"

tracing-attributes/Cargo.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,25 @@ proc-macro = true
3535

3636
[dependencies]
3737
proc-macro2 = "1.0.60"
38-
syn = { version = "2.0", default-features = false, features = ["full", "parsing", "printing", "visit-mut", "clone-impls", "extra-traits", "proc-macro"] }
38+
syn = { version = "2.0", default-features = false, features = [
39+
"full",
40+
"parsing",
41+
"printing",
42+
"visit-mut",
43+
"clone-impls",
44+
"extra-traits",
45+
"proc-macro",
46+
] }
3947
quote = "1.0.20"
4048

4149
[dev-dependencies]
4250
tracing = { path = "../tracing", version = "0.2" }
43-
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
51+
tracing-mock = { path = "../tracing-mock" }
4452
tokio-test = "0.4.2"
45-
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = ["env-filter"] }
53+
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = [
54+
"env-filter",
55+
] }
56+
tracing-test = { path = "../tracing-test" }
4657
async-trait = "0.1.67"
4758
trybuild = "1.0.64"
4859
rustversion = "1.0.9"

tracing-attributes/tests/async_fn.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use tracing_mock::*;
2-
31
use std::convert::Infallible;
42
use std::{future::Future, pin::Pin, sync::Arc};
3+
54
use tracing::collect::with_default;
65
use tracing_attributes::instrument;
6+
use tracing_mock::{collector, expect};
7+
use tracing_test::{block_on_future, PollN};
78

89
#[instrument]
910
async fn test_async_fn(polls: usize) -> Result<(), ()> {

tracing-attributes/tests/err.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use tracing_attributes::instrument;
44
use tracing_mock::*;
55
use tracing_subscriber::filter::EnvFilter;
66
use tracing_subscriber::subscribe::CollectExt;
7+
use tracing_test::{block_on_future, PollN};
78

89
use std::convert::TryFrom;
910
use std::num::TryFromIntError;

tracing-attributes/tests/follows_from.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use tracing::{collect::with_default, Id, Level, Span};
22
use tracing_attributes::instrument;
3-
use tracing_mock::*;
3+
use tracing_mock::{collector, expect};
4+
use tracing_test::block_on_future;
45

56
#[instrument(follows_from = causes, skip(causes))]
67
fn with_follows_from_sync(causes: impl IntoIterator<Item = impl Into<Option<Id>>>) {}

tracing-attributes/tests/ret.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::convert::TryFrom;
22
use std::num::TryFromIntError;
3-
use tracing_mock::*;
43

54
use tracing::{collect::with_default, Level};
65
use tracing_attributes::instrument;
6+
use tracing_mock::{collector, expect};
77
use tracing_subscriber::subscribe::CollectExt;
88
use tracing_subscriber::EnvFilter;
9+
use tracing_test::block_on_future;
910

1011
#[instrument(ret)]
1112
fn ret() -> i32 {

tracing-futures/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ mio = "0.6.23"
4343
futures = "0.3.21"
4444
tokio-test = "0.4.2"
4545
tracing-core = { path = "../tracing-core", version = "0.2" }
46-
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
46+
tracing-mock = { path = "../tracing-mock" }
47+
tracing-test = { path = "../tracing-test" }
4748

4849
[badges]
4950
maintenance = { status = "actively-developed" }

tracing-futures/tests/std_future.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::{future::Future, pin::Pin, task};
33
use futures::FutureExt as _;
44
use tracing::Instrument;
55
use tracing::{collect::with_default, Level};
6-
use tracing_mock::*;
6+
use tracing_mock::{collector, expect};
7+
use tracing_test::{block_on_future, PollN};
78

89
#[test]
910
fn enter_exit_is_reasonable() {

tracing-mock/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ publish = false
2121
tracing = { path = "../tracing", version = "0.2" }
2222
tracing-core = { path = "../tracing-core", version = "0.2", default-features = false }
2323
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", default-features = false, features = ["registry"], optional = true }
24-
tokio-test = { version = "0.4.2", optional = true }
2524

2625
# Fix minimal-versions; tokio-test fails with otherwise acceptable 0.1.0
2726
tokio-stream = "0.1.9"

tracing-mock/src/lib.rs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#![doc = include_str!("../README.md")]
2-
use std::{
3-
pin::Pin,
4-
task::{Context, Poll},
5-
};
6-
72
pub mod collector;
83
pub mod event;
94
pub mod expect;
@@ -22,12 +17,6 @@ pub enum Parent {
2217
Explicit(String),
2318
}
2419

25-
pub struct PollN<T, E> {
26-
and_return: Option<Result<T, E>>,
27-
finish_at: usize,
28-
polls: usize,
29-
}
30-
3120
impl Parent {
3221
pub fn check_parent_name(
3322
&self,
@@ -104,57 +93,3 @@ impl Parent {
10493
}
10594
}
10695
}
107-
108-
impl<T, E> std::future::Future for PollN<T, E>
109-
where
110-
T: Unpin,
111-
E: Unpin,
112-
{
113-
type Output = Result<T, E>;
114-
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
115-
let this = self.get_mut();
116-
117-
this.polls += 1;
118-
if this.polls == this.finish_at {
119-
let value = this.and_return.take().expect("polled after ready");
120-
121-
Poll::Ready(value)
122-
} else {
123-
cx.waker().wake_by_ref();
124-
Poll::Pending
125-
}
126-
}
127-
}
128-
129-
impl PollN<(), ()> {
130-
pub fn new_ok(finish_at: usize) -> Self {
131-
Self {
132-
and_return: Some(Ok(())),
133-
finish_at,
134-
polls: 0,
135-
}
136-
}
137-
138-
pub fn new_err(finish_at: usize) -> Self {
139-
Self {
140-
and_return: Some(Err(())),
141-
finish_at,
142-
polls: 0,
143-
}
144-
}
145-
}
146-
147-
#[cfg(feature = "tokio-test")]
148-
pub fn block_on_future<F>(future: F) -> F::Output
149-
where
150-
F: std::future::Future,
151-
{
152-
use tokio_test::task;
153-
154-
let mut task = task::spawn(future);
155-
loop {
156-
if let Poll::Ready(v) = task.poll() {
157-
break v;
158-
}
159-
}
160-
}

tracing-test/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## BIG SCARY NOTE
2+
# This crate is internal and to be used for testing only. It should not
3+
# be published to crates.io ever. If the functionality is needed outside
4+
# the tracing project, it should be moved back to tracing-mock.
5+
6+
[package]
7+
name = "tracing-test"
8+
version = "0.1.0"
9+
authors = [
10+
"Eliza Weisman <[email protected]>",
11+
"Tokio Contributors <[email protected]>",
12+
]
13+
license = "MIT"
14+
readme = "README.md"
15+
repository = "https://github.com/tokio-rs/tracing"
16+
homepage = "https://tokio.rs"
17+
edition = "2018"
18+
rust-version = "1.49.0"
19+
publish = false
20+
21+
[dependencies]
22+
tokio-test = "0.4.2"
23+
24+
[package.metadata.docs.rs]
25+
all-features = true
26+
rustdoc-args = ["--cfg", "docsrs"]

tracing-test/LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2019 Tokio Contributors
2+
3+
Permission is hereby granted, free of charge, to any
4+
person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the
6+
Software without restriction, including without
7+
limitation the rights to use, copy, modify, merge,
8+
publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software
10+
is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice
14+
shall be included in all copies or substantial portions
15+
of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
DEALINGS IN THE SOFTWARE.

tracing-test/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
![Tracing — Structured, application-level diagnostics][splash]
2+
3+
[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg
4+
5+
# tracing-test
6+
7+
Utilities for testing [`tracing`][tracing] and crates that uses it.
8+
9+
[![Documentation (master)][docs-master-badge]][docs-master-url]
10+
[![MIT licensed][mit-badge]][mit-url]
11+
[![Build Status][actions-badge]][actions-url]
12+
[![Discord chat][discord-badge]][discord-url]
13+
14+
[Documentation][docs-master-url] | [Chat][discord-url]
15+
16+
[docs-master-badge]: https://img.shields.io/badge/docs-master-blue
17+
[docs-master-url]: https://tracing-rs.netlify.com/tracing_mock
18+
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
19+
[mit-url]: https://github.com/tokio-rs/tracing/blob/master/tracing-test/LICENSE
20+
[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg
21+
[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI
22+
[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white
23+
[discord-url]: https://discord.gg/EeF3cQw
24+
25+
## Overview
26+
27+
[`tracing`] is a framework for instrumenting Rust programs to collect
28+
structured, event-based diagnostic information. `tracing-test` provides
29+
some reusable tools to aid in testing, but that are only intended for
30+
internal use. For mocks and expectations, see [`tracing-mock`].
31+
32+
*Compiler support: [requires `rustc` 1.56+][msrv]*
33+
34+
[msrv]: #supported-rust-versions
35+
36+
## Supported Rust Versions
37+
38+
Tracing is built against the latest stable release. The minimum supported
39+
version is 1.56. The current Tracing version is not guaranteed to build on Rust
40+
versions earlier than the minimum supported version.
41+
42+
Tracing follows the same compiler support policies as the rest of the Tokio
43+
project. The current stable Rust compiler and the three most recent minor
44+
versions before it will always be supported. For example, if the current stable
45+
compiler version is 1.45, the minimum supported version will not be increased
46+
past 1.42, three minor versions prior. Increasing the minimum supported compiler
47+
version is not considered a semver breaking change as long as doing so complies
48+
with this policy.
49+
50+
## License
51+
52+
This project is licensed under the [MIT license][mit-url].
53+
54+
### Contribution
55+
56+
Unless you explicitly state otherwise, any contribution intentionally submitted
57+
for inclusion in Tracing by you, shall be licensed as MIT, without any additional
58+
terms or conditions.

tracing-test/src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::{
2+
pin::Pin,
3+
task::{Context, Poll},
4+
};
5+
6+
#[allow(missing_docs)]
7+
8+
pub struct PollN<T, E> {
9+
and_return: Option<Result<T, E>>,
10+
finish_at: usize,
11+
polls: usize,
12+
}
13+
14+
impl<T, E> std::future::Future for PollN<T, E>
15+
where
16+
T: Unpin,
17+
E: Unpin,
18+
{
19+
type Output = Result<T, E>;
20+
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
21+
let this = self.get_mut();
22+
23+
this.polls += 1;
24+
if this.polls == this.finish_at {
25+
let value = this.and_return.take().expect("polled after ready");
26+
27+
Poll::Ready(value)
28+
} else {
29+
cx.waker().wake_by_ref();
30+
Poll::Pending
31+
}
32+
}
33+
}
34+
35+
impl PollN<(), ()> {
36+
pub fn new_ok(finish_at: usize) -> Self {
37+
Self {
38+
and_return: Some(Ok(())),
39+
finish_at,
40+
polls: 0,
41+
}
42+
}
43+
44+
pub fn new_err(finish_at: usize) -> Self {
45+
Self {
46+
and_return: Some(Err(())),
47+
finish_at,
48+
polls: 0,
49+
}
50+
}
51+
}
52+
53+
pub fn block_on_future<F>(future: F) -> F::Output
54+
where
55+
F: std::future::Future,
56+
{
57+
use tokio_test::task;
58+
59+
let mut task = task::spawn(future);
60+
loop {
61+
if let Poll::Ready(v) = task.poll() {
62+
break v;
63+
}
64+
}
65+
}

tracing/test_static_max_level_features/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ features = ["max_level_debug", "release_max_level_info"]
1919

2020
[dev-dependencies]
2121
tokio-test = "0.2.0"
22-
tracing-mock = { path = "../../tracing-mock", features = ["tokio-test"] }
22+
tracing-test = { path = "../../tracing-test" }

tracing/test_static_max_level_features/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tracing::{
66
debug, error, info, instrument, span, trace, warn, Collect, Event, Id, Level, Metadata,
77
};
88
use tracing_core::span::Current;
9-
use tracing_mock::*;
9+
use tracing_test::block_on_future;
1010

1111
struct State {
1212
last_level: Mutex<Option<Level>>,

0 commit comments

Comments
 (0)