Skip to content

Commit

Permalink
Refactor candidates test in paras_inherent (#2004)
Browse files Browse the repository at this point in the history
Splits the test in multiple cases.
  • Loading branch information
tdimitrov authored Oct 24, 2023
1 parent e39253c commit 0284e21
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions polkadot/runtime/parachains/src/paras_inherent/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,9 +1205,17 @@ mod sanitizers {
}
}

#[test]
fn candidates() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
mod candidates {
use super::*;

// Backed candidates and scheduled parachains used for `sanitize_backed_candidates` testing
struct TestData {
backed_candidates: Vec<BackedCandidate>,
scheduled_paras: BTreeMap<primitives::Id, CoreIndex>,
}

// Generate test data for the candidates test
fn get_test_data() -> TestData {
const RELAY_PARENT_NUM: u32 = 3;

let header = default_header();
Expand All @@ -1233,9 +1241,6 @@ mod sanitizers {
.unwrap();
}

let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };

let scheduled = (0_usize..2)
.into_iter()
.map(|idx| (ParaId::from(1_u32 + idx as u32), CoreIndex::from(idx as u32)))
Expand Down Expand Up @@ -1278,29 +1283,54 @@ mod sanitizers {
})
.collect::<Vec<_>>();

// happy path
assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
),
backed_candidates
);
TestData { backed_candidates, scheduled_paras: scheduled }
}

// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
{
#[test]
fn happy_path() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();

let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };

assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
),
backed_candidates
);

{}
});
}

// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
#[test]
fn nothing_scheduled() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: _ } = get_test_data();
let scheduled = &BTreeMap::new();
let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };

assert!(sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
)
.is_empty());
}
});
}

// candidates that have concluded as invalid are filtered out
#[test]
fn invalid_are_filtered_out() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();

// candidates that have concluded as invalid are filtered out
{
// mark every second one as concluded invalid
let set = {
let mut set = std::collections::HashSet::new();
Expand All @@ -1322,7 +1352,7 @@ mod sanitizers {
.len(),
backed_candidates.len() / 2
);
}
});
});
}
}
}

0 comments on commit 0284e21

Please sign in to comment.