Add test for find_next_available_key

This commit is contained in:
onyinyang 2023-07-19 10:25:04 -04:00
parent 6191970d9a
commit 471cb8c8c4
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 104 additions and 40 deletions

View File

@ -597,7 +597,6 @@ fn test_redeem_invite() {
#[test]
fn test_clean_up_blocked() {
let mut th = TestHarness::new_buckets(50, 50);
let mut credentials: Vec<cred::Lox> = Vec::new();
// Users
for _ in 0..25 {
let cred = th.open_invite().1 .0;
@ -611,10 +610,10 @@ fn test_clean_up_blocked() {
th.advance_days(28);
let (_, _) = th.level_up(&bob_cred);
let (_, cred3) = th.level_up(&cred2a);
credentials.push(cred3);
}
// Block 25% == 25 bridges
let blocked = block_bridges(&mut th, 25, credentials);
let blocked = 37;
block_bridges(&mut th, blocked);
println!("\n**AFTER 25% BLOCKING EVENT**\n");
assert!(
blocked == th.ba.bridge_table.blocked_keys.len(),
@ -651,12 +650,10 @@ fn test_clean_up_blocked() {
// 150 is the number of open invitation buckets that will be cleared, + the number of blocked bridges
// 40 is the number of keys that will have had to be used after creating 10 new open invitation buckets
println!(
"Size of recyclable keys: {:?}. Which should be the same as 150+blocked-40 = {:?}",
th.ba.bridge_table.recycleable_keys.len(),
150 + blocked - 40
assert!(
th.ba.bridge_table.recycleable_keys.len() == 150 + blocked - 40,
"Size of recyclable keys should be the same as 150+blocked-40"
);
println!("Counter: {:?}", th.ba.bridge_table.counter);
println!("\n**AFTER NEW BUCKETS ADDED**\n");
assert!(
th.ba.bridge_table.blocked_keys.is_empty(),
@ -666,8 +663,11 @@ fn test_clean_up_blocked() {
th.ba.bridge_table.recycleable_keys.len() == 150 + blocked - 40,
"After adding new buckets, recycleable keys should be empty"
);
// Because of open-entry buckets added and open-entry cleanup, the counter increases to 278
println!("Counter: {:?}", th.ba.bridge_table.counter);
// Because of open-entry buckets added and open-entry cleanup, the counter will not have changed
assert!(
th.ba.bridge_table.counter == 250,
"Counter should not have changed due to many recyclable keys",
);
}
#[test]
@ -759,25 +759,110 @@ fn test_clean_up_open_entry() {
}
#[test]
fn find_next_available_key() {}
fn test_find_next_available_key() {
let mut th = TestHarness::new_buckets(50, 50);
let mut credentials: Vec<cred::Lox> = Vec::new();
// Users
for _ in 0..25 {
let cred = th.open_invite().1 .0;
credentials.push(cred);
}
assert!(
th.ba.bridge_table.counter == 250,
"There should be 50*3 openinv buckets + 50 superset buckets +50 spare buckets"
);
assert!(
th.ba.bridge_table.recycleable_keys.len() == 0,
"There should be no recyclable keys"
);
assert!(
th.ba.bridge_table.blocked_keys.len() == 0,
"There should be no blocked keys"
);
assert!(
th.ba.bridge_table.open_inv_keys.len() == 150,
"There should be 150 open inv keys"
);
block_bridges(&mut th, 45);
assert!(
th.ba.bridge_table.counter == 250,
"There should be 50*3 openinv buckets + 50 superset buckets +50 spare buckets"
);
assert!(
th.ba.bridge_table.blocked_keys.len() == 45,
"Blocked keys should be 45"
);
assert!(
th.ba.bridge_table.recycleable_keys.len() == 0,
"There should be no recyclable keys"
);
let bucket = [
BridgeLine::random(),
BridgeLine::random(),
BridgeLine::random(),
];
// Add new bucket to trigger bucket cleanup and find_next_available_key
th.ba.add_spare_bucket(bucket, &mut th.bdb);
// No recyclable keys yet so counter should increase
assert!(
th.ba.bridge_table.counter == 251,
"There should be 50*3 openinv buckets + 50 superset buckets +50 spare buckets"
);
assert!(
th.ba.bridge_table.recycleable_keys.len() == 0,
"There should be no recyclable keys"
);
assert!(
th.ba.bridge_table.blocked_keys.len() == 45,
"There should still be 45 blocked keys"
);
assert!(
th.ba.bridge_table.open_inv_keys.len() == 150,
"There should still be 150 open inv keys"
);
// Advance to all open inv and blocked bridges being expired
th.advance_days(512);
let bucket = [
BridgeLine::random(),
BridgeLine::random(),
BridgeLine::random(),
];
// Add new bridges to trigger bucket cleanup
th.ba.add_spare_bucket(bucket, &mut th.bdb);
// Now all keys should be cleaned up so the counter won't move
assert!(
th.ba.bridge_table.counter == 251,
"There should be 50*3 openinv buckets + 50 superset buckets +50 spare buckets"
);
// This should be equal to the previous keys - 1 for the new spare bucket
assert!(
th.ba.bridge_table.recycleable_keys.len() == 45 + 150 - 1,
"There should be no recyclable keys"
);
assert!(
th.ba.bridge_table.blocked_keys.len() == 0,
"There should be no blocked keys"
);
assert!(
th.ba.bridge_table.open_inv_keys.len() == 0,
"There should be 150 open inv keys"
);
}
/// Blocks a percentage of the bridges for the passed Test Harness
/// excluding the hot spare buckets as they will not have been handed out.
/// The logic assumes hot spare buckets are appended to the end of the bridge_table
/// bucket list.
fn block_bridges(th: &mut TestHarness, percentage: usize, credentials: Vec<cred::Lox>) -> usize {
let blockable_num = th.ba.bridge_table.buckets.len()
- th.ba.bridge_table.spares.len()
- (th.bdb.openinv_buckets.len() / 3);
fn block_bridges(th: &mut TestHarness, to_block: usize) {
let blockable_range = th.ba.bridge_table.buckets.len() - th.ba.bridge_table.spares.len();
let to_block: usize = blockable_num * percentage / 100;
let mut block_index: HashSet<usize> = HashSet::new();
let mut rng = rand::thread_rng();
while block_index.len() < to_block + 1 {
let rand_num = rng.gen_range(0, blockable_range);
if !th.bdb.openinv_buckets.contains(&(rand_num as u32)) {
while block_index.len() < to_block {
let rand_num = rng.gen_range(1, blockable_range);
if !th.bdb.openinv_buckets.contains(&(rand_num as u32)) && !block_index.contains(&rand_num)
{
block_index.insert(rand_num);
}
}
@ -790,27 +875,6 @@ fn block_bridges(th: &mut TestHarness, percentage: usize, credentials: Vec<cred:
}
}
}
for cred in credentials {
let (id, key) = bridge_table::from_scalar(cred.bucket).unwrap();
let encbuckets = th.ba.enc_bridge_table();
let bucket =
bridge_table::BridgeTable::decrypt_bucket(id, &key, &encbuckets.get(&id).unwrap())
.unwrap();
let mut count = 0;
for bridge_line in &bucket.0 {
if th.ba.bridge_table.reachable.contains_key(bridge_line) {
count += 1;
}
}
if count < 2 {
let (_perf_stat, migration) = th.check_blockage(&cred);
let (_block_perf_stat, _) = th.blockage_migration(&cred, &migration);
}
}
to_block
}
#[test]