accounts db/refactor accounts db test - add macro for accounts-db panic test (#786)

* add macro for accounts-db panic test
convert double remove test for both account file provider

* refactor to share accounts_db_test and accounts_db_panic_test macro

* rebase

---------

Co-authored-by: HaoranYi <haoran.yi@solana.com>
This commit is contained in:
HaoranYi 2024-04-17 12:52:00 -05:00 committed by GitHub
parent 8edc6ccb5c
commit 283f433a3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 19 deletions

View File

@ -9632,18 +9632,36 @@ pub mod tests {
} }
} }
/// Helper macro to define accounts_db_test for both `AppendVec` and `HotStorage`.
/// This macro supports creating both regular tests and tests that should panic.
/// Usage:
/// For regular test, use the following syntax.
/// define_accounts_db_test!(TEST_NAME, |accounts_db| { TEST_BODY }); // regular test
/// For test that should panic, use the following syntax.
/// define_accounts_db_test!(TEST_NAME, panic = "PANIC_MSG", |accounts_db| { TEST_BODY });
macro_rules! define_accounts_db_test { macro_rules! define_accounts_db_test {
(@testfn $name:ident, $accounts_file_provider: ident, |$accounts_db:ident| $inner: tt) => {
fn run_test($accounts_db: AccountsDb) {
$inner
}
let accounts_db =
AccountsDb::new_single_for_tests_with_provider($accounts_file_provider);
run_test(accounts_db);
};
($name:ident, |$accounts_db:ident| $inner: tt) => { ($name:ident, |$accounts_db:ident| $inner: tt) => {
#[test_case(AccountsFileProvider::AppendVec; "append_vec")] #[test_case(AccountsFileProvider::AppendVec; "append_vec")]
#[test_case(AccountsFileProvider::HotStorage; "hot_storage")] #[test_case(AccountsFileProvider::HotStorage; "hot_storage")]
fn $name(accounts_file_provider: AccountsFileProvider) { fn $name(accounts_file_provider: AccountsFileProvider) {
fn run_test($accounts_db: AccountsDb) { define_accounts_db_test!(@testfn $name, accounts_file_provider, |$accounts_db| $inner);
$inner }
} };
($name:ident, panic = $panic_message:literal, |$accounts_db:ident| $inner: tt) => {
let accounts_db = #[test_case(AccountsFileProvider::AppendVec; "append_vec")]
AccountsDb::new_single_for_tests_with_provider(accounts_file_provider); #[test_case(AccountsFileProvider::HotStorage; "hot_storage")]
run_test(accounts_db); #[should_panic(expected = $panic_message)]
fn $name(accounts_file_provider: AccountsFileProvider) {
define_accounts_db_test!(@testfn $name, accounts_file_provider, |$accounts_db| $inner);
} }
}; };
} }
@ -12610,18 +12628,19 @@ pub mod tests {
assert_eq!(1, db.get_snapshot_storages(slot..=slot + 1).0.len()); assert_eq!(1, db.get_snapshot_storages(slot..=slot + 1).0.len());
} }
#[test] define_accounts_db_test!(
#[should_panic(expected = "double remove of account in slot: 0/store: 0!!")] test_storage_remove_account_double_remove,
fn test_storage_remove_account_double_remove() { panic = "double remove of account in slot: 0/store: 0!!",
let accounts = AccountsDb::new_single_for_tests(); |accounts| {
let pubkey = solana_sdk::pubkey::new_rand(); let pubkey = solana_sdk::pubkey::new_rand();
let account = AccountSharedData::new(1, 0, AccountSharedData::default().owner()); let account = AccountSharedData::new(1, 0, AccountSharedData::default().owner());
accounts.store_for_tests(0, &[(&pubkey, &account)]); accounts.store_for_tests(0, &[(&pubkey, &account)]);
accounts.add_root_and_flush_write_cache(0); accounts.add_root_and_flush_write_cache(0);
let storage_entry = accounts.storage.get_slot_storage_entry(0).unwrap(); let storage_entry = accounts.storage.get_slot_storage_entry(0).unwrap();
storage_entry.remove_accounts(0, true, 1); storage_entry.remove_accounts(0, true, 1);
storage_entry.remove_accounts(0, true, 1); storage_entry.remove_accounts(0, true, 1);
} }
);
fn do_full_clean_refcount(store1_first: bool, store_size: u64) { fn do_full_clean_refcount(store1_first: bool, store_size: u64) {
let pubkey1 = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap(); let pubkey1 = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();