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 {
} }
} }
#[test] macro_rules! common_tests {
fn test_single_block_lookup_happy_path() { ($mod_name: ident, $fork_name: ident, $response_type: ident) => {
let fork_name = ForkName::Base; #[cfg(test)]
let response_type = ResponseType::Block; mod $mod_name {
use super::*;
#[test]
fn test_single_block_lookup_happy_path() {
let fork_name = ForkName::$fork_name;
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,16 +224,16 @@ 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();
assert_eq!(bl.single_block_lookups.len(), 0); assert_eq!(bl.single_block_lookups.len(), 0);
} }
#[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();
@@ -243,12 +248,12 @@ fn test_single_block_lookup_empty_response() {
rig.expect_penalty(); rig.expect_penalty();
rig.expect_block_request(response_type); // it should be retried rig.expect_block_request(response_type); // it should be retried
} }
#[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();
@@ -267,11 +272,11 @@ fn test_single_block_lookup_wrong_response() {
// Send the stream termination. This should not produce an additional penalty. // Send the stream termination. This should not produce an additional penalty.
bl.single_block_lookup_response(id, peer_id, None, D, &mut cx); bl.single_block_lookup_response(id, peer_id, None, D, &mut cx);
rig.expect_empty_network(); rig.expect_empty_network();
} }
#[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();
@@ -285,12 +290,12 @@ fn test_single_block_lookup_failure() {
bl.single_block_lookup_failed(id, &peer_id, &mut cx, RPCError::UnsupportedProtocol); bl.single_block_lookup_failed(id, &peer_id, &mut cx, RPCError::UnsupportedProtocol);
rig.expect_block_request(response_type); rig.expect_block_request(response_type);
rig.expect_empty_network(); rig.expect_empty_network();
} }
#[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,19 +324,19 @@ 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);
rig.expect_parent_request(response_type); rig.expect_parent_request(response_type);
rig.expect_empty_network(); rig.expect_empty_network();
assert_eq!(bl.parent_lookups.len(), 1); assert_eq!(bl.parent_lookups.len(), 1);
} }
#[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();
@@ -364,12 +369,12 @@ fn test_parent_lookup_happy_path() {
}; };
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);
} }
#[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();
@@ -411,12 +416,12 @@ fn test_parent_lookup_wrong_response() {
}; };
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);
} }
#[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();
@@ -453,12 +458,12 @@ fn test_parent_lookup_empty_response() {
}; };
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);
} }
#[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();
@@ -502,12 +507,12 @@ fn test_parent_lookup_rpc_failure() {
}; };
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);
} }
#[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();
@@ -550,12 +561,12 @@ fn test_parent_lookup_too_many_attempts() {
} }
assert_eq!(bl.parent_lookups.len(), 0); assert_eq!(bl.parent_lookups.len(), 0);
} }
#[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);
@@ -596,12 +607,12 @@ fn test_parent_lookup_too_many_download_attempts_no_blacklist() {
assert_eq!(bl.parent_lookups.len(), 0); assert_eq!(bl.parent_lookups.len(), 0);
assert!(!bl.failed_chains.contains(&block_hash)); assert!(!bl.failed_chains.contains(&block_hash));
assert!(!bl.failed_chains.contains(&parent.canonical_root())); assert!(!bl.failed_chains.contains(&parent.canonical_root()));
} }
#[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);
@@ -648,15 +659,16 @@ fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
assert!(bl.failed_chains.contains(&block_root)); assert!(bl.failed_chains.contains(&block_root));
assert_eq!(bl.parent_lookups.len(), 0); assert_eq!(bl.parent_lookups.len(), 0);
} }
#[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,18 +704,18 @@ 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,
) )
} }
rig.expect_penalty(); rig.expect_penalty();
assert!(bl.failed_chains.contains(&chain_hash)); assert!(bl.failed_chains.contains(&chain_hash));
} }
#[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);
@@ -720,12 +732,12 @@ fn test_parent_lookup_disconnection() {
bl.peer_disconnected(&peer_id, &mut cx); bl.peer_disconnected(&peer_id, &mut cx);
assert!(bl.parent_lookups.is_empty()); assert!(bl.parent_lookups.is_empty());
} }
#[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,17 +768,17 @@ 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();
assert_eq!(bl.single_block_lookups.len(), 0); assert_eq!(bl.single_block_lookups.len(), 0);
} }
#[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,18 +802,18 @@ 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();
assert_eq!(bl.parent_lookups.len(), 0); assert_eq!(bl.parent_lookups.len(), 0);
} }
/// 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,
) )
} }
@@ -898,4 +910,11 @@ 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);