Skip to content

Commit 1ee408b

Browse files
taiki-eNemo157
authored andcommitted
Re-add task/context.rs to futures-test
1 parent 03fb434 commit 1ee408b

File tree

12 files changed

+82
-34
lines changed

12 files changed

+82
-34
lines changed

futures-channel/benches/sync_mpsc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ use {
1111
sink::Sink,
1212
task::{Context, Poll},
1313
},
14-
futures_test::task::noop_waker_ref,
14+
futures_test::task::noop_context,
1515
std::pin::Pin,
1616
};
1717

1818
/// Single producer, single consumer
1919
#[bench]
2020
fn unbounded_1_tx(b: &mut Bencher) {
21-
let mut cx = Context::from_waker(noop_waker_ref());
21+
let mut cx = noop_context();
2222
b.iter(|| {
2323
let (tx, mut rx) = mpsc::unbounded();
2424

@@ -40,7 +40,7 @@ fn unbounded_1_tx(b: &mut Bencher) {
4040
/// 100 producers, single consumer
4141
#[bench]
4242
fn unbounded_100_tx(b: &mut Bencher) {
43-
let mut cx = Context::from_waker(noop_waker_ref());
43+
let mut cx = noop_context();
4444
b.iter(|| {
4545
let (tx, mut rx) = mpsc::unbounded();
4646

@@ -61,7 +61,7 @@ fn unbounded_100_tx(b: &mut Bencher) {
6161

6262
#[bench]
6363
fn unbounded_uncontended(b: &mut Bencher) {
64-
let mut cx = Context::from_waker(noop_waker_ref());
64+
let mut cx = noop_context();
6565
b.iter(|| {
6666
let (tx, mut rx) = mpsc::unbounded();
6767

@@ -101,7 +101,7 @@ impl Stream for TestSender {
101101
/// Single producers, single consumer
102102
#[bench]
103103
fn bounded_1_tx(b: &mut Bencher) {
104-
let mut cx = Context::from_waker(noop_waker_ref());
104+
let mut cx = noop_context();
105105
b.iter(|| {
106106
let (tx, mut rx) = mpsc::channel(0);
107107

@@ -118,7 +118,7 @@ fn bounded_1_tx(b: &mut Bencher) {
118118
/// 100 producers, single consumer
119119
#[bench]
120120
fn bounded_100_tx(b: &mut Bencher) {
121-
let mut cx = Context::from_waker(noop_waker_ref());
121+
let mut cx = noop_context();
122122
b.iter(|| {
123123
// Each sender can send one item after specified capacity
124124
let (tx, mut rx) = mpsc::channel(0);

futures-channel/tests/mpsc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use futures::executor::{block_on, block_on_stream};
55
use futures::future::{join, poll_fn};
66
use futures::stream::{Stream, StreamExt};
77
use futures::sink::{Sink, SinkExt};
8-
use futures::task::{Context, Poll};
9-
use futures_test::task::noop_waker_ref;
8+
use futures::task::Poll;
9+
use futures_test::task::noop_context;
1010
use pin_utils::pin_mut;
1111
use std::sync::{Arc, Mutex};
1212
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -304,7 +304,7 @@ fn stress_receiver_multi_task_bounded_hard() {
304304
} else {
305305
// Just poll
306306
let n = n.clone();
307-
match rx.poll_next_unpin(&mut Context::from_waker(noop_waker_ref())) {
307+
match rx.poll_next_unpin(&mut noop_context()) {
308308
Poll::Ready(Some(_)) => {
309309
n.fetch_add(1, Ordering::Relaxed);
310310
}

futures-test/src/assert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! assert_stream_pending {
3030
let mut stream = &mut $stream;
3131
$crate::assert::assert_is_unpin_stream(stream);
3232
let stream = $crate::std_reexport::pin::Pin::new(stream);
33-
let mut cx = $crate::std_reexport::task::Context::from_waker($crate::task::noop_waker_ref());
33+
let mut cx = $crate::task::noop_context();
3434
let poll = $crate::futures_core_reexport::stream::Stream::poll_next(
3535
stream, &mut cx,
3636
);
@@ -67,7 +67,7 @@ macro_rules! assert_stream_next {
6767
let mut stream = &mut $stream;
6868
$crate::assert::assert_is_unpin_stream(stream);
6969
let stream = $crate::std_reexport::pin::Pin::new(stream);
70-
let mut cx = $crate::std_reexport::task::Context::from_waker($crate::task::noop_waker_ref());
70+
let mut cx = $crate::task::noop_context();
7171
match $crate::futures_core_reexport::stream::Stream::poll_next(stream, &mut cx) {
7272
$crate::futures_core_reexport::task::Poll::Ready(Some(x)) => {
7373
assert_eq!(x, $item);
@@ -110,7 +110,7 @@ macro_rules! assert_stream_done {
110110
let mut stream = &mut $stream;
111111
$crate::assert::assert_is_unpin_stream(stream);
112112
let stream = $crate::std_reexport::pin::Pin::new(stream);
113-
let mut cx = $crate::std_reexport::task::Context::from_waker($crate::task::noop_waker_ref());
113+
let mut cx = $crate::task::noop_context();
114114
match $crate::futures_core_reexport::stream::Stream::poll_next(stream, &mut cx) {
115115
$crate::futures_core_reexport::task::Poll::Ready(Some(_)) => {
116116
panic!("assertion failed: expected stream to be done but had more elements");

futures-test/src/future/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ pub trait FutureTestExt: Future {
3434
///
3535
/// ```
3636
/// #![feature(async_await, futures_api)]
37-
/// use futures::task::{Context, Poll};
37+
/// use futures::task::Poll;
3838
/// use futures::future::FutureExt;
39-
/// use futures_test::task;
39+
/// use futures_test::task::noop_context;
4040
/// use futures_test::future::FutureTestExt;
4141
/// use pin_utils::pin_mut;
4242
///
4343
/// let future = (async { 5 }).pending_once();
4444
/// pin_mut!(future);
4545
///
46-
/// let mut cx = Context::from_waker(task::noop_waker_ref());
46+
/// let mut cx = noop_context();
4747
///
4848
/// assert_eq!(future.poll_unpin(&mut cx), Poll::Pending);
4949
/// assert_eq!(future.poll_unpin(&mut cx), Poll::Ready(5));

futures-test/src/task/context.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::task::{panic_waker_ref, noop_waker_ref};
2+
use futures_core::task::Context;
3+
4+
/// Create a new [`Context`](core::task::Context) where the
5+
/// [waker](core::task::Context::waker) will panic if used.
6+
///
7+
/// # Examples
8+
///
9+
/// ```should_panic
10+
/// #![feature(futures_api)]
11+
/// use futures_test::task::panic_context;
12+
///
13+
/// let cx = panic_context();
14+
/// cx.waker().wake_by_ref(); // Will panic
15+
/// ```
16+
pub fn panic_context() -> Context<'static> {
17+
Context::from_waker(panic_waker_ref())
18+
}
19+
20+
/// Create a new [`Context`](core::task::Context) where the
21+
/// [waker](core::task::Context::waker) will ignore any uses.
22+
///
23+
/// # Examples
24+
///
25+
/// ```
26+
/// #![feature(async_await, futures_api)]
27+
/// use futures::future::Future;
28+
/// use futures::task::Poll;
29+
/// use futures_test::task::noop_context;
30+
/// use pin_utils::pin_mut;
31+
///
32+
/// let mut future = async { 5 };
33+
/// pin_mut!(future);
34+
///
35+
/// assert_eq!(future.poll(&mut noop_context()), Poll::Ready(5));
36+
/// ```
37+
pub fn noop_context() -> Context<'static> {
38+
Context::from_waker(noop_waker_ref())
39+
}

futures-test/src/task/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
//! Task related testing utilities.
22
//!
33
//! This module provides utilities for creating test
4+
//! [`Context`](futures_core::task::Context)s,
45
//! [`Waker`](futures_core::task::Waker)s and
56
//! [`Spawn`](futures_core::task::Spawn) implementations.
67
//!
8+
//! Test contexts:
9+
//! - [`noop_context`] creates a context that ignores calls to
10+
//! [`cx.waker().wake_by_ref()`](futures_core::task::Waker).
11+
//! - [`panic_context`] creates a context that panics when
12+
//! [`cx.waker().wake_by_ref()`](futures_core::task::Waker) is called.
13+
//!
714
//! Test wakers:
815
//! - [`noop_waker`] creates a waker that ignores calls to
916
//! [`wake`](futures_core::task::Waker).
@@ -23,6 +30,9 @@
2330
//! return waker/spawner references: [`noop_waker_ref`],
2431
//! [`panic_waker_ref`], [`noop_spawner_mut`] and [`panic_spawner_mut`].
2532
33+
mod context;
34+
pub use self::context::{noop_context, panic_context};
35+
2636
mod noop_spawner;
2737
pub use self::noop_spawner::{noop_spawner_mut, NoopSpawner};
2838

futures-util/src/try_stream/into_async_read.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ mod tests {
103103
use super::*;
104104
use futures::stream::{self, StreamExt, TryStreamExt};
105105
use futures_io::AsyncRead;
106-
use futures_test::task::noop_waker_ref;
106+
use futures_test::task::noop_context;
107107

108108
macro_rules! assert_read {
109109
($reader:expr, $buf:expr, $item:expr) => {
110-
let mut cx = Context::from_waker(noop_waker_ref());
110+
let mut cx = noop_context();
111111
match Pin::new(&mut $reader).poll_read(&mut cx, $buf) {
112112
Poll::Ready(Ok(x)) => {
113113
assert_eq!(x, $item);

futures-util/tests/futures_unordered.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#![feature(async_await, await_macro, futures_api)]
22

33
use futures::future;
4-
use futures::task::{Context, Poll};
4+
use futures::task::Poll;
55
use futures::stream::{FusedStream, FuturesUnordered, StreamExt};
6-
use futures_test::task::noop_waker_ref;
6+
use futures_test::task::noop_context;
77

88
#[test]
99
fn is_terminated() {
10-
let mut cx = Context::from_waker(noop_waker_ref());
10+
let mut cx = noop_context();
1111
let mut tasks = FuturesUnordered::new();
1212

1313
assert_eq!(tasks.is_terminated(), false);

futures-util/tests/mutex.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ use futures::lock::Mutex;
66
use futures::stream::StreamExt;
77
use futures::task::{Context, SpawnExt};
88
use futures_test::future::FutureTestExt;
9-
use futures_test::task::{panic_waker_ref, new_count_waker};
9+
use futures_test::task::{panic_context, new_count_waker};
1010
use std::sync::Arc;
1111

1212
#[test]
1313
fn mutex_acquire_uncontested() {
1414
let mutex = Mutex::new(());
1515
for _ in 0..10 {
16-
assert!(mutex.lock().poll_unpin(&mut Context::from_waker(panic_waker_ref())).is_ready());
16+
assert!(mutex.lock().poll_unpin(&mut panic_context()).is_ready());
1717
}
1818
}
1919

2020
#[test]
2121
fn mutex_wakes_waiters() {
2222
let mutex = Mutex::new(());
2323
let (waker, counter) = new_count_waker();
24-
let lock = mutex.lock().poll_unpin(&mut Context::from_waker(panic_waker_ref()));
24+
let lock = mutex.lock().poll_unpin(&mut panic_context());
2525
assert!(lock.is_ready());
2626

2727
let mut cx = Context::from_waker(&waker);
@@ -32,7 +32,7 @@ fn mutex_wakes_waiters() {
3232
drop(lock);
3333

3434
assert_eq!(counter, 1);
35-
assert!(waiter.poll_unpin(&mut Context::from_waker(panic_waker_ref())).is_ready());
35+
assert!(waiter.poll_unpin(&mut panic_context()).is_ready());
3636
}
3737

3838
#[test]

futures-util/tests/select_all.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
use futures::future;
44
use futures::FutureExt;
5-
use futures::task::{Context, Poll};
5+
use futures::task::Poll;
66
use futures::stream::FusedStream;
77
use futures::stream::{SelectAll, StreamExt};
8-
use futures_test::task::noop_waker_ref;
8+
use futures_test::task::noop_context;
99

1010
#[test]
1111
fn is_terminated() {
12-
let mut cx = Context::from_waker(noop_waker_ref());
12+
let mut cx = noop_context();
1313
let mut tasks = SelectAll::new();
1414

1515
assert_eq!(tasks.is_terminated(), false);

0 commit comments

Comments
 (0)