Skip to content
Snippets Groups Projects
Commit e14d6a5c authored by Dmitry Markin's avatar Dmitry Markin Committed by GitHub
Browse files

Clean up tests from `runtime.block_on()` and moving around `Runtime` and `Handle` (#12941)

parent a5cab922
No related merge requests found
Showing
with 987 additions and 1139 deletions
......@@ -77,7 +77,6 @@ pub(crate) mod tests {
use super::*;
use crate::tests::BeefyTestNet;
use sc_network_test::TestNetFactory;
use tokio::runtime::Runtime;
// also used in tests.rs
pub fn verify_persisted_version<B: BlockT, BE: Backend<B>>(backend: &BE) -> bool {
......@@ -85,10 +84,9 @@ pub(crate) mod tests {
version == CURRENT_VERSION
}
#[test]
fn should_load_persistent_sanity_checks() {
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn should_load_persistent_sanity_checks() {
let mut net = BeefyTestNet::new(1);
let backend = net.peer(0).client().as_backend();
// version not available in db -> None
......
This diff is collapsed.
......@@ -983,7 +983,6 @@ pub(crate) mod tests {
runtime::{Block, Digest, DigestItem, Header, H256},
Backend,
};
use tokio::runtime::Runtime;
impl<B: super::Block> PersistedState<B> {
pub fn voting_oracle(&self) -> &VoterOracle<B> {
......@@ -1295,12 +1294,11 @@ pub(crate) mod tests {
assert_eq!(extracted, Some(validator_set));
}
#[test]
fn keystore_vs_validator_set() {
#[tokio::test]
async fn keystore_vs_validator_set() {
let keys = &[Keyring::Alice];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let mut net = BeefyTestNet::new(1);
let mut worker = create_beefy_worker(&net.peer(0), &keys[0], 1, validator_set.clone());
// keystore doesn't contain other keys than validators'
......@@ -1319,12 +1317,11 @@ pub(crate) mod tests {
assert_eq!(worker.verify_validator_set(&1, &validator_set), expected_err);
}
#[test]
fn should_finalize_correctly() {
#[tokio::test]
async fn should_finalize_correctly() {
let keys = [Keyring::Alice];
let validator_set = ValidatorSet::new(make_beefy_ids(&keys), 0).unwrap();
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let mut net = BeefyTestNet::new(1);
let backend = net.peer(0).client().as_backend();
let mut worker = create_beefy_worker(&net.peer(0), &keys[0], 1, validator_set.clone());
// remove default session, will manually add custom one.
......@@ -1347,11 +1344,12 @@ pub(crate) mod tests {
// no 'best beefy block' or finality proofs
assert_eq!(worker.best_beefy_block(), 0);
runtime.block_on(poll_fn(move |cx| {
poll_fn(move |cx| {
assert_eq!(best_block_stream.poll_next_unpin(cx), Poll::Pending);
assert_eq!(finality_proof.poll_next_unpin(cx), Poll::Pending);
Poll::Ready(())
}));
})
.await;
// unknown hash for block #1
let (mut best_block_streams, mut finality_proofs) =
......@@ -1368,7 +1366,7 @@ pub(crate) mod tests {
worker.finalize(justif.clone()).unwrap();
// verify block finalized
assert_eq!(worker.best_beefy_block(), 1);
runtime.block_on(poll_fn(move |cx| {
poll_fn(move |cx| {
// unknown hash -> nothing streamed
assert_eq!(best_block_stream.poll_next_unpin(cx), Poll::Pending);
// commitment streamed
......@@ -1378,7 +1376,8 @@ pub(crate) mod tests {
v => panic!("unexpected value: {:?}", v),
}
Poll::Ready(())
}));
})
.await;
// generate 2 blocks, try again expect success
let (mut best_block_streams, _) = get_beefy_streams(&mut net, keys);
......@@ -1400,7 +1399,7 @@ pub(crate) mod tests {
assert_eq!(worker.active_rounds().unwrap().session_start(), 2);
// verify block finalized
assert_eq!(worker.best_beefy_block(), 2);
runtime.block_on(poll_fn(move |cx| {
poll_fn(move |cx| {
match best_block_stream.poll_next_unpin(cx) {
// expect Some(hash-of-block-2)
Poll::Ready(Some(hash)) => {
......@@ -1410,19 +1409,19 @@ pub(crate) mod tests {
v => panic!("unexpected value: {:?}", v),
}
Poll::Ready(())
}));
})
.await;
// check BEEFY justifications are also appended to backend
let justifs = backend.blockchain().justifications(hashof2).unwrap().unwrap();
assert!(justifs.get(BEEFY_ENGINE_ID).is_some())
}
#[test]
fn should_init_session() {
#[tokio::test]
async fn should_init_session() {
let keys = &[Keyring::Alice, Keyring::Bob];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let mut net = BeefyTestNet::new(1);
let mut worker = create_beefy_worker(&net.peer(0), &keys[0], 1, validator_set.clone());
let worker_rounds = worker.active_rounds().unwrap();
......@@ -1449,12 +1448,11 @@ pub(crate) mod tests {
assert_eq!(rounds.validator_set_id(), new_validator_set.id());
}
#[test]
fn should_triage_votes_and_process_later() {
#[tokio::test]
async fn should_triage_votes_and_process_later() {
let keys = &[Keyring::Alice, Keyring::Bob];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let mut net = BeefyTestNet::new(1);
let mut worker = create_beefy_worker(&net.peer(0), &keys[0], 1, validator_set.clone());
// remove default session, will manually add custom one.
worker.persisted_state.voting_oracle.sessions.clear();
......
......@@ -660,7 +660,6 @@ mod tests {
runtime::{Header, H256},
TestClient,
};
use tokio::runtime::{Handle, Runtime};
const SLOT_DURATION_MS: u64 = 1000;
......@@ -718,20 +717,11 @@ mod tests {
>;
type AuraPeer = Peer<(), PeersClient>;
#[derive(Default)]
pub struct AuraTestNet {
rt_handle: Handle,
peers: Vec<AuraPeer>,
}
impl WithRuntime for AuraTestNet {
fn with_runtime(rt_handle: Handle) -> Self {
AuraTestNet { rt_handle, peers: Vec::new() }
}
fn rt_handle(&self) -> &Handle {
&self.rt_handle
}
}
impl TestNetFactory for AuraTestNet {
type Verifier = AuraVerifier;
type PeerData = ();
......@@ -780,11 +770,10 @@ mod tests {
}
}
#[test]
fn authoring_blocks() {
#[tokio::test]
async fn authoring_blocks() {
sp_tracing::try_init_simple();
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 3);
let net = AuraTestNet::new(3);
let peers = &[(0, Keyring::Alice), (1, Keyring::Bob), (2, Keyring::Charlie)];
......@@ -850,13 +839,14 @@ mod tests {
);
}
runtime.block_on(future::select(
future::select(
future::poll_fn(move |cx| {
net.lock().poll(cx);
Poll::<()>::Pending
}),
future::select(future::join_all(aura_futures), future::join_all(import_notifications)),
));
)
.await;
}
#[test]
......@@ -875,10 +865,9 @@ mod tests {
);
}
#[test]
fn current_node_authority_should_claim_slot() {
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 4);
#[tokio::test]
async fn current_node_authority_should_claim_slot() {
let net = AuraTestNet::new(4);
let mut authorities = vec![
Keyring::Alice.public().into(),
......@@ -922,20 +911,19 @@ mod tests {
Default::default(),
Default::default(),
);
assert!(runtime.block_on(worker.claim_slot(&head, 0.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 1.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 2.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 3.into(), &authorities)).is_some());
assert!(runtime.block_on(worker.claim_slot(&head, 4.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 5.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 6.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 7.into(), &authorities)).is_some());
assert!(worker.claim_slot(&head, 0.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 1.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 2.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 3.into(), &authorities).await.is_some());
assert!(worker.claim_slot(&head, 4.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 5.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 6.into(), &authorities).await.is_none());
assert!(worker.claim_slot(&head, 7.into(), &authorities).await.is_some());
}
#[test]
fn on_slot_returns_correct_block() {
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 4);
#[tokio::test]
async fn on_slot_returns_correct_block() {
let net = AuraTestNet::new(4);
let keystore_path = tempfile::tempdir().expect("Creates keystore path");
let keystore = LocalKeystore::open(keystore_path.path(), None).expect("Creates keystore.");
......@@ -971,15 +959,16 @@ mod tests {
let head = client.header(&BlockId::Number(0)).unwrap().unwrap();
let res = runtime
.block_on(worker.on_slot(SlotInfo {
let res = worker
.on_slot(SlotInfo {
slot: 0.into(),
ends_at: Instant::now() + Duration::from_secs(100),
create_inherent_data: Box::new(()),
duration: Duration::from_millis(1000),
chain_head: head,
block_size_limit: None,
}))
})
.await
.unwrap();
// The returned block should be imported and we should be able to get its header by now.
......
......@@ -49,7 +49,6 @@ use sp_runtime::{
};
use sp_timestamp::Timestamp;
use std::{cell::RefCell, task::Poll, time::Duration};
use tokio::runtime::{Handle, Runtime};
type Item = DigestItem;
......@@ -227,20 +226,11 @@ where
type BabePeer = Peer<Option<PeerData>, BabeBlockImport>;
#[derive(Default)]
pub struct BabeTestNet {
rt_handle: Handle,
peers: Vec<BabePeer>,
}
impl WithRuntime for BabeTestNet {
fn with_runtime(rt_handle: Handle) -> Self {
BabeTestNet { rt_handle, peers: Vec::new() }
}
fn rt_handle(&self) -> &Handle {
&self.rt_handle
}
}
type TestHeader = <TestBlock as BlockT>::Header;
type TestSelectChain =
......@@ -366,12 +356,11 @@ impl TestNetFactory for BabeTestNet {
}
}
#[test]
#[tokio::test]
#[should_panic]
fn rejects_empty_block() {
async fn rejects_empty_block() {
sp_tracing::try_init_simple();
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 3);
let mut net = BabeTestNet::new(3);
let block_builder = |builder: BlockBuilder<_, _, _>| builder.build().unwrap().block;
net.mut_peers(|peer| {
peer[0].generate_blocks(1, BlockOrigin::NetworkInitialSync, block_builder);
......@@ -385,14 +374,13 @@ fn create_keystore(authority: Sr25519Keyring) -> SyncCryptoStorePtr {
keystore
}
fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static) {
async fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static) {
sp_tracing::try_init_simple();
let mutator = Arc::new(mutator) as Mutator;
MUTATOR.with(|m| *m.borrow_mut() = mutator.clone());
let runtime = Runtime::new().unwrap();
let net = BabeTestNet::new(runtime.handle().clone(), 3);
let net = BabeTestNet::new(3);
let peers = [Sr25519Keyring::Alice, Sr25519Keyring::Bob, Sr25519Keyring::Charlie];
......@@ -469,7 +457,7 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
.expect("Starts babe"),
);
}
runtime.block_on(future::select(
future::select(
futures::future::poll_fn(move |cx| {
let mut net = net.lock();
net.poll(cx);
......@@ -482,17 +470,18 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
Poll::<()>::Pending
}),
future::select(future::join_all(import_notifications), future::join_all(babe_futures)),
));
)
.await;
}
#[test]
fn authoring_blocks() {
run_one_test(|_, _| ())
#[tokio::test]
async fn authoring_blocks() {
run_one_test(|_, _| ()).await;
}
#[test]
#[tokio::test]
#[should_panic]
fn rejects_missing_inherent_digest() {
async fn rejects_missing_inherent_digest() {
run_one_test(|header: &mut TestHeader, stage| {
let v = std::mem::take(&mut header.digest_mut().logs);
header.digest_mut().logs = v
......@@ -500,11 +489,12 @@ fn rejects_missing_inherent_digest() {
.filter(|v| stage == Stage::PostSeal || v.as_babe_pre_digest().is_none())
.collect()
})
.await;
}
#[test]
#[tokio::test]
#[should_panic]
fn rejects_missing_seals() {
async fn rejects_missing_seals() {
run_one_test(|header: &mut TestHeader, stage| {
let v = std::mem::take(&mut header.digest_mut().logs);
header.digest_mut().logs = v
......@@ -512,18 +502,20 @@ fn rejects_missing_seals() {
.filter(|v| stage == Stage::PreSeal || v.as_babe_seal().is_none())
.collect()
})
.await;
}
#[test]
#[tokio::test]
#[should_panic]
fn rejects_missing_consensus_digests() {
async fn rejects_missing_consensus_digests() {
run_one_test(|header: &mut TestHeader, stage| {
let v = std::mem::take(&mut header.digest_mut().logs);
header.digest_mut().logs = v
.into_iter()
.filter(|v| stage == Stage::PostSeal || v.as_next_epoch_descriptor().is_none())
.collect()
});
})
.await;
}
#[test]
......@@ -601,14 +593,13 @@ fn can_author_block() {
}
// Propose and import a new BABE block on top of the given parent.
fn propose_and_import_block<Transaction: Send + 'static>(
async fn propose_and_import_block<Transaction: Send + 'static>(
parent: &TestHeader,
slot: Option<Slot>,
proposer_factory: &mut DummyFactory,
block_import: &mut BoxBlockImport<TestBlock, Transaction>,
runtime: &Runtime,
) -> Hash {
let mut proposer = runtime.block_on(proposer_factory.init(parent)).unwrap();
let mut proposer = proposer_factory.init(parent).await.unwrap();
let slot = slot.unwrap_or_else(|| {
let parent_pre_digest = find_pre_digest::<TestBlock>(parent).unwrap();
......@@ -624,7 +615,7 @@ fn propose_and_import_block<Transaction: Send + 'static>(
let parent_hash = parent.hash();
let mut block = runtime.block_on(proposer.propose_with(pre_digest)).unwrap().block;
let mut block = proposer.propose_with(pre_digest).await.unwrap().block;
let epoch_descriptor = proposer_factory
.epoch_changes
......@@ -660,8 +651,7 @@ fn propose_and_import_block<Transaction: Send + 'static>(
import
.insert_intermediate(INTERMEDIATE_KEY, BabeIntermediate::<TestBlock> { epoch_descriptor });
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
let import_result =
runtime.block_on(block_import.import_block(import, Default::default())).unwrap();
let import_result = block_import.import_block(import, Default::default()).await.unwrap();
match import_result {
ImportResult::Imported(_) => {},
......@@ -674,20 +664,19 @@ fn propose_and_import_block<Transaction: Send + 'static>(
// Propose and import n valid BABE blocks that are built on top of the given parent.
// The proposer takes care of producing epoch change digests according to the epoch
// duration (which is set to 6 slots in the test runtime).
fn propose_and_import_blocks<Transaction: Send + 'static>(
async fn propose_and_import_blocks<Transaction: Send + 'static>(
client: &PeersFullClient,
proposer_factory: &mut DummyFactory,
block_import: &mut BoxBlockImport<TestBlock, Transaction>,
parent_id: BlockId<TestBlock>,
n: usize,
runtime: &Runtime,
) -> Vec<Hash> {
let mut hashes = Vec::with_capacity(n);
let mut parent_header = client.header(&parent_id).unwrap().unwrap();
for _ in 0..n {
let block_hash =
propose_and_import_block(&parent_header, None, proposer_factory, block_import, runtime);
propose_and_import_block(&parent_header, None, proposer_factory, block_import).await;
hashes.push(block_hash);
parent_header = client.header(&BlockId::Hash(block_hash)).unwrap().unwrap();
}
......@@ -695,10 +684,9 @@ fn propose_and_import_blocks<Transaction: Send + 'static>(
hashes
}
#[test]
fn importing_block_one_sets_genesis_epoch() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn importing_block_one_sets_genesis_epoch() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -720,8 +708,8 @@ fn importing_block_one_sets_genesis_epoch() {
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
)
.await;
let genesis_epoch = Epoch::genesis(&data.link.config, 999.into());
......@@ -736,10 +724,9 @@ fn importing_block_one_sets_genesis_epoch() {
assert_eq!(epoch_for_second_block, genesis_epoch);
}
#[test]
fn revert_prunes_epoch_changes_and_removes_weights() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn revert_prunes_epoch_changes_and_removes_weights() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -756,17 +743,6 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
mutator: Arc::new(|_, _| ()),
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
// Test scenario.
// Information for epoch 19 is produced on three different forks at block #13.
// One branch starts before the revert point (epoch data should be maintained).
......@@ -779,10 +755,38 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
// \ revert *---- G(#13) ---- H(#19) ---#20 < fork #3
// \ to #10
// *-----E(#7)---#11 < fork #1
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 21);
let fork1 = propose_and_import_blocks_wrap(BlockId::Hash(canon[0]), 10);
let fork2 = propose_and_import_blocks_wrap(BlockId::Hash(canon[7]), 10);
let fork3 = propose_and_import_blocks_wrap(BlockId::Hash(canon[11]), 8);
let canon = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(0),
21,
)
.await;
let fork1 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[0]),
10,
)
.await;
let fork2 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[7]),
10,
)
.await;
let fork3 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[11]),
8,
)
.await;
// We should be tracking a total of 9 epochs in the fork tree
assert_eq!(epoch_changes.shared_data().tree().iter().count(), 8);
......@@ -824,10 +828,9 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
assert!(weight_data_check(&fork3, false));
}
#[test]
fn revert_not_allowed_for_finalized() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn revert_not_allowed_for_finalized() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -843,18 +846,14 @@ fn revert_not_allowed_for_finalized() {
mutator: Arc::new(|_, _| ()),
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 3);
let canon = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(0),
3,
)
.await;
// Finalize best block
client.finalize_block(canon[2], None, false).unwrap();
......@@ -870,10 +869,9 @@ fn revert_not_allowed_for_finalized() {
assert!(weight_data_check(&canon, true));
}
#[test]
fn importing_epoch_change_block_prunes_tree() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn importing_epoch_change_block_prunes_tree() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -889,17 +887,6 @@ fn importing_epoch_change_block_prunes_tree() {
mutator: Arc::new(|_, _| ()),
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
// This is the block tree that we're going to use in this test. Each node
// represents an epoch change block, the epoch duration is 6 slots.
//
......@@ -912,12 +899,40 @@ fn importing_epoch_change_block_prunes_tree() {
// Create and import the canon chain and keep track of fork blocks (A, C, D)
// from the diagram above.
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 30);
let canon = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(0),
30,
)
.await;
// Create the forks
let fork_1 = propose_and_import_blocks_wrap(BlockId::Hash(canon[0]), 10);
let fork_2 = propose_and_import_blocks_wrap(BlockId::Hash(canon[12]), 15);
let fork_3 = propose_and_import_blocks_wrap(BlockId::Hash(canon[18]), 10);
let fork_1 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[0]),
10,
)
.await;
let fork_2 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[12]),
15,
)
.await;
let fork_3 = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(canon[18]),
10,
)
.await;
// We should be tracking a total of 9 epochs in the fork tree
assert_eq!(epoch_changes.shared_data().tree().iter().count(), 9);
......@@ -928,7 +943,14 @@ fn importing_epoch_change_block_prunes_tree() {
// We finalize block #13 from the canon chain, so on the next epoch
// change the tree should be pruned, to not contain F (#7).
client.finalize_block(canon[12], None, false).unwrap();
propose_and_import_blocks_wrap(BlockId::Hash(client.chain_info().best_hash), 7);
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(client.chain_info().best_hash),
7,
)
.await;
let nodes: Vec<_> = epoch_changes.shared_data().tree().iter().map(|(h, _, _)| *h).collect();
......@@ -941,7 +963,14 @@ fn importing_epoch_change_block_prunes_tree() {
// finalizing block #25 from the canon chain should prune out the second fork
client.finalize_block(canon[24], None, false).unwrap();
propose_and_import_blocks_wrap(BlockId::Hash(client.chain_info().best_hash), 8);
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Hash(client.chain_info().best_hash),
8,
)
.await;
let nodes: Vec<_> = epoch_changes.shared_data().tree().iter().map(|(h, _, _)| *h).collect();
......@@ -954,11 +983,10 @@ fn importing_epoch_change_block_prunes_tree() {
assert!(nodes.iter().any(|h| *h == canon[24]));
}
#[test]
#[tokio::test]
#[should_panic]
fn verify_slots_are_strictly_increasing() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
async fn verify_slots_are_strictly_increasing() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -981,20 +1009,14 @@ fn verify_slots_are_strictly_increasing() {
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
)
.await;
let b1 = client.header(&BlockId::Hash(b1)).unwrap().unwrap();
// we should fail to import this block since the slot number didn't increase.
// we will panic due to the `PanickingBlockImport` defined above.
propose_and_import_block(
&b1,
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
propose_and_import_block(&b1, Some(999.into()), &mut proposer_factory, &mut block_import).await;
}
#[test]
......@@ -1027,10 +1049,9 @@ fn babe_transcript_generation_match() {
debug_assert!(test(orig_transcript) == test(transcript_from_data(new_transcript)));
}
#[test]
fn obsolete_blocks_aux_data_cleanup() {
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
#[tokio::test]
async fn obsolete_blocks_aux_data_cleanup() {
let mut net = BabeTestNet::new(1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
......@@ -1052,17 +1073,6 @@ fn obsolete_blocks_aux_data_cleanup() {
let mut block_import = data.block_import.lock().take().expect("import set up during init");
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
let aux_data_check = |hashes: &[Hash], expected: bool| {
hashes.iter().all(|hash| {
aux_schema::load_block_weight(&*peer.client().as_backend(), hash)
......@@ -1077,9 +1087,30 @@ fn obsolete_blocks_aux_data_cleanup() {
// G --- A1 --- A2 --- A3 --- A4 ( < fork1 )
// \-----C4 --- C5 ( < fork3 )
let fork1_hashes = propose_and_import_blocks_wrap(BlockId::Number(0), 4);
let fork2_hashes = propose_and_import_blocks_wrap(BlockId::Number(0), 2);
let fork3_hashes = propose_and_import_blocks_wrap(BlockId::Number(3), 2);
let fork1_hashes = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(0),
4,
)
.await;
let fork2_hashes = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(0),
2,
)
.await;
let fork3_hashes = propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
BlockId::Number(3),
2,
)
.await;
// Check that aux data is present for all but the genesis block.
assert!(aux_data_check(&[client.chain_info().genesis_hash], false));
......
This diff is collapsed.
......@@ -489,7 +489,7 @@ mod tests {
}))
}
#[tokio::test(flavor = "multi_thread")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn keeps_multiple_subscribers_per_topic_updated_with_both_old_and_new_messages() {
let topic = H256::default();
let protocol = ProtocolName::from("/my_protocol");
......
......@@ -483,20 +483,15 @@ mod tests {
use super::{NotificationsIn, NotificationsInOpen, NotificationsOut, NotificationsOutOpen};
use futures::{channel::oneshot, prelude::*};
use libp2p::core::upgrade;
use tokio::{
net::{TcpListener, TcpStream},
runtime::Runtime,
};
use tokio::net::{TcpListener, TcpStream};
use tokio_util::compat::TokioAsyncReadCompatExt;
#[test]
fn basic_works() {
#[tokio::test]
async fn basic_works() {
const PROTO_NAME: &str = "/test/proto/1";
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
let runtime = Runtime::new().unwrap();
let client = runtime.spawn(async move {
let client = tokio::spawn(async move {
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
let NotificationsOutOpen { handshake, mut substream, .. } = upgrade::apply_outbound(
socket.compat(),
......@@ -510,38 +505,34 @@ mod tests {
substream.send(b"test message".to_vec()).await.unwrap();
});
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
assert_eq!(handshake, b"initial message");
substream.send_handshake(&b"hello world"[..]);
assert_eq!(handshake, b"initial message");
substream.send_handshake(&b"hello world"[..]);
let msg = substream.next().await.unwrap().unwrap();
assert_eq!(msg.as_ref(), b"test message");
});
let msg = substream.next().await.unwrap().unwrap();
assert_eq!(msg.as_ref(), b"test message");
runtime.block_on(client).unwrap();
client.await.unwrap();
}
#[test]
fn empty_handshake() {
#[tokio::test]
async fn empty_handshake() {
// Check that everything still works when the handshake messages are empty.
const PROTO_NAME: &str = "/test/proto/1";
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
let runtime = Runtime::new().unwrap();
let client = runtime.spawn(async move {
let client = tokio::spawn(async move {
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
let NotificationsOutOpen { handshake, mut substream, .. } = upgrade::apply_outbound(
socket.compat(),
......@@ -555,36 +546,32 @@ mod tests {
substream.send(Default::default()).await.unwrap();
});
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
assert!(handshake.is_empty());
substream.send_handshake(vec![]);
assert!(handshake.is_empty());
substream.send_handshake(vec![]);
let msg = substream.next().await.unwrap().unwrap();
assert!(msg.as_ref().is_empty());
});
let msg = substream.next().await.unwrap().unwrap();
assert!(msg.as_ref().is_empty());
runtime.block_on(client).unwrap();
client.await.unwrap();
}
#[test]
fn refused() {
#[tokio::test]
async fn refused() {
const PROTO_NAME: &str = "/test/proto/1";
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
let runtime = Runtime::new().unwrap();
let client = runtime.spawn(async move {
let client = tokio::spawn(async move {
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
let outcome = upgrade::apply_outbound(
socket.compat(),
......@@ -599,35 +586,31 @@ mod tests {
assert!(outcome.is_err());
});
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
assert_eq!(handshake, b"hello");
assert_eq!(handshake, b"hello");
// We successfully upgrade to the protocol, but then close the substream.
drop(substream);
});
// We successfully upgrade to the protocol, but then close the substream.
drop(substream);
runtime.block_on(client).unwrap();
client.await.unwrap();
}
#[test]
fn large_initial_message_refused() {
#[tokio::test]
async fn large_initial_message_refused() {
const PROTO_NAME: &str = "/test/proto/1";
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
let runtime = Runtime::new().unwrap();
let client = runtime.spawn(async move {
let client = tokio::spawn(async move {
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
let ret = upgrade::apply_outbound(
socket.compat(),
......@@ -644,30 +627,26 @@ mod tests {
assert!(ret.is_err());
});
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let (socket, _) = listener.accept().await.unwrap();
let ret = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await;
assert!(ret.is_err());
});
let (socket, _) = listener.accept().await.unwrap();
let ret = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await;
assert!(ret.is_err());
runtime.block_on(client).unwrap();
client.await.unwrap();
}
#[test]
fn large_handshake_refused() {
#[tokio::test]
async fn large_handshake_refused() {
const PROTO_NAME: &str = "/test/proto/1";
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
let runtime = Runtime::new().unwrap();
let client = runtime.spawn(async move {
let client = tokio::spawn(async move {
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
let ret = upgrade::apply_outbound(
socket.compat(),
......@@ -678,24 +657,22 @@ mod tests {
assert!(ret.is_err());
});
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
assert_eq!(handshake, b"initial message");
let (socket, _) = listener.accept().await.unwrap();
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
socket.compat(),
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
)
.await
.unwrap();
assert_eq!(handshake, b"initial message");
// We check that a handshake that is too large gets refused.
substream.send_handshake((0..32768).map(|_| 0).collect::<Vec<_>>());
let _ = substream.next().await;
});
// We check that a handshake that is too large gets refused.
substream.send_handshake((0..32768).map(|_| 0).collect::<Vec<_>>());
let _ = substream.next().await;
runtime.block_on(client).unwrap();
client.await.unwrap();
}
}
......@@ -44,7 +44,6 @@ use std::{
time::Duration,
};
use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _};
use tokio::runtime::Handle;
fn set_default_expecations_no_peers(
chain_sync: &mut MockChainSync<substrate_test_runtime_client::runtime::Block>,
......@@ -72,7 +71,7 @@ async fn normal_network_poll_no_peers() {
let chain_sync_service =
Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());
let mut network = TestNetworkBuilder::new(Handle::current())
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, chain_sync_service))
.build();
......@@ -104,7 +103,7 @@ async fn request_justification() {
let mut chain_sync = MockChainSync::<substrate_test_runtime_client::runtime::Block>::new();
set_default_expecations_no_peers(&mut chain_sync);
let mut network = TestNetworkBuilder::new(Handle::current())
let mut network = TestNetworkBuilder::new()
.with_chain_sync((Box::new(chain_sync), chain_sync_service))
.build();
......@@ -135,7 +134,7 @@ async fn clear_justification_requests() {
Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());
set_default_expecations_no_peers(&mut chain_sync);
let mut network = TestNetworkBuilder::new(Handle::current())
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, chain_sync_service))
.build();
......@@ -174,7 +173,7 @@ async fn set_sync_fork_request() {
.once()
.returning(|_, _, _| ());
let mut network = TestNetworkBuilder::new(Handle::current())
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, Box::new(chain_sync_service)))
.build();
......@@ -218,7 +217,7 @@ async fn on_block_finalized() {
.returning(|_, _| ());
set_default_expecations_no_peers(&mut chain_sync);
let mut network = TestNetworkBuilder::new(Handle::current())
let mut network = TestNetworkBuilder::new()
.with_client(client)
.with_chain_sync((chain_sync, chain_sync_service))
.build();
......@@ -316,7 +315,7 @@ async fn invalid_justification_imported() {
let justification_info = Arc::new(RwLock::new(None));
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (service1, mut event_stream1) = TestNetworkBuilder::new(Handle::current())
let (service1, mut event_stream1) = TestNetworkBuilder::new()
.with_import_queue(Box::new(DummyImportQueue(
justification_info.clone(),
DummyImportQueueHandle {},
......@@ -325,7 +324,7 @@ async fn invalid_justification_imported() {
.build()
.start_network();
let (service2, mut event_stream2) = TestNetworkBuilder::new(Handle::current())
let (service2, mut event_stream2) = TestNetworkBuilder::new()
.with_set_config(SetConfig {
reserved_nodes: vec![MultiaddrWithPeerId {
multiaddr: listen_addr,
......@@ -393,7 +392,7 @@ async fn disconnect_peer_using_chain_sync_handle() {
)
.unwrap();
let (node1, mut event_stream1) = TestNetworkBuilder::new(Handle::current())
let (node1, mut event_stream1) = TestNetworkBuilder::new()
.with_listen_addresses(vec![listen_addr.clone()])
.with_chain_sync((Box::new(chain_sync), Box::new(chain_sync_service)))
.with_chain_sync_network((chain_sync_network_provider, chain_sync_network_handle))
......@@ -401,7 +400,7 @@ async fn disconnect_peer_using_chain_sync_handle() {
.build()
.start_network();
let (node2, mut event_stream2) = TestNetworkBuilder::new(Handle::current())
let (node2, mut event_stream2) = TestNetworkBuilder::new()
.with_set_config(SetConfig {
reserved_nodes: vec![MultiaddrWithPeerId {
multiaddr: listen_addr,
......
......@@ -44,7 +44,6 @@ use substrate_test_runtime_client::{
runtime::{Block as TestBlock, Hash as TestHash},
TestClient, TestClientBuilder, TestClientBuilderExt as _,
};
use tokio::runtime::Handle;
#[cfg(test)]
mod chain_sync;
......@@ -59,12 +58,11 @@ const PROTOCOL_NAME: &str = "/foo";
struct TestNetwork {
network: TestNetworkWorker,
rt_handle: Handle,
}
impl TestNetwork {
pub fn new(network: TestNetworkWorker, rt_handle: Handle) -> Self {
Self { network, rt_handle }
pub fn new(network: TestNetworkWorker) -> Self {
Self { network }
}
pub fn service(&self) -> &Arc<TestNetworkService> {
......@@ -82,7 +80,7 @@ impl TestNetwork {
let service = worker.service().clone();
let event_stream = service.event_stream("test");
self.rt_handle.spawn(async move {
tokio::spawn(async move {
futures::pin_mut!(worker);
let _ = worker.await;
});
......@@ -100,11 +98,10 @@ struct TestNetworkBuilder {
chain_sync: Option<(Box<dyn ChainSyncT<TestBlock>>, Box<dyn ChainSyncInterface<TestBlock>>)>,
chain_sync_network: Option<(NetworkServiceProvider, NetworkServiceHandle)>,
config: Option<config::NetworkConfiguration>,
rt_handle: Handle,
}
impl TestNetworkBuilder {
pub fn new(rt_handle: Handle) -> Self {
pub fn new() -> Self {
Self {
import_queue: None,
link: None,
......@@ -114,7 +111,6 @@ impl TestNetworkBuilder {
chain_sync: None,
chain_sync_network: None,
config: None,
rt_handle,
}
}
......@@ -229,21 +225,21 @@ impl TestNetworkBuilder {
let block_request_protocol_config = {
let (handler, protocol_config) =
BlockRequestHandler::new(&protocol_id, None, client.clone(), 50);
self.rt_handle.spawn(handler.run().boxed());
tokio::spawn(handler.run().boxed());
protocol_config
};
let state_request_protocol_config = {
let (handler, protocol_config) =
StateRequestHandler::new(&protocol_id, None, client.clone(), 50);
self.rt_handle.spawn(handler.run().boxed());
tokio::spawn(handler.run().boxed());
protocol_config
};
let light_client_request_protocol_config = {
let (handler, protocol_config) =
LightClientRequestHandler::new(&protocol_id, None, client.clone());
self.rt_handle.spawn(handler.run().boxed());
tokio::spawn(handler.run().boxed());
protocol_config
};
......@@ -310,11 +306,6 @@ impl TestNetworkBuilder {
.link
.unwrap_or(Box::new(sc_network_sync::service::mock::MockChainSyncInterface::new()));
let handle = self.rt_handle.clone();
let executor = move |f| {
handle.spawn(f);
};
let worker = NetworkWorker::<
substrate_test_runtime_client::runtime::Block,
substrate_test_runtime_client::runtime::Hash,
......@@ -322,7 +313,9 @@ impl TestNetworkBuilder {
>::new(config::Params {
block_announce_config,
role: config::Role::Full,
executor: Box::new(executor),
executor: Box::new(|f| {
tokio::spawn(f);
}),
network_config,
chain: client.clone(),
protocol_id,
......@@ -340,10 +333,10 @@ impl TestNetworkBuilder {
.unwrap();
let service = worker.service().clone();
self.rt_handle.spawn(async move {
tokio::spawn(async move {
let _ = chain_sync_network_provider.run(service).await;
});
self.rt_handle.spawn(async move {
tokio::spawn(async move {
loop {
futures::future::poll_fn(|cx| {
import_queue.poll_actions(cx, &mut *link);
......@@ -354,6 +347,6 @@ impl TestNetworkBuilder {
}
});
TestNetwork::new(worker, self.rt_handle)
TestNetwork::new(worker)
}
}
......@@ -26,7 +26,6 @@ use sc_network_common::{
service::{NetworkNotification, NetworkPeers, NetworkStateInfo},
};
use std::{sync::Arc, time::Duration};
use tokio::runtime::Handle;
type TestNetworkService = NetworkService<
substrate_test_runtime_client::runtime::Block,
......@@ -38,9 +37,7 @@ const PROTOCOL_NAME: &str = "/foo";
/// Builds two nodes and their associated events stream.
/// The nodes are connected together and have the `PROTOCOL_NAME` protocol registered.
fn build_nodes_one_proto(
rt_handle: &Handle,
) -> (
fn build_nodes_one_proto() -> (
Arc<TestNetworkService>,
impl Stream<Item = Event>,
Arc<TestNetworkService>,
......@@ -48,12 +45,12 @@ fn build_nodes_one_proto(
) {
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (node1, events_stream1) = TestNetworkBuilder::new(rt_handle.clone())
let (node1, events_stream1) = TestNetworkBuilder::new()
.with_listen_addresses(vec![listen_addr.clone()])
.build()
.start_network();
let (node2, events_stream2) = TestNetworkBuilder::new(rt_handle.clone())
let (node2, events_stream2) = TestNetworkBuilder::new()
.with_set_config(SetConfig {
reserved_nodes: vec![MultiaddrWithPeerId {
multiaddr: listen_addr,
......@@ -67,15 +64,12 @@ fn build_nodes_one_proto(
(node1, events_stream1, node2, events_stream2)
}
#[test]
fn notifications_state_consistent() {
#[tokio::test]
async fn notifications_state_consistent() {
// Runs two nodes and ensures that events are propagated out of the API in a consistent
// correct order, which means no notification received on a closed substream.
let runtime = tokio::runtime::Runtime::new().unwrap();
let (node1, mut events_stream1, node2, mut events_stream2) =
build_nodes_one_proto(runtime.handle());
let (node1, mut events_stream1, node2, mut events_stream2) = build_nodes_one_proto();
// Write some initial notifications that shouldn't get through.
for _ in 0..(rand::random::<u8>() % 5) {
......@@ -93,140 +87,130 @@ fn notifications_state_consistent() {
);
}
runtime.block_on(async move {
// True if we have an active substream from node1 to node2.
let mut node1_to_node2_open = false;
// True if we have an active substream from node2 to node1.
let mut node2_to_node1_open = false;
// We stop the test after a certain number of iterations.
let mut iterations = 0;
// Safe guard because we don't want the test to pass if no substream has been open.
let mut something_happened = false;
// True if we have an active substream from node1 to node2.
let mut node1_to_node2_open = false;
// True if we have an active substream from node2 to node1.
let mut node2_to_node1_open = false;
// We stop the test after a certain number of iterations.
let mut iterations = 0;
// Safe guard because we don't want the test to pass if no substream has been open.
let mut something_happened = false;
loop {
iterations += 1;
if iterations >= 1_000 {
assert!(something_happened);
break
}
loop {
iterations += 1;
if iterations >= 1_000 {
assert!(something_happened);
break
}
// Start by sending a notification from node1 to node2 and vice-versa. Part of the
// test consists in ensuring that notifications get ignored if the stream isn't open.
if rand::random::<u8>() % 5 >= 3 {
node1.write_notification(
node2.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
if rand::random::<u8>() % 5 >= 3 {
node2.write_notification(
node1.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
// Start by sending a notification from node1 to node2 and vice-versa. Part of the
// test consists in ensuring that notifications get ignored if the stream isn't open.
if rand::random::<u8>() % 5 >= 3 {
node1.write_notification(
node2.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
if rand::random::<u8>() % 5 >= 3 {
node2.write_notification(
node1.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
// Also randomly disconnect the two nodes from time to time.
if rand::random::<u8>() % 20 == 0 {
node1.disconnect_peer(node2.local_peer_id(), PROTOCOL_NAME.into());
}
if rand::random::<u8>() % 20 == 0 {
node2.disconnect_peer(node1.local_peer_id(), PROTOCOL_NAME.into());
}
// Also randomly disconnect the two nodes from time to time.
if rand::random::<u8>() % 20 == 0 {
node1.disconnect_peer(node2.local_peer_id(), PROTOCOL_NAME.into());
// Grab next event from either `events_stream1` or `events_stream2`.
let next_event = {
let next1 = events_stream1.next();
let next2 = events_stream2.next();
// We also await on a small timer, otherwise it is possible for the test to wait
// forever while nothing at all happens on the network.
let continue_test = futures_timer::Delay::new(Duration::from_millis(20));
match future::select(future::select(next1, next2), continue_test).await {
future::Either::Left((future::Either::Left((Some(ev), _)), _)) =>
future::Either::Left(ev),
future::Either::Left((future::Either::Right((Some(ev), _)), _)) =>
future::Either::Right(ev),
future::Either::Right(_) => continue,
_ => break,
}
if rand::random::<u8>() % 20 == 0 {
node2.disconnect_peer(node1.local_peer_id(), PROTOCOL_NAME.into());
}
// Grab next event from either `events_stream1` or `events_stream2`.
let next_event = {
let next1 = events_stream1.next();
let next2 = events_stream2.next();
// We also await on a small timer, otherwise it is possible for the test to wait
// forever while nothing at all happens on the network.
let continue_test = futures_timer::Delay::new(Duration::from_millis(20));
match future::select(future::select(next1, next2), continue_test).await {
future::Either::Left((future::Either::Left((Some(ev), _)), _)) =>
future::Either::Left(ev),
future::Either::Left((future::Either::Right((Some(ev), _)), _)) =>
future::Either::Right(ev),
future::Either::Right(_) => continue,
_ => break,
}
};
match next_event {
future::Either::Left(Event::NotificationStreamOpened {
remote, protocol, ..
}) =>
if protocol == PROTOCOL_NAME.into() {
something_happened = true;
assert!(!node1_to_node2_open);
node1_to_node2_open = true;
assert_eq!(remote, node2.local_peer_id());
},
future::Either::Right(Event::NotificationStreamOpened {
remote, protocol, ..
}) =>
if protocol == PROTOCOL_NAME.into() {
something_happened = true;
assert!(!node2_to_node1_open);
node2_to_node1_open = true;
assert_eq!(remote, node1.local_peer_id());
},
future::Either::Left(Event::NotificationStreamClosed {
remote, protocol, ..
}) =>
if protocol == PROTOCOL_NAME.into() {
assert!(node1_to_node2_open);
node1_to_node2_open = false;
assert_eq!(remote, node2.local_peer_id());
},
future::Either::Right(Event::NotificationStreamClosed {
remote, protocol, ..
}) =>
if protocol == PROTOCOL_NAME.into() {
assert!(node2_to_node1_open);
node2_to_node1_open = false;
assert_eq!(remote, node1.local_peer_id());
},
future::Either::Left(Event::NotificationsReceived { remote, .. }) => {
};
match next_event {
future::Either::Left(Event::NotificationStreamOpened { remote, protocol, .. }) =>
if protocol == PROTOCOL_NAME.into() {
something_happened = true;
assert!(!node1_to_node2_open);
node1_to_node2_open = true;
assert_eq!(remote, node2.local_peer_id());
},
future::Either::Right(Event::NotificationStreamOpened { remote, protocol, .. }) =>
if protocol == PROTOCOL_NAME.into() {
something_happened = true;
assert!(!node2_to_node1_open);
node2_to_node1_open = true;
assert_eq!(remote, node1.local_peer_id());
},
future::Either::Left(Event::NotificationStreamClosed { remote, protocol, .. }) =>
if protocol == PROTOCOL_NAME.into() {
assert!(node1_to_node2_open);
node1_to_node2_open = false;
assert_eq!(remote, node2.local_peer_id());
if rand::random::<u8>() % 5 >= 4 {
node1.write_notification(
node2.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
},
future::Either::Right(Event::NotificationsReceived { remote, .. }) => {
future::Either::Right(Event::NotificationStreamClosed { remote, protocol, .. }) =>
if protocol == PROTOCOL_NAME.into() {
assert!(node2_to_node1_open);
node2_to_node1_open = false;
assert_eq!(remote, node1.local_peer_id());
if rand::random::<u8>() % 5 >= 4 {
node2.write_notification(
node1.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
},
future::Either::Left(Event::NotificationsReceived { remote, .. }) => {
assert!(node1_to_node2_open);
assert_eq!(remote, node2.local_peer_id());
if rand::random::<u8>() % 5 >= 4 {
node1.write_notification(
node2.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
},
future::Either::Right(Event::NotificationsReceived { remote, .. }) => {
assert!(node2_to_node1_open);
assert_eq!(remote, node1.local_peer_id());
if rand::random::<u8>() % 5 >= 4 {
node2.write_notification(
node1.local_peer_id(),
PROTOCOL_NAME.into(),
b"hello world".to_vec(),
);
}
},
// Add new events here.
future::Either::Left(Event::SyncConnected { .. }) => {},
future::Either::Right(Event::SyncConnected { .. }) => {},
future::Either::Left(Event::SyncDisconnected { .. }) => {},
future::Either::Right(Event::SyncDisconnected { .. }) => {},
future::Either::Left(Event::Dht(_)) => {},
future::Either::Right(Event::Dht(_)) => {},
};
}
});
// Add new events here.
future::Either::Left(Event::SyncConnected { .. }) => {},
future::Either::Right(Event::SyncConnected { .. }) => {},
future::Either::Left(Event::SyncDisconnected { .. }) => {},
future::Either::Right(Event::SyncDisconnected { .. }) => {},
future::Either::Left(Event::Dht(_)) => {},
future::Either::Right(Event::Dht(_)) => {},
};
}
}
#[tokio::test]
async fn lots_of_incoming_peers_works() {
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (main_node, _) = TestNetworkBuilder::new(Handle::current())
let (main_node, _) = TestNetworkBuilder::new()
.with_listen_addresses(vec![listen_addr.clone()])
.with_set_config(SetConfig { in_peers: u32::MAX, ..Default::default() })
.build()
......@@ -239,7 +223,7 @@ async fn lots_of_incoming_peers_works() {
let mut background_tasks_to_wait = Vec::new();
for _ in 0..32 {
let (_dialing_node, event_stream) = TestNetworkBuilder::new(Handle::current())
let (_dialing_node, event_stream) = TestNetworkBuilder::new()
.with_set_config(SetConfig {
reserved_nodes: vec![MultiaddrWithPeerId {
multiaddr: listen_addr.clone(),
......@@ -286,20 +270,17 @@ async fn lots_of_incoming_peers_works() {
future::join_all(background_tasks_to_wait).await;
}
#[test]
fn notifications_back_pressure() {
#[tokio::test]
async fn notifications_back_pressure() {
// Node 1 floods node 2 with notifications. Random sleeps are done on node 2 to simulate the
// node being busy. We make sure that all notifications are received.
const TOTAL_NOTIFS: usize = 10_000;
let runtime = tokio::runtime::Runtime::new().unwrap();
let (node1, mut events_stream1, node2, mut events_stream2) =
build_nodes_one_proto(runtime.handle());
let (node1, mut events_stream1, node2, mut events_stream2) = build_nodes_one_proto();
let node2_id = node2.local_peer_id();
let receiver = runtime.spawn(async move {
let receiver = tokio::spawn(async move {
let mut received_notifications = 0;
while received_notifications < TOTAL_NOTIFS {
......@@ -320,40 +301,36 @@ fn notifications_back_pressure() {
}
});
runtime.block_on(async move {
// Wait for the `NotificationStreamOpened`.
loop {
match events_stream1.next().await.unwrap() {
Event::NotificationStreamOpened { .. } => break,
_ => {},
};
}
// Wait for the `NotificationStreamOpened`.
loop {
match events_stream1.next().await.unwrap() {
Event::NotificationStreamOpened { .. } => break,
_ => {},
};
}
// Sending!
for num in 0..TOTAL_NOTIFS {
let notif = node1.notification_sender(node2_id, PROTOCOL_NAME.into()).unwrap();
notif
.ready()
.await
.unwrap()
.send(format!("hello #{}", num).into_bytes())
.unwrap();
}
// Sending!
for num in 0..TOTAL_NOTIFS {
let notif = node1.notification_sender(node2_id, PROTOCOL_NAME.into()).unwrap();
notif
.ready()
.await
.unwrap()
.send(format!("hello #{}", num).into_bytes())
.unwrap();
}
receiver.await.unwrap();
});
receiver.await.unwrap();
}
#[test]
fn fallback_name_working() {
#[tokio::test]
async fn fallback_name_working() {
// Node 1 supports the protocols "new" and "old". Node 2 only supports "old". Checks whether
// they can connect.
const NEW_PROTOCOL_NAME: &str = "/new-shiny-protocol-that-isnt-PROTOCOL_NAME";
let runtime = tokio::runtime::Runtime::new().unwrap();
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (node1, mut events_stream1) = TestNetworkBuilder::new(runtime.handle().clone())
let (node1, mut events_stream1) = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
extra_sets: vec![NonDefaultSetConfig {
notifications_protocol: NEW_PROTOCOL_NAME.into(),
......@@ -369,7 +346,7 @@ fn fallback_name_working() {
.build()
.start_network();
let (_, mut events_stream2) = TestNetworkBuilder::new(runtime.handle().clone())
let (_, mut events_stream2) = TestNetworkBuilder::new()
.with_set_config(SetConfig {
reserved_nodes: vec![MultiaddrWithPeerId {
multiaddr: listen_addr,
......@@ -380,7 +357,7 @@ fn fallback_name_working() {
.build()
.start_network();
let receiver = runtime.spawn(async move {
let receiver = tokio::spawn(async move {
// Wait for the `NotificationStreamOpened`.
loop {
match events_stream2.next().await.unwrap() {
......@@ -394,30 +371,27 @@ fn fallback_name_working() {
}
});
runtime.block_on(async move {
// Wait for the `NotificationStreamOpened`.
loop {
match events_stream1.next().await.unwrap() {
Event::NotificationStreamOpened { protocol, negotiated_fallback, .. }
if protocol == NEW_PROTOCOL_NAME.into() =>
{
assert_eq!(negotiated_fallback, Some(PROTOCOL_NAME.into()));
break
},
_ => {},
};
}
// Wait for the `NotificationStreamOpened`.
loop {
match events_stream1.next().await.unwrap() {
Event::NotificationStreamOpened { protocol, negotiated_fallback, .. }
if protocol == NEW_PROTOCOL_NAME.into() =>
{
assert_eq!(negotiated_fallback, Some(PROTOCOL_NAME.into()));
break
},
_ => {},
};
}
receiver.await.unwrap();
});
receiver.await.unwrap();
}
// Disconnect peer by calling `Protocol::disconnect_peer()` with the supplied block announcement
// protocol name and verify that `SyncDisconnected` event is emitted
#[tokio::test]
async fn disconnect_sync_peer_using_block_announcement_protocol_name() {
let (node1, mut events_stream1, node2, mut events_stream2) =
build_nodes_one_proto(&Handle::current());
let (node1, mut events_stream1, node2, mut events_stream2) = build_nodes_one_proto();
async fn wait_for_events(stream: &mut (impl Stream<Item = Event> + std::marker::Unpin)) {
let mut notif_received = false;
......@@ -454,7 +428,7 @@ async fn disconnect_sync_peer_using_block_announcement_protocol_name() {
async fn ensure_listen_addresses_consistent_with_transport_memory() {
let listen_addr = config::build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(0_u16)];
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
transport: TransportConfig::MemoryOnly,
......@@ -474,7 +448,7 @@ async fn ensure_listen_addresses_consistent_with_transport_memory() {
async fn ensure_listen_addresses_consistent_with_transport_not_memory() {
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
..config::NetworkConfiguration::new(
......@@ -497,7 +471,7 @@ async fn ensure_boot_node_addresses_consistent_with_transport_memory() {
peer_id: PeerId::random(),
};
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
transport: TransportConfig::MemoryOnly,
......@@ -522,7 +496,7 @@ async fn ensure_boot_node_addresses_consistent_with_transport_not_memory() {
peer_id: PeerId::random(),
};
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
boot_nodes: vec![boot_node],
......@@ -546,7 +520,7 @@ async fn ensure_reserved_node_addresses_consistent_with_transport_memory() {
peer_id: PeerId::random(),
};
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
transport: TransportConfig::MemoryOnly,
......@@ -574,7 +548,7 @@ async fn ensure_reserved_node_addresses_consistent_with_transport_not_memory() {
peer_id: PeerId::random(),
};
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
default_peers_set: SetConfig {
......@@ -598,7 +572,7 @@ async fn ensure_public_addresses_consistent_with_transport_memory() {
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let public_address = config::build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(0_u16)];
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
transport: TransportConfig::MemoryOnly,
......@@ -620,7 +594,7 @@ async fn ensure_public_addresses_consistent_with_transport_not_memory() {
let listen_addr = config::build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(0_u16)];
let public_address = config::build_multiaddr![Memory(rand::random::<u64>())];
let _ = TestNetworkBuilder::new(Handle::current())
let _ = TestNetworkBuilder::new()
.with_config(config::NetworkConfiguration {
listen_addresses: vec![listen_addr.clone()],
public_addresses: vec![public_address],
......
......@@ -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;
......
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment