summaryrefslogtreecommitdiffstats
path: root/tokio-test/src/task.rs
diff options
context:
space:
mode:
authorLucio Franco <luciofranco14@gmail.com>2019-08-07 18:02:38 -0400
committerCarl Lerche <me@carllerche.com>2019-08-07 15:02:38 -0700
commit0a05332648589e2f063e678f174fb15d22cb7c28 (patch)
tree453933bc662c2c2ec20254ae084b2179bb5a2dba /tokio-test/src/task.rs
parent7268b0bb3a3f661869a539efaacfd8d049a74e9f (diff)
Remove git dep and add macro examples (#1404)
Signed-off-by: Lucio Franco <luciofranco14@gmail.com>
Diffstat (limited to 'tokio-test/src/task.rs')
-rw-r--r--tokio-test/src/task.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/tokio-test/src/task.rs b/tokio-test/src/task.rs
index f715ef34..e7d6c811 100644
--- a/tokio-test/src/task.rs
+++ b/tokio-test/src/task.rs
@@ -5,7 +5,7 @@
//! This example will use the `MockTask` to set the current task on
//! poll.
//!
-//! ```
+//! ```ignore
//! # use tokio_test::assert_ready_eq;
//! # use tokio_test::task::MockTask;
//! # use futures::{sync::mpsc, Stream, Sink, Future, Async};
@@ -26,10 +26,34 @@ use std::pin::Pin;
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
+/// Run the provided closure in a `MockTask` context.
+///
+/// # Examples
+///
+/// ```
+/// use std::future::Future;
+/// use futures_util::{future, pin_mut};
+/// use tokio_test::task;
+///
+/// task::mock(|cx| {
+/// let fut = future::ready(());
+///
+/// pin_mut!(fut);
+/// assert!(fut.poll(cx).is_ready());
+/// })
+/// ```
+pub fn mock<F, R>(f: F) -> R
+where
+ F: Fn(&mut Context<'_>) -> R,
+{
+ let mut task = MockTask::new();
+ task.enter(|cx| f(cx))
+}
+
/// Mock task
///
/// A mock task is able to intercept and track wake notifications.
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct MockTask {
waker: Arc<ThreadWaker>,
}