diff --git a/crates/db/Cargo.toml b/crates/db/Cargo.toml index 16d6ef5bf..464051d78 100644 --- a/crates/db/Cargo.toml +++ b/crates/db/Cargo.toml @@ -16,6 +16,10 @@ bytes = "1.2.1" libmdbx = "0.1.8" page_size = "0.4.2" thiserror = "1.0.37" +tempfile = { version = "3.3.0", optional = true } [dev-dependencies] tempfile = "3.3.0" + +[features] +test-utils = ["tempfile"] diff --git a/crates/db/src/kv/mod.rs b/crates/db/src/kv/mod.rs index 25f2e98be..a77244506 100644 --- a/crates/db/src/kv/mod.rs +++ b/crates/db/src/kv/mod.rs @@ -137,37 +137,55 @@ impl Deref for Env { } } +/// Collection of database test utilities +#[cfg(any(test, feature = "test-utils"))] +pub mod test_utils { + use super::{Env, EnvKind, EnvironmentKind, Path}; + + /// Error during database creation + pub const ERROR_DB_CREATION: &str = "Not able to create the mdbx file."; + /// Error during table creation + pub const ERROR_TABLE_CREATION: &str = "Not able to create tables in the database."; + /// Error during tempdir creation + pub const ERROR_TEMPDIR: &str = "Not able to create a temporary directory."; + + /// Create database for testing + pub fn create_test_db(kind: EnvKind) -> Env { + create_test_db_with_path(kind, &tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path()) + } + + /// Create database for testing with specified path + pub fn create_test_db_with_path(kind: EnvKind, path: &Path) -> Env { + let env = Env::::open(path, kind).expect(ERROR_DB_CREATION); + env.create_tables().expect(ERROR_TABLE_CREATION); + env + } +} + #[cfg(test)] mod tests { - use super::{tables::PlainState, Env, EnvKind}; + use super::{tables::PlainState, test_utils, Env, EnvKind}; use libmdbx::{NoWriteMap, WriteMap}; use reth_primitives::Address; use std::str::FromStr; use tempfile::TempDir; const ERROR_DB_CREATION: &str = "Not able to create the mdbx file."; - const ERROR_DB_OPEN: &str = "Not able to open existing mdbx file."; - const ERROR_TABLE_CREATION: &str = "Not able to create tables in the database."; const ERROR_PUT: &str = "Not able to insert value into table."; const ERROR_GET: &str = "Not able to get value from table."; const ERROR_COMMIT: &str = "Not able to commit transaction."; const ERROR_RETURN_VALUE: &str = "Mismatching result."; const ERROR_INIT_TX: &str = "Failed to create a MDBX transaction."; const ERROR_ETH_ADDRESS: &str = "Invalid address."; - const ERROR_TEMPDIR: &str = "Not able to create a temporary directory."; #[test] fn db_creation() { - Env::::open(&TempDir::new().expect(ERROR_TEMPDIR).into_path(), EnvKind::RW) - .expect(ERROR_DB_CREATION); + test_utils::create_test_db::(EnvKind::RW); } #[test] fn db_manual_put_get() { - let env = - Env::::open(&TempDir::new().expect(ERROR_TEMPDIR).into_path(), EnvKind::RW) - .expect(ERROR_DB_CREATION); - env.create_tables().expect(ERROR_TABLE_CREATION); + let env = test_utils::create_test_db::(EnvKind::RW); let value = vec![1, 3, 3, 7]; let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047") @@ -187,15 +205,14 @@ mod tests { #[test] fn db_closure_put_get() { - let path = TempDir::new().expect(ERROR_TEMPDIR).into_path(); + let path = TempDir::new().expect(test_utils::ERROR_TEMPDIR).into_path(); let value = vec![1, 3, 3, 7]; let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047") .expect(ERROR_ETH_ADDRESS); { - let env = Env::::open(&path, EnvKind::RW).expect(ERROR_DB_OPEN); - env.create_tables().expect(ERROR_TABLE_CREATION); + let env = test_utils::create_test_db_with_path::(EnvKind::RW, &path); // PUT let result = env.update(|tx| { diff --git a/crates/stages/Cargo.toml b/crates/stages/Cargo.toml index 0189c5803..23872f5a6 100644 --- a/crates/stages/Cargo.toml +++ b/crates/stages/Cargo.toml @@ -19,4 +19,5 @@ tokio = { version = "1.21.2", features = ["sync"] } [dev-dependencies] tokio = { version = "*", features = ["rt", "sync", "macros"] } tokio-stream = "0.1.10" -tempfile = "3.3.0" \ No newline at end of file +tempfile = "3.3.0" +reth-db = { path = "../db", features = ["test-utils"] } \ No newline at end of file diff --git a/crates/stages/src/pipeline.rs b/crates/stages/src/pipeline.rs index 6345ca84d..c1de58c0d 100644 --- a/crates/stages/src/pipeline.rs +++ b/crates/stages/src/pipeline.rs @@ -356,10 +356,9 @@ mod tests { use super::*; use crate::{StageId, UnwindOutput}; use reth_db::{ - kv::{tx::Tx, EnvKind}, + kv::{test_utils, tx::Tx, EnvKind}, mdbx, }; - use tempfile::TempDir; use tokio::sync::mpsc::channel; use tokio_stream::{wrappers::ReceiverStream, StreamExt}; use utils::TestStage; @@ -368,7 +367,7 @@ mod tests { #[tokio::test] async fn run_pipeline() { let (tx, rx) = channel(2); - let db = utils::test_db().expect("Could not open test database"); + let db = test_utils::create_test_db(EnvKind::RW); // Run pipeline tokio::spawn(async move { @@ -416,7 +415,7 @@ mod tests { #[tokio::test] async fn unwind_pipeline() { let (tx, rx) = channel(2); - let db = utils::test_db().expect("Could not open test database"); + let db = test_utils::create_test_db(EnvKind::RW); // Run pipeline tokio::spawn(async move { @@ -489,7 +488,7 @@ mod tests { #[tokio::test] async fn run_pipeline_with_unwind() { let (tx, rx) = channel(2); - let db = utils::test_db().expect("Could not open test database"); + let db = test_utils::create_test_db(EnvKind::RW); // Run pipeline tokio::spawn(async move { @@ -573,7 +572,7 @@ mod tests { #[tokio::test] async fn unwind_priority() { let (tx, rx) = channel(2); - let db = utils::test_db().expect("Could not open test database"); + let db = test_utils::create_test_db(EnvKind::RW); // Run pipeline tokio::spawn(async move { @@ -655,18 +654,8 @@ mod tests { mod utils { use super::*; use async_trait::async_trait; - use reth_db::kv::KVError; use std::{collections::VecDeque, error::Error}; - pub(crate) fn test_db() -> Result, KVError> { - let path = - TempDir::new().expect("Not able to create a temporary directory.").into_path(); - let db = Env::::open(&path, EnvKind::RW) - .expect("Not able to open existing mdbx file."); - db.create_tables()?; - Ok(db) - } - pub(crate) struct TestStage { id: StageId, exec_outputs: VecDeque>,