Skip to content

Commit

Permalink
accounts db/refactor accounts db test - add macro for accounts-db pan…
Browse files Browse the repository at this point in the history
…ic test (solana-labs#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 <[email protected]>
  • Loading branch information
HaoranYi and HaoranYi authored Apr 17, 2024
1 parent 8edc6cc commit 283f433
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
($name:ident, |$accounts_db:ident| $inner: tt) => {
#[test_case(AccountsFileProvider::AppendVec; "append_vec")]
#[test_case(AccountsFileProvider::HotStorage; "hot_storage")]
fn $name(accounts_file_provider: AccountsFileProvider) {
(@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);
AccountsDb::new_single_for_tests_with_provider($accounts_file_provider);
run_test(accounts_db);

};
($name:ident, |$accounts_db:ident| $inner: tt) => {
#[test_case(AccountsFileProvider::AppendVec; "append_vec")]
#[test_case(AccountsFileProvider::HotStorage; "hot_storage")]
fn $name(accounts_file_provider: AccountsFileProvider) {
define_accounts_db_test!(@testfn $name, accounts_file_provider, |$accounts_db| $inner);
}
};
($name:ident, panic = $panic_message:literal, |$accounts_db:ident| $inner: tt) => {
#[test_case(AccountsFileProvider::AppendVec; "append_vec")]
#[test_case(AccountsFileProvider::HotStorage; "hot_storage")]
#[should_panic(expected = $panic_message)]
fn $name(accounts_file_provider: AccountsFileProvider) {
define_accounts_db_test!(@testfn $name, accounts_file_provider, |$accounts_db| $inner);
}
};
}
Expand Down Expand Up @@ -12610,18 +12628,19 @@ pub mod tests {
assert_eq!(1, db.get_snapshot_storages(slot..=slot + 1).0.len());
}

#[test]
#[should_panic(expected = "double remove of account in slot: 0/store: 0!!")]
fn test_storage_remove_account_double_remove() {
let accounts = AccountsDb::new_single_for_tests();
let pubkey = solana_sdk::pubkey::new_rand();
let account = AccountSharedData::new(1, 0, AccountSharedData::default().owner());
accounts.store_for_tests(0, &[(&pubkey, &account)]);
accounts.add_root_and_flush_write_cache(0);
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);
}
define_accounts_db_test!(
test_storage_remove_account_double_remove,
panic = "double remove of account in slot: 0/store: 0!!",
|accounts| {
let pubkey = solana_sdk::pubkey::new_rand();
let account = AccountSharedData::new(1, 0, AccountSharedData::default().owner());
accounts.store_for_tests(0, &[(&pubkey, &account)]);
accounts.add_root_and_flush_write_cache(0);
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);
}
);

fn do_full_clean_refcount(store1_first: bool, store_size: u64) {
let pubkey1 = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
Expand Down

0 comments on commit 283f433

Please sign in to comment.