AcctIdx: create test fn get_test() to isolate changes to AcctIdx::get() (#21909)

This commit is contained in:
Jeff Washington (jwash)
2021-12-15 09:09:56 -06:00
committed by GitHub
parent 71b12b1f56
commit 02fa135815
2 changed files with 84 additions and 33 deletions

View File

@ -8685,7 +8685,7 @@ pub mod tests {
let id = {
let (lock, idx) = accounts
.accounts_index
.get(&pubkey, Some(&ancestors), None)
.get_for_tests(&pubkey, Some(&ancestors), None)
.unwrap();
lock.slot_list()[idx].1.store_id()
};
@ -8767,12 +8767,12 @@ pub mod tests {
let ancestors = vec![(0, 1)].into_iter().collect();
let (slot1, account_info1) = accounts
.accounts_index
.get(&pubkey1, Some(&ancestors), None)
.get_for_tests(&pubkey1, Some(&ancestors), None)
.map(|(account_list1, index1)| account_list1.slot_list()[index1])
.unwrap();
let (slot2, account_info2) = accounts
.accounts_index
.get(&pubkey2, Some(&ancestors), None)
.get_for_tests(&pubkey2, Some(&ancestors), None)
.map(|(account_list2, index2)| account_list2.slot_list()[index2])
.unwrap();
assert_eq!(slot1, 0);
@ -8885,7 +8885,10 @@ pub mod tests {
// zero lamport account, should no longer exist in accounts index
// because it has been removed
assert!(accounts.accounts_index.get(&pubkey, None, None).is_none());
assert!(accounts
.accounts_index
.get_for_tests(&pubkey, None, None)
.is_none());
}
#[test]
@ -9073,7 +9076,10 @@ pub mod tests {
// `pubkey1`, a zero lamport account, should no longer exist in accounts index
// because it has been removed by the clean
assert!(accounts.accounts_index.get(&pubkey1, None, None).is_none());
assert!(accounts
.accounts_index
.get_for_tests(&pubkey1, None, None)
.is_none());
// Secondary index should have purged `pubkey1` as well
let mut found_accounts = vec![];
@ -9115,7 +9121,10 @@ pub mod tests {
accounts.clean_accounts(Some(0), false, None);
assert_eq!(accounts.alive_account_count_in_slot(0), 1);
assert_eq!(accounts.alive_account_count_in_slot(1), 1);
assert!(accounts.accounts_index.get(&pubkey, None, None).is_some());
assert!(accounts
.accounts_index
.get_for_tests(&pubkey, None, None)
.is_some());
// Now the account can be cleaned up
accounts.clean_accounts(Some(1), false, None);
@ -9124,7 +9133,10 @@ pub mod tests {
// The zero lamport account, should no longer exist in accounts index
// because it has been removed
assert!(accounts.accounts_index.get(&pubkey, None, None).is_none());
assert!(accounts
.accounts_index
.get_for_tests(&pubkey, None, None)
.is_none());
}
#[test]
@ -9328,12 +9340,12 @@ pub mod tests {
accounts.add_root(current_slot);
let (slot1, account_info1) = accounts
.accounts_index
.get(&pubkey, None, None)
.get_for_tests(&pubkey, None, None)
.map(|(account_list1, index1)| account_list1.slot_list()[index1])
.unwrap();
let (slot2, account_info2) = accounts
.accounts_index
.get(&pubkey2, None, None)
.get_for_tests(&pubkey2, None, None)
.map(|(account_list2, index2)| account_list2.slot_list()[index2])
.unwrap();
assert_eq!(slot1, current_slot);
@ -11061,11 +11073,11 @@ pub mod tests {
accounts_index.add_root(2, false);
accounts_index.add_root(3, false);
let mut purges = HashMap::new();
let (key0_entry, _) = accounts_index.get(&key0, None, None).unwrap();
let (key0_entry, _) = accounts_index.get_for_tests(&key0, None, None).unwrap();
purges.insert(key0, accounts_index.roots_and_ref_count(&key0_entry, None));
let (key1_entry, _) = accounts_index.get(&key1, None, None).unwrap();
let (key1_entry, _) = accounts_index.get_for_tests(&key1, None, None).unwrap();
purges.insert(key1, accounts_index.roots_and_ref_count(&key1_entry, None));
let (key2_entry, _) = accounts_index.get(&key2, None, None).unwrap();
let (key2_entry, _) = accounts_index.get_for_tests(&key2, None, None).unwrap();
purges.insert(key2, accounts_index.roots_and_ref_count(&key2_entry, None));
for (key, (list, ref_count)) in &purges {
info!(" purge {} ref_count {} =>", key, ref_count);

View File

@ -2099,6 +2099,18 @@ pub mod tests {
}
}
impl<T: IndexValue> AccountsIndex<T> {
/// provides the ability to refactor this function on the api without bloody changes
pub fn get_for_tests(
&self,
pubkey: &Pubkey,
ancestors: Option<&Ancestors>,
max_root: Option<Slot>,
) -> AccountIndexGetResult<T> {
self.get(pubkey, ancestors, max_root)
}
}
#[test]
fn test_bitfield_delete_non_excess() {
solana_logger::setup();
@ -2746,8 +2758,9 @@ pub mod tests {
let key = Keypair::new();
let index = AccountsIndex::<bool>::default_for_tests();
let ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
let key = &key.pubkey();
assert!(index.get_for_tests(key, Some(&ancestors), None).is_none());
assert!(index.get_for_tests(key, None, None).is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -2824,8 +2837,10 @@ pub mod tests {
assert!(gc.is_empty());
let ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
assert!(index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.is_none());
assert!(index.get_for_tests(&key.pubkey(), None, None).is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -2863,8 +2878,10 @@ pub mod tests {
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());
let mut ancestors = Ancestors::default();
assert!(index.get(pubkey, Some(&ancestors), None).is_none());
assert!(index.get(pubkey, None, None).is_none());
assert!(index
.get_for_tests(pubkey, Some(&ancestors), None)
.is_none());
assert!(index.get_for_tests(pubkey, None, None).is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -2875,7 +2892,9 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(pubkey, Some(&ancestors), None).is_some());
assert!(index
.get_for_tests(pubkey, Some(&ancestors), None)
.is_some());
assert_eq!(index.ref_count_from_storage(pubkey), 1);
index.unchecked_scan_accounts(
"",
@ -2892,8 +2911,10 @@ pub mod tests {
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());
let mut ancestors = Ancestors::default();
assert!(index.get(pubkey, Some(&ancestors), None).is_none());
assert!(index.get(pubkey, None, None).is_none());
assert!(index
.get_for_tests(pubkey, Some(&ancestors), None)
.is_none());
assert!(index.get_for_tests(pubkey, None, None).is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -2904,7 +2925,9 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(pubkey, Some(&ancestors), None).is_some());
assert!(index
.get_for_tests(pubkey, Some(&ancestors), None)
.is_some());
assert_eq!(index.ref_count_from_storage(pubkey), 0); // cached, so 0
index.unchecked_scan_accounts(
"",
@ -3132,8 +3155,10 @@ pub mod tests {
assert_eq!(1, account_maps_stats_len(&index));
let mut ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
assert!(index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.is_none());
assert!(index.get_for_tests(&key.pubkey(), None, None).is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -3144,7 +3169,9 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_some());
assert!(index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.is_some());
index.unchecked_scan_accounts(
"",
&ancestors,
@ -3172,7 +3199,9 @@ pub mod tests {
assert!(gc.is_empty());
let ancestors = vec![(1, 1)].into_iter().collect();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.is_none());
let mut num = 0;
index.unchecked_scan_accounts(
@ -3202,7 +3231,9 @@ pub mod tests {
assert!(gc.is_empty());
let ancestors = vec![(0, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
let mut num = 0;
@ -3427,7 +3458,7 @@ pub mod tests {
assert!(gc.is_empty());
index.add_root(0, false);
let (list, idx) = index.get(&key.pubkey(), None, None).unwrap();
let (list, idx) = index.get_for_tests(&key.pubkey(), None, None).unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
}
@ -3540,7 +3571,9 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
drop(list);
@ -3556,7 +3589,9 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert_eq!(gc, vec![(0, true)]);
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, false));
}
@ -3589,10 +3624,14 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
let ancestors = vec![(1, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_for_tests(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (1, false));
}
@ -3659,7 +3698,7 @@ pub mod tests {
// Updating index should not purge older roots, only purges
// previous updates within the same slot
assert_eq!(gc, vec![]);
let (list, idx) = index.get(&key.pubkey(), None, None).unwrap();
let (list, idx) = index.get_for_tests(&key.pubkey(), None, None).unwrap();
assert_eq!(list.slot_list()[idx], (3, true));
let mut num = 0;