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:
Sean McArthur
2018-03-29 13:29:41 -07:00
parent 1c5d4ded50
commit 65f69a3062
3 changed files with 52 additions and 14 deletions

View File

@@ -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 {}

View File

@@ -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,