use own timeout in tests instead of smol-timeout (#1618)

This commit is contained in:
Fedor Sakharov
2020-08-20 20:27:09 +03:00
committed by GitHub
parent d2c4e0cc8e
commit cc19f13468
7 changed files with 50 additions and 23 deletions
@@ -24,6 +24,7 @@ use futures::poll;
use futures::prelude::*;
use futures_timer::Delay;
use parking_lot::Mutex;
use pin_project::pin_project;
use sp_core::{testing::TaskExecutor, traits::SpawnNamed};
use std::convert::Infallible;
@@ -286,3 +287,45 @@ pub fn subsystem_test_harness<M, OverseerFactory, Overseer, TestFactory, Test>(
}
});
}
/// A future that wraps another future with a `Delay` allowing for time-limited futures.
#[pin_project]
pub struct Timeout<F: Future> {
#[pin]
future: F,
#[pin]
delay: Delay,
}
/// Extends `Future` to allow time-limited futures.
pub trait TimeoutExt: Future {
fn timeout(self, duration: Duration) -> Timeout<Self>
where
Self: Sized,
{
Timeout {
future: self,
delay: Delay::new(duration),
}
}
}
impl<F: Future> TimeoutExt for F {}
impl<F: Future> Future for Timeout<F> {
type Output = Option<F::Output>;
fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
let this = self.project();
if this.delay.poll(ctx).is_ready() {
return Poll::Ready(None);
}
if let Poll::Ready(output) = this.future.poll(ctx) {
return Poll::Ready(Some(output));
}
Poll::Pending
}
}