Skip to content

Commit a87faa3

Browse files
hdsdavidbarsky
andcommitted
test: add tracing-test crate for non-publishable test utils (#2466)
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. 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 00809ea commit a87faa3

File tree

16 files changed

+204
-77
lines changed

16 files changed

+204
-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
@@ -40,14 +40,25 @@ async-await = []
4040

4141
[dependencies]
4242
proc-macro2 = "1.0.60"
43-
syn = { version = "2.0", default-features = false, features = ["full", "parsing", "printing", "visit-mut", "clone-impls", "extra-traits", "proc-macro"] }
43+
syn = { version = "2.0", default-features = false, features = [
44+
"full",
45+
"parsing",
46+
"printing",
47+
"visit-mut",
48+
"clone-impls",
49+
"extra-traits",
50+
"proc-macro",
51+
] }
4452
quote = "1.0.20"
4553

4654
[dev-dependencies]
4755
tracing = { path = "../tracing", version = "0.1.35" }
48-
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
49-
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3.0", features = ["env-filter"] }
56+
tracing-mock = { path = "../tracing-mock" }
5057
tokio-test = "0.4.2"
58+
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3.0", features = [
59+
"env-filter",
60+
] }
61+
tracing-test = { path = "../tracing-test" }
5162
async-trait = "0.1.67"
5263
trybuild = "1.0.64"
5364
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::subscriber::with_default;
65
use tracing_attributes::instrument;
6+
use tracing_mock::{expect, subscriber};
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::layer::SubscriberExt;
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::{subscriber::with_default, Id, Level, Span};
22
use tracing_attributes::instrument;
3-
use tracing_mock::*;
3+
use tracing_mock::{expect, subscriber};
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::{subscriber::with_default, Level};
65
use tracing_attributes::instrument;
6+
use tracing_mock::{expect, subscriber};
77
use tracing_subscriber::layer::SubscriberExt;
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 = { version = "0.6.23", optional = true }
4343
futures = "0.3.21"
4444
tokio-test = "0.4.2"
4545
tracing-core = { path = "../tracing-core", version = "0.1.28" }
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::{subscriber::with_default, Level};
6-
use tracing_mock::*;
6+
use tracing_mock::{expect, subscriber};
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.1.35", features = ["std", "attributes"], default-features = false }
2222
tracing-core = { path = "../tracing-core", version = "0.1.28", 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 = { version = "0.1.9", optional = true }

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 event;
83
pub mod expect;
94
pub mod field;
@@ -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-
}

0 commit comments

Comments
 (0)