Skip to content

Commit

Permalink
Add free function and rename in_context
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Aug 4, 2018
1 parent 3ed9ab1 commit e76d3b7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion futures-testing/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mod context;
pub mod executor;
pub mod wake;

pub use self::context::TestContext;
pub use self::context::{TestContext, with_context};
37 changes: 34 additions & 3 deletions futures-testing/src/task/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ use std::sync::Arc;
/// tx2.send(val).unwrap();
/// });
///
/// let res = context.in_context(|cx| future.as_pin_mut().poll(cx));
/// let res = context.with(|cx| future.as_pin_mut().poll(cx));
/// assert_eq!(res, Poll::Pending);
///
/// assert_eq!(context.wake().count(), 0);
/// tx1.send(5).unwrap();
/// assert_eq!(context.wake().count(), 1);
///
/// let res = context.in_context(|cx| future.as_pin_mut().poll(cx));
/// let res = context.with(|cx| future.as_pin_mut().poll(cx));
/// assert_eq!(res, Poll::Ready(()));
///
/// assert_eq!(rx2.try_recv(), Ok(Some(5)));
Expand Down Expand Up @@ -73,7 +73,7 @@ where

/// Call a method with a [`Context`] referring to this [`TestContext`]'s
/// [`Wake`] and [`Executor`] implementations.
pub fn in_context<R, F>(&mut self, f: F) -> R
pub fn with<R, F>(&mut self, f: F) -> R
where
F: FnOnce(&mut Context) -> R,
{
Expand All @@ -92,3 +92,34 @@ where
Self::new(W::default(), E::default())
}
}

/// Call a method with a [`Context`][futures_core::task::Context] using the
/// specified [`Wake`][futures_core::task::Wake] and
/// [`Executor`][futures_core::task::Executor] implementations.
///
/// # Examples
///
/// ```
/// #![feature(async_await, await_macro, futures_api, pin)]
///
/// use futures::{future::Future, task::Poll};
/// use futures_testing::task::{self, wake, executor};
/// use std::boxed::PinBox;
///
/// let mut future = PinBox::new(async {
/// 5
/// });
///
/// let res = task::with_context::<wake::Panic, executor::Panic, _, _>(|cx| {
/// future.as_pin_mut().poll(cx)
/// });
/// assert_eq!(res, Poll::Ready(5));
/// ```
pub fn with_context<W, E, R, F>(f: F) -> R
where
W: Wake + Default + 'static,
E: Executor + Default + 'static,
F: FnOnce(&mut Context) -> R,
{
TestContext::<W, E>::default().with(f)
}
2 changes: 1 addition & 1 deletion futures-testing/src/task/executor/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use futures_core::{
///
/// let mut context = TestContext::<wake::Panic, executor::Panic>::default();
///
/// context.in_context(|cx| {
/// context.with(|cx| {
/// cx.executor().spawn(async { });
/// });
/// ```
Expand Down
2 changes: 1 addition & 1 deletion futures-testing/src/task/executor/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use futures_core::{
///
/// let mut context = TestContext::<wake::Panic, executor::Record>::default();
///
/// context.in_context(|cx| {
/// context.with(|cx| {
/// cx.executor().spawn(async { });
/// });
///
Expand Down
2 changes: 1 addition & 1 deletion futures-testing/src/task/wake/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::{
/// assert_eq!(context.wake().count(), 0);
///
/// let mut waker = None;
/// context.in_context(|cx| {
/// context.with(|cx| {
/// cx.waker().wake();
/// waker = Some(cx.waker().clone());
/// });
Expand Down
2 changes: 1 addition & 1 deletion futures-testing/src/task/wake/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;
///
/// let mut context = TestContext::<wake::Noop, executor::Panic>::default();
///
/// context.in_context(|cx| {
/// context.with(|cx| {
/// cx.waker().wake();
/// });
/// ```
Expand Down
2 changes: 1 addition & 1 deletion futures-testing/src/task/wake/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::Arc;
///
/// let mut context = TestContext::<wake::Panic, executor::Panic>::default();
///
/// context.in_context(|cx| {
/// context.with(|cx| {
/// cx.waker().wake();
/// });
/// ```
Expand Down

0 comments on commit e76d3b7

Please sign in to comment.