solana/accounts-db/src/append_vec/test_utils.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

//! Helpers for AppendVec tests and benches
use {
super::StoredMeta,
rand::{distributions::Alphanumeric, Rng},
solana_sdk::{account::AccountSharedData, pubkey::Pubkey},
std::path::PathBuf,
};
pub struct TempFile {
pub path: PathBuf,
}
impl Drop for TempFile {
fn drop(&mut self) {
let path = std::mem::replace(&mut self.path, PathBuf::new());
let _ignored = std::fs::remove_file(path);
}
}
pub fn get_append_vec_dir() -> String {
std::env::var("FARF_DIR").unwrap_or_else(|_| "farf/append_vec_tests".to_string())
}
pub fn get_append_vec_path(path: &str) -> TempFile {
let out_dir = get_append_vec_dir();
let rand_string: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
Bump rand to 0.8, rand_chacha to 0.3, getrandom to 0.2 (#32871) * sdk: Add concurrent support for rand 0.7 and 0.8 * Update rand, rand_chacha, and getrandom versions * Run command to replace `gen_range` Run `git grep -l gen_range | xargs sed -i'' -e 's/gen_range(\(\S*\), /gen_range(\1../' * sdk: Fix users of older `gen_range` * Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` * perf: Use `Keypair::new()` instead of `generate` * Use older rand version in zk-token-sdk * program-runtime: Inline random key generation * bloom: Fix clippy warnings in tests * streamer: Scope rng usage correctly * perf: Fix clippy warning * accounts-db: Map to char to generate a random string * Remove `from_secret_key_bytes`, it's just `keypair_from_seed` * ledger: Generate keypairs by hand * ed25519-tests: Use new rand * runtime: Use new rand in all tests * gossip: Clean up clippy and inline keypair generators * core: Inline keypair generation for tests * Push sbf lockfile change * sdk: Sort dependencies correctly * Remove `hash::new_with_thread_rng`, use `Hash::new_unique()` * Use Keypair::new where chacha isn't used * sdk: Fix build by marking rand 0.7 optional * Hardcode secret key length, add static assertion * Unify `getrandom` crate usage to fix linking errors * bloom: Fix tests that require a random hash * Remove some dependencies, try to unify others * Remove unnecessary uses of rand and rand_core * Update lockfiles * Add back some dependencies to reduce rebuilds * Increase max rebuilds from 14 to 15 * frozen-abi: Remove `getrandom` * Bump rebuilds to 17 * Remove getrandom from zk-token-proof
2023-08-21 10:11:21 -07:00
.map(char::from)
.take(30)
.collect();
let dir = format!("{out_dir}/{rand_string}");
let mut buf = PathBuf::new();
buf.push(format!("{dir}/{path}"));
std::fs::create_dir_all(dir).expect("Create directory failed");
TempFile { path: buf }
}
pub fn create_test_account(sample: usize) -> (StoredMeta, AccountSharedData) {
let data_len = sample % 256;
let mut account = AccountSharedData::new(sample as u64, 0, &Pubkey::default());
account.set_data_from_slice(&vec![data_len as u8; data_len]);
let stored_meta = StoredMeta {
write_version_obsolete: 0,
pubkey: Pubkey::default(),
data_len: data_len as u64,
};
(stored_meta, account)
}