mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
Clean up tests from runtime.block_on() and moving around Runtime and Handle (#12941)
This commit is contained in:
@@ -708,16 +708,8 @@ pub struct FullPeerConfig {
|
||||
pub storage_chain: bool,
|
||||
}
|
||||
|
||||
/// Trait for text fixtures with tokio runtime.
|
||||
pub trait WithRuntime {
|
||||
/// Construct with runtime handle.
|
||||
fn with_runtime(rt_handle: tokio::runtime::Handle) -> Self;
|
||||
/// Get runtime handle.
|
||||
fn rt_handle(&self) -> &tokio::runtime::Handle;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait TestNetFactory: WithRuntime + Sized
|
||||
pub trait TestNetFactory: Default + Sized
|
||||
where
|
||||
<Self::BlockImport as BlockImport<Block>>::Transaction: Send,
|
||||
{
|
||||
@@ -747,9 +739,9 @@ where
|
||||
);
|
||||
|
||||
/// Create new test network with this many peers.
|
||||
fn new(rt_handle: tokio::runtime::Handle, n: usize) -> Self {
|
||||
fn new(n: usize) -> Self {
|
||||
trace!(target: "test_network", "Creating test network");
|
||||
let mut net = Self::with_runtime(rt_handle);
|
||||
let mut net = Self::default();
|
||||
|
||||
for i in 0..n {
|
||||
trace!(target: "test_network", "Adding peer {}", i);
|
||||
@@ -905,14 +897,11 @@ where
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let handle = self.rt_handle().clone();
|
||||
let executor = move |f| {
|
||||
handle.spawn(f);
|
||||
};
|
||||
|
||||
let network = NetworkWorker::new(sc_network::config::Params {
|
||||
role: if config.is_authority { Role::Authority } else { Role::Full },
|
||||
executor: Box::new(executor),
|
||||
executor: Box::new(|f| {
|
||||
tokio::spawn(f);
|
||||
}),
|
||||
network_config,
|
||||
chain: client.clone(),
|
||||
protocol_id,
|
||||
@@ -934,10 +923,10 @@ where
|
||||
trace!(target: "test_network", "Peer identifier: {}", network.service().local_peer_id());
|
||||
|
||||
let service = network.service().clone();
|
||||
self.rt_handle().spawn(async move {
|
||||
tokio::spawn(async move {
|
||||
chain_sync_network_provider.run(service).await;
|
||||
});
|
||||
self.rt_handle().spawn(async move {
|
||||
tokio::spawn(async move {
|
||||
import_queue.run(Box::new(chain_sync_service)).await;
|
||||
});
|
||||
|
||||
@@ -968,7 +957,7 @@ where
|
||||
|
||||
/// Used to spawn background tasks, e.g. the block request protocol handler.
|
||||
fn spawn_task(&self, f: BoxFuture<'static, ()>) {
|
||||
self.rt_handle().spawn(f);
|
||||
tokio::spawn(f);
|
||||
}
|
||||
|
||||
/// Polls the testnet until all nodes are in sync.
|
||||
@@ -1027,11 +1016,11 @@ where
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
/// Wait until we are sync'ed.
|
||||
/// Run the network until we are sync'ed.
|
||||
///
|
||||
/// Calls `poll_until_sync` repeatedly.
|
||||
/// (If we've not synced within 10 mins then panic rather than hang.)
|
||||
async fn wait_until_sync(&mut self) {
|
||||
async fn run_until_sync(&mut self) {
|
||||
timeout(
|
||||
Duration::from_secs(10 * 60),
|
||||
futures::future::poll_fn::<(), _>(|cx| self.poll_until_sync(cx)),
|
||||
@@ -1040,17 +1029,17 @@ where
|
||||
.expect("sync didn't happen within 10 mins");
|
||||
}
|
||||
|
||||
/// Wait until there are no pending packets.
|
||||
/// Run the network until there are no pending packets.
|
||||
///
|
||||
/// Calls `poll_until_idle` repeatedly with the runtime passed as parameter.
|
||||
async fn wait_until_idle(&mut self) {
|
||||
async fn run_until_idle(&mut self) {
|
||||
futures::future::poll_fn::<(), _>(|cx| self.poll_until_idle(cx)).await;
|
||||
}
|
||||
|
||||
/// Wait until all peers are connected to each other.
|
||||
/// Run the network until all peers are connected to each other.
|
||||
///
|
||||
/// Calls `poll_until_connected` repeatedly with the runtime passed as parameter.
|
||||
async fn wait_until_connected(&mut self) {
|
||||
async fn run_until_connected(&mut self) {
|
||||
futures::future::poll_fn::<(), _>(|cx| self.poll_until_connected(cx)).await;
|
||||
}
|
||||
|
||||
@@ -1082,20 +1071,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TestNet {
|
||||
rt_handle: tokio::runtime::Handle,
|
||||
peers: Vec<Peer<(), PeersClient>>,
|
||||
}
|
||||
|
||||
impl WithRuntime for TestNet {
|
||||
fn with_runtime(rt_handle: tokio::runtime::Handle) -> Self {
|
||||
TestNet { rt_handle, peers: Vec::new() }
|
||||
}
|
||||
fn rt_handle(&self) -> &tokio::runtime::Handle {
|
||||
&self.rt_handle
|
||||
}
|
||||
}
|
||||
|
||||
impl TestNetFactory for TestNet {
|
||||
type Verifier = PassThroughVerifier;
|
||||
type PeerData = ();
|
||||
@@ -1150,16 +1130,9 @@ impl JustificationImport<Block> for ForceFinalized {
|
||||
.map_err(|_| ConsensusError::InvalidJustification)
|
||||
}
|
||||
}
|
||||
pub struct JustificationTestNet(TestNet);
|
||||
|
||||
impl WithRuntime for JustificationTestNet {
|
||||
fn with_runtime(rt_handle: tokio::runtime::Handle) -> Self {
|
||||
JustificationTestNet(TestNet::with_runtime(rt_handle))
|
||||
}
|
||||
fn rt_handle(&self) -> &tokio::runtime::Handle {
|
||||
&self.0.rt_handle()
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct JustificationTestNet(TestNet);
|
||||
|
||||
impl TestNetFactory for JustificationTestNet {
|
||||
type Verifier = PassThroughVerifier;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user