mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: test utils (#48)
This commit is contained in:
@ -16,6 +16,10 @@ bytes = "1.2.1"
|
|||||||
libmdbx = "0.1.8"
|
libmdbx = "0.1.8"
|
||||||
page_size = "0.4.2"
|
page_size = "0.4.2"
|
||||||
thiserror = "1.0.37"
|
thiserror = "1.0.37"
|
||||||
|
tempfile = { version = "3.3.0", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.3.0"
|
tempfile = "3.3.0"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
test-utils = ["tempfile"]
|
||||||
|
|||||||
@ -137,37 +137,55 @@ impl<E: EnvironmentKind> Deref for Env<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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<E: EnvironmentKind>(kind: EnvKind) -> Env<E> {
|
||||||
|
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<E: EnvironmentKind>(kind: EnvKind, path: &Path) -> Env<E> {
|
||||||
|
let env = Env::<E>::open(path, kind).expect(ERROR_DB_CREATION);
|
||||||
|
env.create_tables().expect(ERROR_TABLE_CREATION);
|
||||||
|
env
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{tables::PlainState, Env, EnvKind};
|
use super::{tables::PlainState, test_utils, Env, EnvKind};
|
||||||
use libmdbx::{NoWriteMap, WriteMap};
|
use libmdbx::{NoWriteMap, WriteMap};
|
||||||
use reth_primitives::Address;
|
use reth_primitives::Address;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
const ERROR_DB_CREATION: &str = "Not able to create the mdbx file.";
|
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_PUT: &str = "Not able to insert value into table.";
|
||||||
const ERROR_GET: &str = "Not able to get value from table.";
|
const ERROR_GET: &str = "Not able to get value from table.";
|
||||||
const ERROR_COMMIT: &str = "Not able to commit transaction.";
|
const ERROR_COMMIT: &str = "Not able to commit transaction.";
|
||||||
const ERROR_RETURN_VALUE: &str = "Mismatching result.";
|
const ERROR_RETURN_VALUE: &str = "Mismatching result.";
|
||||||
const ERROR_INIT_TX: &str = "Failed to create a MDBX transaction.";
|
const ERROR_INIT_TX: &str = "Failed to create a MDBX transaction.";
|
||||||
const ERROR_ETH_ADDRESS: &str = "Invalid address.";
|
const ERROR_ETH_ADDRESS: &str = "Invalid address.";
|
||||||
const ERROR_TEMPDIR: &str = "Not able to create a temporary directory.";
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn db_creation() {
|
fn db_creation() {
|
||||||
Env::<NoWriteMap>::open(&TempDir::new().expect(ERROR_TEMPDIR).into_path(), EnvKind::RW)
|
test_utils::create_test_db::<NoWriteMap>(EnvKind::RW);
|
||||||
.expect(ERROR_DB_CREATION);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn db_manual_put_get() {
|
fn db_manual_put_get() {
|
||||||
let env =
|
let env = test_utils::create_test_db::<NoWriteMap>(EnvKind::RW);
|
||||||
Env::<NoWriteMap>::open(&TempDir::new().expect(ERROR_TEMPDIR).into_path(), EnvKind::RW)
|
|
||||||
.expect(ERROR_DB_CREATION);
|
|
||||||
env.create_tables().expect(ERROR_TABLE_CREATION);
|
|
||||||
|
|
||||||
let value = vec![1, 3, 3, 7];
|
let value = vec![1, 3, 3, 7];
|
||||||
let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
|
let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
|
||||||
@ -187,15 +205,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn db_closure_put_get() {
|
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 value = vec![1, 3, 3, 7];
|
||||||
let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
|
let key = Address::from_str("0xa2c122be93b0074270ebee7f6b7292c7deb45047")
|
||||||
.expect(ERROR_ETH_ADDRESS);
|
.expect(ERROR_ETH_ADDRESS);
|
||||||
|
|
||||||
{
|
{
|
||||||
let env = Env::<WriteMap>::open(&path, EnvKind::RW).expect(ERROR_DB_OPEN);
|
let env = test_utils::create_test_db_with_path::<WriteMap>(EnvKind::RW, &path);
|
||||||
env.create_tables().expect(ERROR_TABLE_CREATION);
|
|
||||||
|
|
||||||
// PUT
|
// PUT
|
||||||
let result = env.update(|tx| {
|
let result = env.update(|tx| {
|
||||||
|
|||||||
@ -19,4 +19,5 @@ tokio = { version = "1.21.2", features = ["sync"] }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "*", features = ["rt", "sync", "macros"] }
|
tokio = { version = "*", features = ["rt", "sync", "macros"] }
|
||||||
tokio-stream = "0.1.10"
|
tokio-stream = "0.1.10"
|
||||||
tempfile = "3.3.0"
|
tempfile = "3.3.0"
|
||||||
|
reth-db = { path = "../db", features = ["test-utils"] }
|
||||||
@ -356,10 +356,9 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::{StageId, UnwindOutput};
|
use crate::{StageId, UnwindOutput};
|
||||||
use reth_db::{
|
use reth_db::{
|
||||||
kv::{tx::Tx, EnvKind},
|
kv::{test_utils, tx::Tx, EnvKind},
|
||||||
mdbx,
|
mdbx,
|
||||||
};
|
};
|
||||||
use tempfile::TempDir;
|
|
||||||
use tokio::sync::mpsc::channel;
|
use tokio::sync::mpsc::channel;
|
||||||
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
|
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
|
||||||
use utils::TestStage;
|
use utils::TestStage;
|
||||||
@ -368,7 +367,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn run_pipeline() {
|
async fn run_pipeline() {
|
||||||
let (tx, rx) = channel(2);
|
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
|
// Run pipeline
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -416,7 +415,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn unwind_pipeline() {
|
async fn unwind_pipeline() {
|
||||||
let (tx, rx) = channel(2);
|
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
|
// Run pipeline
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -489,7 +488,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn run_pipeline_with_unwind() {
|
async fn run_pipeline_with_unwind() {
|
||||||
let (tx, rx) = channel(2);
|
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
|
// Run pipeline
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -573,7 +572,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn unwind_priority() {
|
async fn unwind_priority() {
|
||||||
let (tx, rx) = channel(2);
|
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
|
// Run pipeline
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -655,18 +654,8 @@ mod tests {
|
|||||||
mod utils {
|
mod utils {
|
||||||
use super::*;
|
use super::*;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use reth_db::kv::KVError;
|
|
||||||
use std::{collections::VecDeque, error::Error};
|
use std::{collections::VecDeque, error::Error};
|
||||||
|
|
||||||
pub(crate) fn test_db() -> Result<Env<mdbx::WriteMap>, KVError> {
|
|
||||||
let path =
|
|
||||||
TempDir::new().expect("Not able to create a temporary directory.").into_path();
|
|
||||||
let db = Env::<mdbx::WriteMap>::open(&path, EnvKind::RW)
|
|
||||||
.expect("Not able to open existing mdbx file.");
|
|
||||||
db.create_tables()?;
|
|
||||||
Ok(db)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct TestStage {
|
pub(crate) struct TestStage {
|
||||||
id: StageId,
|
id: StageId,
|
||||||
exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
|
exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
|
||||||
|
|||||||
Reference in New Issue
Block a user