make tests into macros

This commit is contained in:
realbigsean
2023-04-27 13:02:53 -04:00
parent f2267212a5
commit 8e9c98e8e5

View File

@@ -191,10 +191,15 @@ impl TestRig {
} }
} }
macro_rules! common_tests {
($mod_name: ident, $fork_name: ident, $response_type: ident) => {
#[cfg(test)]
mod $mod_name {
use super::*;
#[test] #[test]
fn test_single_block_lookup_happy_path() { fn test_single_block_lookup_happy_path() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block = rig.rand_block(fork_name); let block = rig.rand_block(fork_name);
@@ -219,7 +224,7 @@ fn test_single_block_lookup_happy_path() {
bl.single_block_processed( bl.single_block_processed(
id, id,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)), BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_empty_network(); rig.expect_empty_network();
@@ -228,7 +233,7 @@ fn test_single_block_lookup_happy_path() {
#[test] #[test]
fn test_single_block_lookup_empty_response() { fn test_single_block_lookup_empty_response() {
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block_hash = Hash256::random(); let block_hash = Hash256::random();
@@ -247,8 +252,8 @@ fn test_single_block_lookup_empty_response() {
#[test] #[test]
fn test_single_block_lookup_wrong_response() { fn test_single_block_lookup_wrong_response() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block_hash = Hash256::random(); let block_hash = Hash256::random();
@@ -271,7 +276,7 @@ fn test_single_block_lookup_wrong_response() {
#[test] #[test]
fn test_single_block_lookup_failure() { fn test_single_block_lookup_failure() {
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block_hash = Hash256::random(); let block_hash = Hash256::random();
@@ -289,8 +294,8 @@ fn test_single_block_lookup_failure() {
#[test] #[test]
fn test_single_block_lookup_becomes_parent_request() { fn test_single_block_lookup_becomes_parent_request() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block = Arc::new(rig.rand_block(fork_name)); let block = Arc::new(rig.rand_block(fork_name));
@@ -319,7 +324,7 @@ fn test_single_block_lookup_becomes_parent_request() {
bl.single_block_processed( bl.single_block_processed(
id, id,
BlockError::ParentUnknown(block.into()).into(), BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
assert_eq!(bl.single_block_lookups.len(), 1); assert_eq!(bl.single_block_lookups.len(), 1);
@@ -330,8 +335,8 @@ fn test_single_block_lookup_becomes_parent_request() {
#[test] #[test]
fn test_parent_lookup_happy_path() { fn test_parent_lookup_happy_path() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -355,7 +360,7 @@ fn test_parent_lookup_happy_path() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockError::BlockIsAlreadyKnown.into(), BlockError::BlockIsAlreadyKnown.into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_parent_chain_process(); rig.expect_parent_chain_process();
@@ -368,8 +373,8 @@ fn test_parent_lookup_happy_path() {
#[test] #[test]
fn test_parent_lookup_wrong_response() { fn test_parent_lookup_wrong_response() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -402,7 +407,7 @@ fn test_parent_lookup_wrong_response() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)), BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_parent_chain_process(); rig.expect_parent_chain_process();
@@ -415,8 +420,8 @@ fn test_parent_lookup_wrong_response() {
#[test] #[test]
fn test_parent_lookup_empty_response() { fn test_parent_lookup_empty_response() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -444,7 +449,7 @@ fn test_parent_lookup_empty_response() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)), BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_parent_chain_process(); rig.expect_parent_chain_process();
@@ -457,8 +462,8 @@ fn test_parent_lookup_empty_response() {
#[test] #[test]
fn test_parent_lookup_rpc_failure() { fn test_parent_lookup_rpc_failure() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -493,7 +498,7 @@ fn test_parent_lookup_rpc_failure() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)), BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_parent_chain_process(); rig.expect_parent_chain_process();
@@ -506,8 +511,8 @@ fn test_parent_lookup_rpc_failure() {
#[test] #[test]
fn test_parent_lookup_too_many_attempts() { fn test_parent_lookup_too_many_attempts() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -538,7 +543,13 @@ fn test_parent_lookup_too_many_attempts() {
_ => { _ => {
// Send a bad block this time. It should be tried again. // Send a bad block this time. It should be tried again.
let bad_block = rig.rand_block(fork_name); let bad_block = rig.rand_block(fork_name);
bl.parent_lookup_response(id, peer_id, Some(bad_block.into()), D, &mut cx); bl.parent_lookup_response(
id,
peer_id,
Some(bad_block.into()),
D,
&mut cx,
);
// Send the stream termination // Send the stream termination
bl.parent_lookup_response(id, peer_id, None, D, &mut cx); bl.parent_lookup_response(id, peer_id, None, D, &mut cx);
rig.expect_penalty(); rig.expect_penalty();
@@ -554,8 +565,8 @@ fn test_parent_lookup_too_many_attempts() {
#[test] #[test]
fn test_parent_lookup_too_many_download_attempts_no_blacklist() { fn test_parent_lookup_too_many_download_attempts_no_blacklist() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -600,8 +611,8 @@ fn test_parent_lookup_too_many_download_attempts_no_blacklist() {
#[test] #[test]
fn test_parent_lookup_too_many_processing_attempts_must_blacklist() { fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
const PROCESSING_FAILURES: u8 = parent_lookup::PARENT_FAIL_TOLERANCE / 2 + 1; const PROCESSING_FAILURES: u8 = parent_lookup::PARENT_FAIL_TOLERANCE / 2 + 1;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
@@ -639,7 +650,7 @@ fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
bl.parent_block_processed( bl.parent_block_processed(
block_root, block_root,
BlockError::InvalidSignature.into(), BlockError::InvalidSignature.into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
bl.parent_lookup_response(id, peer_id, None, D, &mut cx); bl.parent_lookup_response(id, peer_id, None, D, &mut cx);
@@ -652,11 +663,12 @@ fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
#[test] #[test]
fn test_parent_lookup_too_deep() { fn test_parent_lookup_too_deep() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let mut blocks = let mut blocks = Vec::<Arc<SignedBeaconBlock<E>>>::with_capacity(
Vec::<Arc<SignedBeaconBlock<E>>>::with_capacity(parent_lookup::PARENT_DEPTH_TOLERANCE); parent_lookup::PARENT_DEPTH_TOLERANCE,
);
while blocks.len() < parent_lookup::PARENT_DEPTH_TOLERANCE { while blocks.len() < parent_lookup::PARENT_DEPTH_TOLERANCE {
let parent = blocks let parent = blocks
.last() .last()
@@ -692,7 +704,7 @@ fn test_parent_lookup_too_deep() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockError::ParentUnknown(block.into()).into(), BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
) )
} }
@@ -703,7 +715,7 @@ fn test_parent_lookup_too_deep() {
#[test] #[test]
fn test_parent_lookup_disconnection() { fn test_parent_lookup_disconnection() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let peer_id = PeerId::random(); let peer_id = PeerId::random();
let trigger_block = rig.rand_block(fork_name); let trigger_block = rig.rand_block(fork_name);
@@ -724,8 +736,8 @@ fn test_parent_lookup_disconnection() {
#[test] #[test]
fn test_single_block_lookup_ignored_response() { fn test_single_block_lookup_ignored_response() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block = rig.rand_block(fork_name); let block = rig.rand_block(fork_name);
@@ -756,7 +768,7 @@ fn test_single_block_lookup_ignored_response() {
bl.single_block_processed( bl.single_block_processed(
id, id,
BlockProcessingResult::Ignored, BlockProcessingResult::Ignored,
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_empty_network(); rig.expect_empty_network();
@@ -765,8 +777,8 @@ fn test_single_block_lookup_ignored_response() {
#[test] #[test]
fn test_parent_lookup_ignored_response() { fn test_parent_lookup_ignored_response() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false); let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name); let parent = rig.rand_block(fork_name);
@@ -790,7 +802,7 @@ fn test_parent_lookup_ignored_response() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockProcessingResult::Ignored, BlockProcessingResult::Ignored,
ResponseType::Block, $response_type,
&mut cx, &mut cx,
); );
rig.expect_empty_network(); rig.expect_empty_network();
@@ -800,8 +812,8 @@ fn test_parent_lookup_ignored_response() {
/// This is a regression test. /// This is a regression test.
#[test] #[test]
fn test_same_chain_race_condition() { fn test_same_chain_race_condition() {
let fork_name = ForkName::Base; let fork_name = ForkName::$fork_name;
let response_type = ResponseType::Block; let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(true); let (mut bl, mut cx, mut rig) = TestRig::test_setup(true);
#[track_caller] #[track_caller]
@@ -862,14 +874,14 @@ fn test_same_chain_race_condition() {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockError::BlockIsAlreadyKnown.into(), BlockError::BlockIsAlreadyKnown.into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
) )
} else { } else {
bl.parent_block_processed( bl.parent_block_processed(
chain_hash, chain_hash,
BlockError::ParentUnknown(block.into()).into(), BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block, $response_type,
&mut cx, &mut cx,
) )
} }
@@ -899,3 +911,10 @@ fn test_same_chain_race_condition() {
bl.parent_chain_processed(chain_hash, process_result, &mut cx); bl.parent_chain_processed(chain_hash, process_result, &mut cx);
assert_eq!(bl.parent_lookups.len(), 0); assert_eq!(bl.parent_lookups.len(), 0);
} }
}
};
}
use crate::sync::manager::ResponseType::Block;
common_tests!(base, Base, Block);
common_tests!(capella, Capella, Block);
common_tests!(deneb, Deneb, Block);