Skip to content

Commit

Permalink
use own timeout in tests instead of smol-timeout (paritytech#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki committed Aug 20, 2020
1 parent 61f306d commit bf7ccb8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
20 changes: 4 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion node/network/availability-distribution/Cargo.toml
Expand Up @@ -25,7 +25,6 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master",
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
parking_lot = "0.11.0"
futures-timer = "3.0.2"
smol-timeout = "0.1.0"
env_logger = "0.7.1"
assert_matches = "1.3.0"
smallvec = "1"
3 changes: 1 addition & 2 deletions node/network/availability-distribution/src/tests.rs
Expand Up @@ -22,13 +22,12 @@ use polkadot_primitives::v1::{
GroupRotationInfo, HeadData, PersistedValidationData, OccupiedCore,
PoV, ScheduledCore, ValidatorPair,
};
use polkadot_subsystem_testhelpers as test_helpers;
use polkadot_subsystem_testhelpers::{self as test_helpers, TimeoutExt};
use polkadot_node_network_protocol::ObservedRole;

use futures::{executor, future, Future};
use futures_timer::Delay;
use smallvec::smallvec;
use smol_timeout::TimeoutExt;
use std::time::Duration;

macro_rules! view {
Expand Down
1 change: 0 additions & 1 deletion node/network/bitfield-distribution/Cargo.toml
Expand Up @@ -24,6 +24,5 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
parking_lot = "0.11.0"
maplit = "1.0.2"
smol = "0.3.3"
smol-timeout = "0.1.0"
env_logger = "0.7.1"
assert_matches = "1.3.0"
3 changes: 1 addition & 2 deletions node/network/bitfield-distribution/src/lib.rs
Expand Up @@ -611,8 +611,7 @@ mod test {
use futures::executor;
use maplit::hashmap;
use polkadot_primitives::v1::{Signed, ValidatorPair, AvailabilityBitfield};
use polkadot_node_subsystem_test_helpers::make_subsystem_context;
use smol_timeout::TimeoutExt;
use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TimeoutExt};
use sp_core::crypto::Pair;
use std::time::Duration;
use assert_matches::assert_matches;
Expand Down
2 changes: 1 addition & 1 deletion node/subsystem-test-helpers/Cargo.toml
Expand Up @@ -13,7 +13,7 @@ futures-timer = "3.0.2"
log = "0.4.8"
parity-scale-codec = "1.3.4"
parking_lot = "0.10.0"
pin-project = "0.4.22"
pin-project = "0.4.23"
polkadot-node-primitives = { path = "../primitives" }
polkadot-node-subsystem = { path = "../subsystem" }
polkadot-primitives = { path = "../../primitives" }
Expand Down
43 changes: 43 additions & 0 deletions node/subsystem-test-helpers/src/lib.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}

0 comments on commit bf7ccb8

Please sign in to comment.