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

This commit is contained in:
Dmitry Markin
2022-12-16 16:29:39 +03:00
committed by GitHub
parent a5cab922a5
commit e14d6a5c4e
13 changed files with 996 additions and 1148 deletions
+21 -23
View File
@@ -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();