Skip to content

Commit 9127563

Browse files
CPerezzmattsseampcode-com
authored
fix: cleanup entire temp directory when using testing_node (#18399)
Co-authored-by: Matthias Seitz <[email protected]> Co-authored-by: Amp <[email protected]>
1 parent a500fb2 commit 9127563

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

crates/node/builder/src/builder/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
251251
}
252252

253253
/// Creates a preconfigured node for testing purposes with a specific datadir.
254+
///
255+
/// The entire `datadir` will be cleaned up when the node is dropped.
254256
#[cfg(feature = "test-utils")]
255257
pub fn testing_node_with_datadir(
256258
mut self,
@@ -268,7 +270,7 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
268270
let data_dir =
269271
path.unwrap_or_chain_default(self.config.chain.chain(), self.config.datadir.clone());
270272

271-
let db = reth_db::test_utils::create_test_rw_db_with_path(data_dir.db());
273+
let db = reth_db::test_utils::create_test_rw_db_with_datadir(data_dir.data_dir());
272274

273275
WithLaunchContext { builder: self.with_database(db), task_executor }
274276
}

crates/storage/db/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ pub mod test_utils {
203203
Arc::new(TempDatabase::new(db, path))
204204
}
205205

206+
/// Create read/write database for testing within a data directory.
207+
///
208+
/// The database is created at `datadir/db`, and `TempDatabase` will clean up the entire
209+
/// `datadir` on drop.
210+
#[track_caller]
211+
pub fn create_test_rw_db_with_datadir<P: AsRef<Path>>(
212+
datadir: P,
213+
) -> Arc<TempDatabase<DatabaseEnv>> {
214+
let datadir = datadir.as_ref().to_path_buf();
215+
let db_path = datadir.join("db");
216+
let emsg = format!("{ERROR_DB_CREATION}: {db_path:?}");
217+
let db = init_db(
218+
&db_path,
219+
DatabaseArguments::new(ClientVersion::default())
220+
.with_max_read_transaction_duration(Some(MaxReadTransactionDuration::Unbounded)),
221+
)
222+
.expect(&emsg);
223+
Arc::new(TempDatabase::new(db, datadir))
224+
}
225+
206226
/// Create read only database for testing
207227
#[track_caller]
208228
pub fn create_test_ro_db() -> Arc<TempDatabase<DatabaseEnv>> {
@@ -235,6 +255,24 @@ mod tests {
235255
use std::time::Duration;
236256
use tempfile::tempdir;
237257

258+
#[test]
259+
fn test_temp_database_cleanup() {
260+
// Test that TempDatabase properly cleans up its directory when dropped
261+
let temp_path = {
262+
let db = crate::test_utils::create_test_rw_db();
263+
let path = db.path().to_path_buf();
264+
assert!(path.exists(), "Database directory should exist while TempDatabase is alive");
265+
path
266+
// TempDatabase dropped here
267+
};
268+
269+
// Verify the directory was cleaned up
270+
assert!(
271+
!temp_path.exists(),
272+
"Database directory should be cleaned up after TempDatabase is dropped"
273+
);
274+
}
275+
238276
#[test]
239277
fn db_version() {
240278
let path = tempdir().unwrap();

0 commit comments

Comments
 (0)