Skip to content

Commit c82a1d1

Browse files
committed
futures: separate std-future tests into submodule that is only run on the nightly toolchain
1 parent 8097d08 commit c82a1d1

File tree

5 files changed

+131
-65
lines changed

5 files changed

+131
-65
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ jobs:
2828
- script: cargo test --features=doctest-readme --all
2929
name: "doctest readme"
3030
rust: nightly
31-
- script: (cd tracing-futures && cargo test --all-features)
32-
name: "futures all features"
31+
- script: (cd tracing-futures/test_std_futures && cargo test)
32+
name: "futures nightly"
3333
rust: nightly

tracing-futures/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ tokio-executor = { version = "0.1", optional = true }
1818

1919
[dev-dependencies]
2020
tokio = "0.1.22"
21-
tokio-test = { git = "https://github.com/tokio-rs/tokio.git" }
2221
tracing-fmt = { path = "../tracing-fmt" }
2322
tracing-core = "0.1.2"

tracing-futures/src/lib.rs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ mod tests {
194194

195195
#[cfg(feature = "futures-01")]
196196
use futures::{future, stream, task, Async, Future};
197-
#[cfg(feature = "std-future")]
198-
use tokio_test::task::MockTask;
199197
use tracing::{subscriber::with_default, Level};
200198

201199
struct PollN<T, E> {
@@ -204,28 +202,6 @@ mod tests {
204202
polls: usize,
205203
}
206204

207-
#[cfg(feature = "std-future")]
208-
impl<T, E> std::future::Future for PollN<T, E>
209-
where
210-
T: Unpin,
211-
E: Unpin,
212-
{
213-
type Output = Result<T, E>;
214-
fn poll(self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
215-
let this = self.get_mut();
216-
217-
this.polls += 1;
218-
if this.polls == this.finish_at {
219-
let value = this.and_return.take().expect("polled after ready");
220-
221-
std::task::Poll::Ready(value)
222-
} else {
223-
lw.waker().wake_by_ref();
224-
std::task::Poll::Pending
225-
}
226-
}
227-
}
228-
229205
#[cfg(feature = "futures-01")]
230206
impl<T, E> futures::Future for PollN<T, E> {
231207
type Item = T;
@@ -372,42 +348,4 @@ mod tests {
372348
});
373349
handle.assert_finished();
374350
}
375-
376-
#[cfg(feature = "std-future")]
377-
#[test]
378-
fn std_future_enter_exit_is_reasonable() {
379-
let (subscriber, handle) = subscriber::mock()
380-
.enter(span::mock().named("foo"))
381-
.exit(span::mock().named("foo"))
382-
.enter(span::mock().named("foo"))
383-
.exit(span::mock().named("foo"))
384-
.drop_span(span::mock().named("foo"))
385-
.done()
386-
.run_with_handle();
387-
let mut task = MockTask::new();
388-
with_default(subscriber, || {
389-
let future = PollN::new_ok(2).instrument(span!(Level::TRACE, "foo"));
390-
block_on_future(&mut task, future).unwrap();
391-
});
392-
handle.assert_finished();
393-
}
394-
395-
#[cfg(feature = "std-future")]
396-
#[test]
397-
fn std_future_error_ends_span() {
398-
let (subscriber, handle) = subscriber::mock()
399-
.enter(span::mock().named("foo"))
400-
.exit(span::mock().named("foo"))
401-
.enter(span::mock().named("foo"))
402-
.exit(span::mock().named("foo"))
403-
.drop_span(span::mock().named("foo"))
404-
.done()
405-
.run_with_handle();
406-
let mut task = MockTask::new();
407-
with_default(subscriber, || {
408-
let future = PollN::new_err(2).instrument(span!(Level::TRACE, "foo"));
409-
block_on_future(&mut task, future).unwrap_err();
410-
});
411-
handle.assert_finished();
412-
}
413351
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Note: these tests depend on crates that use nightly features which do not
2+
# run under stable and beta toolchains.
3+
#
4+
# Do not add these tests to the other tracing-futures tests unless the
5+
# minimum Rust version is at least 1.36.
6+
[workspace]
7+
8+
[package]
9+
name = "test_std_future"
10+
version = "0.1.0"
11+
publish = false
12+
edition = "2018"
13+
14+
[dependencies]
15+
tokio-test = { git = "https://github.com/tokio-rs/tokio.git" }
16+
tracing = "0.1"
17+
tracing-core = "0.1.2"
18+
tracing-futures = { path = "..", features = ["std-future"] }
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#[macro_use]
2+
extern crate tracing;
3+
extern crate tracing_core;
4+
5+
pub use self::support as test_support;
6+
// This has to have the same name as the module in `tracing`.
7+
#[path = "../../../tracing/tests/support/mod.rs"]
8+
pub mod support;
9+
10+
use std::pin::Pin;
11+
use std::task::Context;
12+
13+
use support::*;
14+
use tokio_test::task::MockTask;
15+
use tracing::{subscriber::with_default, Level};
16+
use tracing_futures::Instrument;
17+
18+
struct PollN<T, E> {
19+
and_return: Option<Result<T, E>>,
20+
finish_at: usize,
21+
polls: usize,
22+
}
23+
24+
impl<T, E> std::future::Future for PollN<T, E>
25+
where
26+
T: Unpin,
27+
E: Unpin,
28+
{
29+
type Output = Result<T, E>;
30+
fn poll(self: Pin<&mut Self>, lw: &mut Context) -> std::task::Poll<Self::Output> {
31+
let this = self.get_mut();
32+
33+
this.polls += 1;
34+
if this.polls == this.finish_at {
35+
let value = this.and_return.take().expect("polled after ready");
36+
37+
std::task::Poll::Ready(value)
38+
} else {
39+
lw.waker().wake_by_ref();
40+
std::task::Poll::Pending
41+
}
42+
}
43+
}
44+
45+
impl PollN<(), ()> {
46+
fn new_ok(finish_at: usize) -> Self {
47+
Self {
48+
and_return: Some(Ok(())),
49+
finish_at,
50+
polls: 0,
51+
}
52+
}
53+
54+
fn new_err(finish_at: usize) -> Self {
55+
Self {
56+
and_return: Some(Err(())),
57+
finish_at,
58+
polls: 0,
59+
}
60+
}
61+
}
62+
63+
fn block_on_future<F>(task: &mut MockTask, future: F) -> F::Output
64+
where
65+
F: std::future::Future,
66+
{
67+
let mut future = Box::pin(future);
68+
69+
loop {
70+
match task.poll(&mut future) {
71+
std::task::Poll::Ready(v) => break v,
72+
_ => {}
73+
}
74+
}
75+
}
76+
77+
#[test]
78+
fn std_future_enter_exit_is_reasonable() {
79+
let (subscriber, handle) = subscriber::mock()
80+
.enter(span::mock().named("foo"))
81+
.exit(span::mock().named("foo"))
82+
.enter(span::mock().named("foo"))
83+
.exit(span::mock().named("foo"))
84+
.drop_span(span::mock().named("foo"))
85+
.done()
86+
.run_with_handle();
87+
let mut task = MockTask::new();
88+
with_default(subscriber, || {
89+
let future = PollN::new_ok(2).instrument(span!(Level::TRACE, "foo"));
90+
block_on_future(&mut task, future).unwrap();
91+
});
92+
handle.assert_finished();
93+
}
94+
95+
#[test]
96+
fn std_future_error_ends_span() {
97+
let (subscriber, handle) = subscriber::mock()
98+
.enter(span::mock().named("foo"))
99+
.exit(span::mock().named("foo"))
100+
.enter(span::mock().named("foo"))
101+
.exit(span::mock().named("foo"))
102+
.drop_span(span::mock().named("foo"))
103+
.done()
104+
.run_with_handle();
105+
let mut task = MockTask::new();
106+
with_default(subscriber, || {
107+
let future = PollN::new_err(2).instrument(span!(Level::TRACE, "foo"));
108+
block_on_future(&mut task, future).unwrap_err();
109+
});
110+
handle.assert_finished();
111+
}

0 commit comments

Comments
 (0)