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]
fn test_single_block_lookup_happy_path() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
macro_rules! common_tests {
($mod_name: ident, $fork_name: ident, $response_type: ident) => {
#[cfg(test)]
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 block = rig.rand_block(fork_name);
@@ -219,16 +224,16 @@ fn test_single_block_lookup_happy_path() {
bl.single_block_processed(
id,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block,
$response_type,
&mut cx,
);
rig.expect_empty_network();
assert_eq!(bl.single_block_lookups.len(), 0);
}
}
#[test]
fn test_single_block_lookup_empty_response() {
let response_type = ResponseType::Block;
#[test]
fn test_single_block_lookup_empty_response() {
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block_hash = Hash256::random();
@@ -243,12 +248,12 @@ fn test_single_block_lookup_empty_response() {
rig.expect_penalty();
rig.expect_block_request(response_type); // it should be retried
}
}
#[test]
fn test_single_block_lookup_wrong_response() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_single_block_lookup_wrong_response() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
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.
bl.single_block_lookup_response(id, peer_id, None, D, &mut cx);
rig.expect_empty_network();
}
}
#[test]
fn test_single_block_lookup_failure() {
let response_type = ResponseType::Block;
#[test]
fn test_single_block_lookup_failure() {
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
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);
rig.expect_block_request(response_type);
rig.expect_empty_network();
}
}
#[test]
fn test_single_block_lookup_becomes_parent_request() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_single_block_lookup_becomes_parent_request() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
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(
id,
BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block,
$response_type,
&mut cx,
);
assert_eq!(bl.single_block_lookups.len(), 1);
rig.expect_parent_request(response_type);
rig.expect_empty_network();
assert_eq!(bl.parent_lookups.len(), 1);
}
}
#[test]
fn test_parent_lookup_happy_path() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_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 parent = rig.rand_block(fork_name);
@@ -355,7 +360,7 @@ fn test_parent_lookup_happy_path() {
bl.parent_block_processed(
chain_hash,
BlockError::BlockIsAlreadyKnown.into(),
ResponseType::Block,
$response_type,
&mut cx,
);
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);
assert_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_wrong_response() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_wrong_response() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name);
@@ -402,7 +407,7 @@ fn test_parent_lookup_wrong_response() {
bl.parent_block_processed(
chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block,
$response_type,
&mut cx,
);
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);
assert_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_empty_response() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_empty_response() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name);
@@ -444,7 +449,7 @@ fn test_parent_lookup_empty_response() {
bl.parent_block_processed(
chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block,
$response_type,
&mut cx,
);
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);
assert_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_rpc_failure() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_rpc_failure() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name);
@@ -493,7 +498,7 @@ fn test_parent_lookup_rpc_failure() {
bl.parent_block_processed(
chain_hash,
BlockProcessingResult::Ok(AvailabilityProcessingStatus::Imported(block_root)),
ResponseType::Block,
$response_type,
&mut cx,
);
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);
assert_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_too_many_attempts() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_too_many_attempts() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
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.
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
bl.parent_lookup_response(id, peer_id, None, D, &mut cx);
rig.expect_penalty();
@@ -550,12 +561,12 @@ fn test_parent_lookup_too_many_attempts() {
}
assert_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_too_many_download_attempts_no_blacklist() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_too_many_download_attempts_no_blacklist() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
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!(!bl.failed_chains.contains(&block_hash));
assert!(!bl.failed_chains.contains(&parent.canonical_root()));
}
}
#[test]
fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_too_many_processing_attempts_must_blacklist() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
const PROCESSING_FAILURES: u8 = parent_lookup::PARENT_FAIL_TOLERANCE / 2 + 1;
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(
block_root,
BlockError::InvalidSignature.into(),
ResponseType::Block,
$response_type,
&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_eq!(bl.parent_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_too_deep() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_too_deep() {
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 blocks =
Vec::<Arc<SignedBeaconBlock<E>>>::with_capacity(parent_lookup::PARENT_DEPTH_TOLERANCE);
let mut blocks = Vec::<Arc<SignedBeaconBlock<E>>>::with_capacity(
parent_lookup::PARENT_DEPTH_TOLERANCE,
);
while blocks.len() < parent_lookup::PARENT_DEPTH_TOLERANCE {
let parent = blocks
.last()
@@ -692,18 +704,18 @@ fn test_parent_lookup_too_deep() {
bl.parent_block_processed(
chain_hash,
BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block,
$response_type,
&mut cx,
)
}
rig.expect_penalty();
assert!(bl.failed_chains.contains(&chain_hash));
}
}
#[test]
fn test_parent_lookup_disconnection() {
let fork_name = ForkName::Base;
#[test]
fn test_parent_lookup_disconnection() {
let fork_name = ForkName::$fork_name;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let peer_id = PeerId::random();
let trigger_block = rig.rand_block(fork_name);
@@ -720,12 +732,12 @@ fn test_parent_lookup_disconnection() {
bl.peer_disconnected(&peer_id, &mut cx);
assert!(bl.parent_lookups.is_empty());
}
}
#[test]
fn test_single_block_lookup_ignored_response() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_single_block_lookup_ignored_response() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let block = rig.rand_block(fork_name);
@@ -756,17 +768,17 @@ fn test_single_block_lookup_ignored_response() {
bl.single_block_processed(
id,
BlockProcessingResult::Ignored,
ResponseType::Block,
$response_type,
&mut cx,
);
rig.expect_empty_network();
assert_eq!(bl.single_block_lookups.len(), 0);
}
}
#[test]
fn test_parent_lookup_ignored_response() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
#[test]
fn test_parent_lookup_ignored_response() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(false);
let parent = rig.rand_block(fork_name);
@@ -790,18 +802,18 @@ fn test_parent_lookup_ignored_response() {
bl.parent_block_processed(
chain_hash,
BlockProcessingResult::Ignored,
ResponseType::Block,
$response_type,
&mut cx,
);
rig.expect_empty_network();
assert_eq!(bl.parent_lookups.len(), 0);
}
}
/// This is a regression test.
#[test]
fn test_same_chain_race_condition() {
let fork_name = ForkName::Base;
let response_type = ResponseType::Block;
/// This is a regression test.
#[test]
fn test_same_chain_race_condition() {
let fork_name = ForkName::$fork_name;
let response_type = ResponseType::$response_type;
let (mut bl, mut cx, mut rig) = TestRig::test_setup(true);
#[track_caller]
@@ -862,14 +874,14 @@ fn test_same_chain_race_condition() {
bl.parent_block_processed(
chain_hash,
BlockError::BlockIsAlreadyKnown.into(),
ResponseType::Block,
$response_type,
&mut cx,
)
} else {
bl.parent_block_processed(
chain_hash,
BlockError::ParentUnknown(block.into()).into(),
ResponseType::Block,
$response_type,
&mut cx,
)
}
@@ -898,4 +910,11 @@ fn test_same_chain_race_condition() {
};
bl.parent_chain_processed(chain_hash, process_result, &mut cx);
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);