-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clean up database file after testing #5087
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea3d5d0
clean up database file after testing
robinsdan e566fd7
fix compile
robinsdan d810aa3
fix compile
robinsdan a6ba0c6
fix compile
robinsdan acbf87d
fix
robinsdan bfed459
fix compile
robinsdan 3877e7f
fix compile && rename
robinsdan 8e8d7fd
fix clippy
robinsdan a2e2cf0
Update crates/storage/db/src/lib.rs
mattsse 533c7d2
Update crates/storage/db/src/lib.rs
mattsse d359182
Merge branch 'main' into test
mattsse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,5 +72,5 @@ where | |
tx.inner.commit().unwrap(); | ||
} | ||
|
||
db | ||
db.into_inner_db() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,8 @@ pub fn open_db(path: &Path, log_level: Option<LogLevel>) -> eyre::Result<Databas | |
#[cfg(any(test, feature = "test-utils"))] | ||
pub mod test_utils { | ||
use super::*; | ||
use std::sync::Arc; | ||
use crate::database::{Database, DatabaseGAT}; | ||
use std::{path::PathBuf, sync::Arc}; | ||
|
||
/// Error during database open | ||
pub const ERROR_DB_OPEN: &str = "Not able to open the database file."; | ||
|
@@ -171,26 +172,76 @@ pub mod test_utils { | |
/// Error during tempdir creation | ||
pub const ERROR_TEMPDIR: &str = "Not able to create a temporary directory."; | ||
|
||
/// A database will delete the db dir when dropped. | ||
#[derive(Debug)] | ||
pub struct TestTempDatabase<DB> { | ||
db: Option<DB>, | ||
mattsse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
path: PathBuf, | ||
} | ||
|
||
impl<DB> Drop for TestTempDatabase<DB> { | ||
fn drop(&mut self) { | ||
if let Some(db) = self.db.take() { | ||
drop(db); | ||
let _ = std::fs::remove_dir_all(&self.path); | ||
} | ||
} | ||
} | ||
|
||
impl<DB> TestTempDatabase<DB> { | ||
/// retunrs the ref of inner db | ||
mattsse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn db(&self) -> &DB { | ||
self.db.as_ref().unwrap() | ||
} | ||
|
||
/// retunrs the inner db | ||
mattsse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn into_inner_db(mut self) -> DB { | ||
self.db.take().unwrap() // take out db to avoid clean path in drop fn | ||
} | ||
} | ||
|
||
impl<'a, DB: Database> DatabaseGAT<'a> for TestTempDatabase<DB> { | ||
type TX = <DB as DatabaseGAT<'a>>::TX; | ||
type TXMut = <DB as DatabaseGAT<'a>>::TXMut; | ||
} | ||
|
||
impl<DB: Database> Database for TestTempDatabase<DB> { | ||
fn tx(&self) -> Result<<Self as DatabaseGAT<'_>>::TX, DatabaseError> { | ||
self.db().tx() | ||
} | ||
|
||
fn tx_mut(&self) -> Result<<Self as DatabaseGAT<'_>>::TXMut, DatabaseError> { | ||
self.db().tx_mut() | ||
} | ||
} | ||
|
||
/// Create read/write database for testing | ||
pub fn create_test_rw_db() -> Arc<DatabaseEnv> { | ||
Arc::new( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, since we consumed the tempdir it never got cleaned up on drop |
||
init_db(tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path(), None) | ||
.expect(ERROR_DB_CREATION), | ||
) | ||
pub fn create_test_rw_db() -> Arc<TestTempDatabase<DatabaseEnv>> { | ||
let path = tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path(); | ||
let emsg = format!("{}: {:?}", ERROR_DB_CREATION, path); | ||
|
||
let db = init_db(&path, None).expect(&emsg); | ||
|
||
Arc::new(TestTempDatabase { db: Some(db), path }) | ||
} | ||
|
||
/// Create read/write database for testing | ||
pub fn create_test_rw_db_with_path<P: AsRef<Path>>(path: P) -> Arc<DatabaseEnv> { | ||
Arc::new(init_db(path.as_ref(), None).expect(ERROR_DB_CREATION)) | ||
pub fn create_test_rw_db_with_path<P: AsRef<Path>>( | ||
path: P, | ||
) -> Arc<TestTempDatabase<DatabaseEnv>> { | ||
let path = path.as_ref().to_path_buf(); | ||
let db = init_db(path.as_path(), None).expect(ERROR_DB_CREATION); | ||
Arc::new(TestTempDatabase { db: Some(db), path }) | ||
} | ||
|
||
/// Create read only database for testing | ||
pub fn create_test_ro_db() -> Arc<DatabaseEnvRO> { | ||
pub fn create_test_ro_db() -> Arc<TestTempDatabase<DatabaseEnvRO>> { | ||
let path = tempfile::TempDir::new().expect(ERROR_TEMPDIR).into_path(); | ||
{ | ||
init_db(path.as_path(), None).expect(ERROR_DB_CREATION); | ||
} | ||
Arc::new(open_db_read_only(path.as_path(), None).expect(ERROR_DB_OPEN)) | ||
let db = open_db_read_only(path.as_path(), None).expect(ERROR_DB_OPEN); | ||
Arc::new(TestTempDatabase { db: Some(db), path }) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's rename this To TempDatabase instead