mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: use tempdir when testing static files (#6962)
This commit is contained in:
@ -2150,12 +2150,10 @@ mod tests {
|
||||
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1].into_iter(),
|
||||
);
|
||||
@ -2208,12 +2206,11 @@ mod tests {
|
||||
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1].into_iter(),
|
||||
);
|
||||
@ -2230,14 +2227,11 @@ mod tests {
|
||||
// if we `await` in the assert, the forkchoice will poll after we've inserted the block,
|
||||
// and it will return VALID instead of SYNCING
|
||||
let invalid_rx = env.send_forkchoice_updated(next_forkchoice_state).await;
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
// Insert next head immediately after sending forkchoice update
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&next_head].into_iter(),
|
||||
);
|
||||
@ -2274,12 +2268,11 @@ mod tests {
|
||||
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1].into_iter(),
|
||||
);
|
||||
@ -2329,12 +2322,9 @@ mod tests {
|
||||
let mut block3 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
block3.header.set_difficulty(U256::from(1));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1, &block2, &block3].into_iter(),
|
||||
);
|
||||
@ -2377,12 +2367,10 @@ mod tests {
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
|
||||
let (_temp_dir, temp_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), temp_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1].into_iter(),
|
||||
);
|
||||
@ -2481,12 +2469,10 @@ mod tests {
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0));
|
||||
let block2 = random_block(&mut rng, 2, Some(block1.hash()), None, Some(0));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1, &block2].into_iter(),
|
||||
);
|
||||
@ -2552,12 +2538,10 @@ mod tests {
|
||||
// TODO: add transactions that transfer from the alloc accounts, generating the new
|
||||
// block tx and state root
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis, &block1].into_iter(),
|
||||
);
|
||||
@ -2598,12 +2582,10 @@ mod tests {
|
||||
|
||||
let genesis = random_block(&mut rng, 0, None, None, Some(0));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&genesis].into_iter(),
|
||||
);
|
||||
@ -2664,12 +2646,10 @@ mod tests {
|
||||
.with_executor_results(Vec::from([exec_result2]))
|
||||
.build();
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
insert_blocks(
|
||||
ProviderFactory::new(
|
||||
env.db.as_ref(),
|
||||
chain_spec.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
ProviderFactory::new(env.db.as_ref(), chain_spec.clone(), static_dir_path)
|
||||
.expect("create provider factory with static_files"),
|
||||
[&data.genesis, &block1].into_iter(),
|
||||
);
|
||||
|
||||
@ -615,10 +615,12 @@ mod tests {
|
||||
let client = Arc::new(
|
||||
TestBodiesClient::default().with_bodies(bodies.clone()).with_should_delay(true),
|
||||
);
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
let mut downloader = BodiesDownloaderBuilder::default().build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
downloader.set_download_range(0..=19).expect("failed to set download range");
|
||||
|
||||
@ -653,11 +655,13 @@ mod tests {
|
||||
|
||||
let request_limit = 10;
|
||||
let client = Arc::new(TestBodiesClient::default().with_bodies(bodies.clone()));
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
let mut downloader =
|
||||
BodiesDownloaderBuilder::default().with_request_limit(request_limit).build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
downloader.set_download_range(0..=199).expect("failed to set download range");
|
||||
|
||||
@ -680,13 +684,14 @@ mod tests {
|
||||
let client = Arc::new(
|
||||
TestBodiesClient::default().with_bodies(bodies.clone()).with_should_delay(true),
|
||||
);
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
let mut downloader = BodiesDownloaderBuilder::default()
|
||||
.with_stream_batch_size(stream_batch_size)
|
||||
.with_request_limit(request_limit)
|
||||
.build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
|
||||
let mut range_start = 0;
|
||||
@ -713,10 +718,12 @@ mod tests {
|
||||
insert_headers(db.db(), &headers);
|
||||
|
||||
let client = Arc::new(TestBodiesClient::default().with_bodies(bodies.clone()));
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
let mut downloader = BodiesDownloaderBuilder::default().with_stream_batch_size(100).build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
|
||||
// Set and download the first range
|
||||
@ -747,6 +754,8 @@ mod tests {
|
||||
insert_headers(db.db(), &headers);
|
||||
|
||||
let client = Arc::new(TestBodiesClient::default().with_bodies(bodies.clone()));
|
||||
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
// Set the max buffered block size to 1 byte, to make sure that every response exceeds the
|
||||
// limit
|
||||
let mut downloader = BodiesDownloaderBuilder::default()
|
||||
@ -756,7 +765,7 @@ mod tests {
|
||||
.build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
|
||||
// Set and download the entire range
|
||||
@ -781,13 +790,15 @@ mod tests {
|
||||
let client = Arc::new(
|
||||
TestBodiesClient::default().with_bodies(bodies.clone()).with_empty_responses(2),
|
||||
);
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
let mut downloader = BodiesDownloaderBuilder::default()
|
||||
.with_request_limit(3)
|
||||
.with_stream_batch_size(100)
|
||||
.build(
|
||||
client.clone(),
|
||||
Arc::new(TestConsensus::default()),
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir()).unwrap(),
|
||||
ProviderFactory::new(db, MAINNET.clone(), static_dir_path).unwrap(),
|
||||
);
|
||||
|
||||
// Download the requested range
|
||||
|
||||
@ -205,11 +205,12 @@ mod tests {
|
||||
#[allow(clippy::reversed_empty_ranges)]
|
||||
async fn set_download_range_error_returned() {
|
||||
reth_tracing::init_test_tracing();
|
||||
let factory = create_test_provider_factory();
|
||||
|
||||
let downloader = BodiesDownloaderBuilder::default().build(
|
||||
Arc::new(TestBodiesClient::default()),
|
||||
Arc::new(TestConsensus::default()),
|
||||
create_test_provider_factory(),
|
||||
factory,
|
||||
);
|
||||
let mut downloader = TaskDownloader::spawn(downloader);
|
||||
|
||||
|
||||
@ -273,8 +273,8 @@ mod tests {
|
||||
#[test]
|
||||
fn is_pruning_needed() {
|
||||
let db = create_test_rw_db();
|
||||
let provider_factory =
|
||||
ProviderFactory::new(db, MAINNET.clone(), create_test_static_files_dir())
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
let provider_factory = ProviderFactory::new(db, MAINNET.clone(), static_dir_path)
|
||||
.expect("create provide factory with static_files");
|
||||
let mut pruner = Pruner::new(provider_factory, vec![], 5, 0, 5);
|
||||
|
||||
|
||||
@ -21,22 +21,22 @@ use reth_provider::{
|
||||
HistoryWriter, ProviderError, ProviderFactory,
|
||||
};
|
||||
use std::{collections::BTreeMap, path::Path, sync::Arc};
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// Test database that is used for testing stage implementations.
|
||||
#[derive(Debug)]
|
||||
pub struct TestStageDB {
|
||||
pub factory: ProviderFactory<Arc<TempDatabase<DatabaseEnv>>>,
|
||||
pub temp_static_files_dir: TempDir,
|
||||
}
|
||||
|
||||
impl Default for TestStageDB {
|
||||
/// Create a new instance of [TestStageDB]
|
||||
fn default() -> Self {
|
||||
let (static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
Self {
|
||||
factory: ProviderFactory::new(
|
||||
create_test_rw_db(),
|
||||
MAINNET.clone(),
|
||||
create_test_static_files_dir(),
|
||||
)
|
||||
temp_static_files_dir: static_dir,
|
||||
factory: ProviderFactory::new(create_test_rw_db(), MAINNET.clone(), static_dir_path)
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
@ -44,11 +44,14 @@ impl Default for TestStageDB {
|
||||
|
||||
impl TestStageDB {
|
||||
pub fn new(path: &Path) -> Self {
|
||||
let (static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
|
||||
Self {
|
||||
temp_static_files_dir: static_dir,
|
||||
factory: ProviderFactory::new(
|
||||
create_test_rw_db_with_path(path),
|
||||
MAINNET.clone(),
|
||||
create_test_static_files_dir(),
|
||||
static_dir_path,
|
||||
)
|
||||
.unwrap(),
|
||||
}
|
||||
|
||||
@ -257,8 +257,9 @@ mod tests {
|
||||
sync::{mpsc::channel, Arc},
|
||||
time::Duration,
|
||||
};
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn setup() -> (ProviderFactory<Arc<TempDatabase<DatabaseEnv>>>, StaticFileProvider) {
|
||||
fn setup() -> (ProviderFactory<Arc<TempDatabase<DatabaseEnv>>>, StaticFileProvider, TempDir) {
|
||||
let mut rng = generators::rng();
|
||||
let db = TestStageDB::default();
|
||||
|
||||
@ -290,12 +291,12 @@ mod tests {
|
||||
|
||||
let provider_factory = db.factory;
|
||||
let static_file_provider = provider_factory.static_file_provider();
|
||||
(provider_factory, static_file_provider)
|
||||
(provider_factory, static_file_provider, db.temp_static_files_dir)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run() {
|
||||
let (provider_factory, static_file_provider) = setup();
|
||||
let (provider_factory, static_file_provider, _temp_static_files_dir) = setup();
|
||||
|
||||
let mut static_file_producer = StaticFileProducerInner::new(
|
||||
provider_factory,
|
||||
@ -373,7 +374,7 @@ mod tests {
|
||||
/// Tests that a cloneable [`StaticFileProducer`] type is not susceptible to any race condition.
|
||||
#[test]
|
||||
fn only_one() {
|
||||
let (provider_factory, static_file_provider) = setup();
|
||||
let (provider_factory, static_file_provider, _temp_static_files_dir) = setup();
|
||||
|
||||
let static_file_producer = StaticFileProducer::new(
|
||||
provider_factory,
|
||||
|
||||
@ -163,6 +163,7 @@ pub mod test_utils {
|
||||
use reth_libmdbx::MaxReadTransactionDuration;
|
||||
use reth_primitives::fs;
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// Error during database open
|
||||
pub const ERROR_DB_OPEN: &str = "Not able to open the database file.";
|
||||
@ -233,12 +234,10 @@ pub mod test_utils {
|
||||
}
|
||||
|
||||
/// Create static_files path for testing
|
||||
pub fn create_test_static_files_dir() -> PathBuf {
|
||||
let path = tempdir_path();
|
||||
let emsg = format!("{}: {:?}", ERROR_STATIC_FILES_CREATION, path);
|
||||
|
||||
reth_primitives::fs::create_dir_all(path.clone()).expect(&emsg);
|
||||
path
|
||||
pub fn create_test_static_files_dir() -> (TempDir, PathBuf) {
|
||||
let temp_dir = TempDir::with_prefix("reth-test-static-").expect(ERROR_TEMPDIR);
|
||||
let path = temp_dir.path().to_path_buf();
|
||||
(temp_dir, path)
|
||||
}
|
||||
|
||||
/// Get a temporary directory path to use for the database
|
||||
|
||||
@ -661,11 +661,12 @@ mod tests {
|
||||
#[test]
|
||||
fn provider_factory_with_database_path() {
|
||||
let chain_spec = ChainSpecBuilder::mainnet().build();
|
||||
let (_static_dir, static_dir_path) = create_test_static_files_dir();
|
||||
let factory = ProviderFactory::new_with_database_path(
|
||||
tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path(),
|
||||
Arc::new(chain_spec),
|
||||
DatabaseArguments::new(Default::default()),
|
||||
create_test_static_files_dir(),
|
||||
static_dir_path,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
||||
@ -26,7 +26,8 @@ pub fn create_test_provider_factory() -> ProviderFactory<Arc<TempDatabase<Databa
|
||||
pub fn create_test_provider_factory_with_chain_spec(
|
||||
chain_spec: Arc<ChainSpec>,
|
||||
) -> ProviderFactory<Arc<TempDatabase<DatabaseEnv>>> {
|
||||
let (static_dir, _) = create_test_static_files_dir();
|
||||
let db = create_test_rw_db();
|
||||
ProviderFactory::new(db, chain_spec, create_test_static_files_dir())
|
||||
ProviderFactory::new(db, chain_spec, static_dir.into_path())
|
||||
.expect("create provider factory with static_files")
|
||||
}
|
||||
|
||||
@ -83,11 +83,11 @@ impl Case for BlockchainTestCase {
|
||||
.try_for_each(|case| {
|
||||
// Create a new test database and initialize a provider for the test case.
|
||||
let db = create_test_rw_db();
|
||||
let static_files_dir = create_test_static_files_dir();
|
||||
let (_static_files_dir, static_files_dir_path) = create_test_static_files_dir();
|
||||
let provider = ProviderFactory::new(
|
||||
db.as_ref(),
|
||||
Arc::new(case.network.clone().into()),
|
||||
static_files_dir.clone(),
|
||||
static_files_dir_path,
|
||||
)?
|
||||
.provider_rw()
|
||||
.unwrap();
|
||||
@ -169,11 +169,6 @@ impl Case for BlockchainTestCase {
|
||||
|
||||
// Drop the provider without committing to the database.
|
||||
drop(provider);
|
||||
// TODO: replace with `tempdir` usage, so the temp directory is removed
|
||||
// automatically when the variable goes out of scope
|
||||
reth_primitives::fs::remove_dir_all(static_files_dir)
|
||||
.expect("Failed to remove static files directory");
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user