Skip to content

Commit 23e8b73

Browse files
committed
test: add tracing-test crate for non-publishable test utils
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.
1 parent a0126b2 commit 23e8b73

File tree

14 files changed

+195
-73
lines changed

14 files changed

+195
-73
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ members = [
1414
"tracing-opentelemetry",
1515
"tracing-subscriber",
1616
"tracing-serde",
17+
"tracing-test",
1718
"tracing-appender",
1819
"tracing-journald",
1920
"examples"

tracing-attributes/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ quote = "1.0.20"
4040

4141
[dev-dependencies]
4242
tracing = { path = "../tracing", version = "0.2" }
43-
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
43+
tracing-mock = { path = "../tracing-mock" }
4444
tokio-test = "0.4.2"
4545
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = ["env-filter"] }
46+
tracing-test = { path = "../tracing-test" }
4647
async-trait = "0.1.56"
4748
trybuild = "1.0.64"
4849
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
@@ -42,7 +42,8 @@ mio = "0.6.23"
4242
[dev-dependencies]
4343
tokio-test = "0.4.2"
4444
tracing-core = { path = "../tracing-core", version = "0.2" }
45-
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
45+
tracing-mock = { path = "../tracing-mock" }
46+
tracing-test = { path = "../tracing-test" }
4647

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

tracing-futures/tests/std_future.rs

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

56
#[test]
67
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-
}

0 commit comments

Comments
 (0)