mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 20:31:04 +00:00
use own timeout in tests instead of smol-timeout (#1618)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user