Skip to content

Commit

Permalink
Added test_helper for db path creation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
varunu28 authored Jan 14, 2024
1 parent d6c28c7 commit 54c1619
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 76 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ protobuf-codegen = "3.2.0"
tokio = { version = "1.26.0", features = ["full"] }
rand = "0.8.5"
async-trait = "0.1.68"
tempfile = "3.7.0"

[build-dependencies]
protobuf = "3.2.0"
protobuf-codegen = "3.2.0"

[dev-dependencies]
tempfile = "3.7.0"
76 changes: 35 additions & 41 deletions src/db/db_test.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
#[cfg(test)]
mod test {

use tempfile::tempdir;

// Helper function to create tempoary directory for new database creation.
// This returns the path for newly created directory.
fn create_temp_dir() -> String {
tempdir()
.map(|temp_dir| temp_dir.path().to_str().unwrap_or_default().to_string())
.unwrap_or_else(|err| {
eprintln!("Failed to create temporary directory: {}", err);
String::new()
})
}

// simple tests that involve writes and reads
#[cfg(test)]
mod single_txn_simple_test {
use super::create_temp_dir;
use std::sync::Arc;

use crate::db::db::{Timestamp, DB};
use crate::{
db::db::{Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
async fn two_writes_with_different_keys() {
Expand All @@ -46,7 +35,6 @@ mod test {

#[cfg(test)]
mod transaction_conflicts {
use super::create_temp_dir;
// A read running into an uncommitted intent with a lower timestamp will wait for the
// earlier transaction

Expand All @@ -61,9 +49,9 @@ mod test {
mod uncommitted_intent_has_lower_timestamp {
use std::sync::Arc;

use crate::db::{
db::{Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -95,9 +83,9 @@ mod test {
mod uncommitted_intent_has_higher_timestamp {
use std::sync::Arc;

use crate::db::{
db::{Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -125,8 +113,10 @@ mod test {

use crate::db::db::{CommitTxnResult, Timestamp, DB};

use crate::db::db_test::test::create_temp_dir;
use crate::hlc::timestamp::Timestamp as HLCTimestamp;
use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::Timestamp as HLCTimestamp,
};

#[tokio::test]
async fn write_waits_for_uncommitted_write() {
Expand Down Expand Up @@ -173,9 +163,9 @@ mod test {
mod run_into_committed_intent {
use std::sync::Arc;

use crate::db::{
db::{CommitTxnResult, Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{CommitTxnResult, Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -228,9 +218,9 @@ mod test {
mod read_write {
use std::sync::Arc;

use crate::db::{
db::{CommitTxnResult, Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{CommitTxnResult, Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -272,9 +262,9 @@ mod test {
mod read_refresh_success {
use std::sync::Arc;

use crate::db::{
db::{CommitTxnResult, Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{CommitTxnResult, Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -314,9 +304,9 @@ mod test {
mod read_refresh_failure {
use std::sync::Arc;

use crate::db::{
db::{CommitTxnResult, Timestamp, DB},
db_test::test::create_temp_dir,
use crate::{
db::db::{CommitTxnResult, Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
Expand Down Expand Up @@ -360,8 +350,10 @@ mod test {

#[cfg(test)]
mod abort_txn {
use super::create_temp_dir;
use crate::db::db::{Timestamp, DB};
use crate::{
db::db::{Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
async fn read_write_after_abort_transaction() {
Expand All @@ -387,11 +379,11 @@ mod test {

#[cfg(test)]
mod deadlock {
use super::create_temp_dir;
use std::sync::Arc;

use crate::{
db::db::{CommitTxnResult, Timestamp, DB},
helpers::test_helpers::create_temp_dir,
storage::str_to_key,
};

Expand Down Expand Up @@ -500,10 +492,12 @@ mod test {

#[cfg(test)]
mod run_txn {
use super::create_temp_dir;
use std::sync::Arc;

use crate::db::db::{Timestamp, DB};
use crate::{
db::db::{Timestamp, DB},
helpers::test_helpers::create_temp_dir,
};

#[tokio::test]
async fn reading_its_txn_own_write() {
Expand Down
1 change: 1 addition & 0 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod test_helpers;
12 changes: 12 additions & 0 deletions src/helpers/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use tempfile::tempdir;

// Helper function to create tempoary directory for new database creation.
// This returns the path for newly created directory.
pub fn create_temp_dir() -> String {
tempdir()
.map(|temp_dir| temp_dir.path().to_str().unwrap_or_default().to_string())
.unwrap_or_else(|err| {
eprintln!("Failed to create temporary directory: {}", err);
String::new()
})
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod concurrency;
pub mod db;
pub mod execute;
pub mod helpers;
pub mod hlc;
mod interval;
mod latch_manager;
Expand Down
4 changes: 3 additions & 1 deletion src/lock_table/lock_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ impl LockTable {
pub fn new_with_defaults() -> Self {
use tokio::sync::mpsc;

use crate::helpers::test_helpers::create_temp_dir;

let (sender, _receiver) = mpsc::channel::<TaskQueueRequest>(1);
LockTable {
locks: RwLock::new(HashMap::new()),
txn_map: Arc::new(RwLock::new(HashMap::new())),
txn_wait_queue: TxnWaitQueue::new(
Arc::new(sender),
Arc::new(KVStore::new_random_path()),
Arc::new(KVStore::new_cleaned(&create_temp_dir())),
),
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/storage/mvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ impl KVStore {
}
}

pub fn new_random_path() -> Self {
let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);

KVStore::new_cleaned(&format!("./tmp/{}", string))
}

pub fn new(path: &str) -> Self {
KVStore {
storage: Storage::new(path),
Expand Down
10 changes: 6 additions & 4 deletions src/storage/mvcc_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<'a> MVCCIterator<'a> {
#[cfg(test)]
mod tests {
use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::{get_intent_timestamp, Timestamp},
storage::{
mvcc_iterator::{IterOptions, MVCCIterator},
Expand All @@ -164,7 +165,7 @@ mod tests {

#[test]
fn test_current_key_and_current_value() {
let storage = Storage::new_random_path();
let storage = Storage::new_cleaned(&create_temp_dir());
let mvcc_key = MVCCKey::new(
str_to_key("hello"),
Timestamp {
Expand All @@ -190,7 +191,7 @@ mod tests {

#[test]
fn test_order_of_iteration_with_intent_timestamp() {
let storage = Storage::new_random_path();
let storage = Storage::new_cleaned(&create_temp_dir());
let key = "hello";

let mvcc_key_2 = MVCCKey::new(
Expand Down Expand Up @@ -253,6 +254,7 @@ mod tests {
#[cfg(test)]
mod test_seek_ge {
use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::Timestamp,
storage::{
mvcc_iterator::{IterOptions, MVCCIterator},
Expand All @@ -264,7 +266,7 @@ mod tests {

#[test]
fn test_multiple_timestamps_with_same_prefix() {
let storage = Storage::new_random_path();
let storage = Storage::new_cleaned(&create_temp_dir());
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down Expand Up @@ -315,7 +317,7 @@ mod tests {

#[test]
fn empty_db() {
let storage = Storage::new_random_path();
let storage = Storage::new_cleaned(&create_temp_dir());
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down
15 changes: 9 additions & 6 deletions src/storage/mvcc_scanner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod tests {
use uuid::Uuid;

use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::Timestamp,
storage::{
mvcc::KVStore,
Expand All @@ -19,7 +20,7 @@ mod tests {

#[test]
fn no_intent_and_no_end_key() {
let storage = Storage::new_random_path();
let storage = Storage::new_cleaned(&create_temp_dir());
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down Expand Up @@ -63,7 +64,7 @@ mod tests {

#[test]
fn intent_found() {
let kv_store = KVStore::new_random_path();
let kv_store = KVStore::new_cleaned(&create_temp_dir());
let timestamp = Timestamp::new(12, 0);
let txn_id = Uuid::new_v4();
let txn = Txn::new_link(txn_id, timestamp);
Expand Down Expand Up @@ -101,6 +102,7 @@ mod tests {
#[cfg(test)]
mod advance_to_next_key {
use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::Timestamp,
storage::{
mvcc::KVStore,
Expand All @@ -113,7 +115,7 @@ mod tests {

#[test]
fn advances_to_next_key() {
let kv_store = KVStore::new_random_path();
let kv_store = KVStore::new_cleaned(&create_temp_dir());
let first_key = "apple";
let first_key_timestamp1 = Timestamp::new(2, 3);
let first_key_timestamp2 = Timestamp::new(3, 0);
Expand Down Expand Up @@ -155,7 +157,7 @@ mod tests {

#[test]
fn there_is_no_next_key() {
let kv_store = KVStore::new_random_path();
let kv_store = KVStore::new_cleaned(&create_temp_dir());
let iterator = MVCCIterator::new(&kv_store.storage, IterOptions { prefix: true });
let scanner_timestamp = Timestamp {
logical_time: 3,
Expand All @@ -178,6 +180,7 @@ mod tests {
use uuid::Uuid;

use crate::{
helpers::test_helpers::create_temp_dir,
hlc::timestamp::Timestamp,
storage::{
mvcc::KVStore,
Expand All @@ -191,7 +194,7 @@ mod tests {

#[test]
fn multiple_timestamps_for_same_keys() {
let kv_store = KVStore::new_random_path();
let kv_store = KVStore::new_cleaned(&create_temp_dir());

let scan_timestamp = Timestamp::new(12, 3);

Expand Down Expand Up @@ -255,7 +258,7 @@ mod tests {

#[test]
fn multiple_intents() {
let kv_store = KVStore::new_random_path();
let kv_store = KVStore::new_cleaned(&create_temp_dir());
let txn_id = Uuid::new_v4();
let transaction_timestamp = Timestamp::new(12, 0);
let txn = Txn::new_link(txn_id, transaction_timestamp);
Expand Down
Loading

0 comments on commit 54c1619

Please sign in to comment.