Skip to content

Commit

Permalink
Merge pull request #367 from flavio/fix-flaky-test
Browse files Browse the repository at this point in the history
test: fix flaky unit test
  • Loading branch information
flavio authored Oct 24, 2023
2 parents 6902f6f + 0ce077d commit 086640e
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/policy_evaluator_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ mod tests {
Ok(())
}

fn find_wapc_policy_id(policy: &Policy) -> Option<u64> {
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
map.iter()
.find(|(_, v)| *v == policy)
.map(|(k, _)| k.to_owned())
}

fn is_wapc_instance_registered(policy_id: u64) -> bool {
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
map.get(&policy_id).is_some()
}

#[test]
fn policy_wapc_mapping_is_cleaned_when_the_evaluator_is_dropped() {
// we need a real WASM module, we don't care about the contents yet
Expand All @@ -396,20 +412,13 @@ mod tests {
.execution_mode(PolicyExecutionMode::KubewardenWapc)
.engine(engine)
.policy_module(module);

let evaluator = builder.build().expect("cannot create evaluator");
{
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
assert_eq!(map.len(), 1);
}
let policy_id =
find_wapc_policy_id(&evaluator.policy).expect("cannot find the wapc we just created");

drop(evaluator);
{
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
assert_eq!(map.len(), 0);
}
assert!(!is_wapc_instance_registered(policy_id));
}

#[test]
Expand Down

0 comments on commit 086640e

Please sign in to comment.