add ability to synchronize in tests
- Adds `wait_for` that takes another future to signal the mock should continue. - Adds `yield_once` to allow one chain of futures to yield to the other.
This commit is contained in:
@@ -67,6 +67,26 @@ pub trait FutureExt: Future {
|
||||
future: other,
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap this future in one that will yield NotReady once before continuing.
|
||||
///
|
||||
/// This allows the executor to poll other futures before trying this one
|
||||
/// again.
|
||||
fn yield_once(self) -> Box<Future<Item = Self::Item, Error = Self::Error>>
|
||||
where
|
||||
Self: Future + Sized + 'static,
|
||||
{
|
||||
let mut ready = false;
|
||||
Box::new(::futures::future::poll_fn(move || {
|
||||
if ready {
|
||||
Ok::<_, ()>(().into())
|
||||
} else {
|
||||
ready = true;
|
||||
::futures::task::current().notify();
|
||||
Ok(::futures::Async::NotReady)
|
||||
}
|
||||
}).then(|_| self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Future> FutureExt for T {}
|
||||
|
||||
@@ -477,6 +477,16 @@ pub trait HandleFutureExt {
|
||||
}))
|
||||
}
|
||||
|
||||
fn wait_for<F>(self, other: F) -> Box<Future<Item = Self::Item, Error = Self::Error>>
|
||||
where
|
||||
F: Future + 'static,
|
||||
Self: Future + Sized + 'static
|
||||
{
|
||||
Box::new(self.then(move |result| {
|
||||
other.then(move |_| result)
|
||||
}))
|
||||
}
|
||||
|
||||
fn close(self) -> Box<Future<Item = (), Error = ()>>
|
||||
where
|
||||
Self: Future<Error = ()> + Sized + 'static,
|
||||
|
||||
Reference in New Issue
Block a user