Skip to content

Commit

Permalink
Update db_test.rs to use tempdir for test data (#14)
Browse files Browse the repository at this point in the history
* Running cargo update

* Update db_test.rs to use tempdir for test data

Add helper function for directory creation
  • Loading branch information
varunu28 authored Jan 11, 2024
1 parent 3715c08 commit d6c28c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ async-trait = "0.1.68"
[build-dependencies]
protobuf = "3.2.0"
protobuf-codegen = "3.2.0"

[dev-dependencies]
tempfile = "3.7.0"
11 changes: 0 additions & 11 deletions src/db/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ impl DB {
}
}

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

DB {
db: Arc::new(InternalDB::new_cleaned(
&format!("./tmp/{}", string),
initial_time,
)),
}
}

pub fn new(path: &str, initial_time: Timestamp) -> Self {
DB {
db: Arc::new(InternalDB::new(path, initial_time)),
Expand Down
77 changes: 56 additions & 21 deletions src/db/db_test.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
#[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};

#[tokio::test]
async fn two_writes_with_different_keys() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let key_a = "A";
let key_b = "B";

Expand All @@ -33,6 +46,7 @@ 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 @@ -47,11 +61,14 @@ mod test {
mod uncommitted_intent_has_lower_timestamp {
use std::sync::Arc;

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

#[tokio::test]
async fn read_waits_for_uncommitted_write() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let write_txn = db.begin_txn().await;
let key = "foo";
db.write(key, 12, write_txn).await;
Expand All @@ -78,11 +95,14 @@ mod test {
mod uncommitted_intent_has_higher_timestamp {
use std::sync::Arc;

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

#[tokio::test]
async fn ignores_intent_with_higher_timestamp() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let read_txn = db.begin_txn().await;
let key = "foo";

Expand All @@ -105,11 +125,12 @@ 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;

#[tokio::test]
async fn write_waits_for_uncommitted_write() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let txn_1 = db.begin_txn().await;
let txn_2 = db.begin_txn().await;
let key = "baz";
Expand Down Expand Up @@ -152,11 +173,14 @@ mod test {
mod run_into_committed_intent {
use std::sync::Arc;

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

#[tokio::test]
async fn bump_write_timestamp_before_committing() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let key = "foo";

// begin txn1
Expand Down Expand Up @@ -204,11 +228,14 @@ mod test {
mod read_write {
use std::sync::Arc;

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

#[tokio::test]
async fn bump_write_timestamp_before_committing() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let key = "foo";

let write_txn = db.begin_txn().await;
Expand Down Expand Up @@ -245,12 +272,14 @@ mod test {
mod read_refresh_success {
use std::sync::Arc;

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

#[tokio::test]
async fn read_refresh_from_write_write_conflict() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let read_key = "foo";
let write_key = "bar";

Expand Down Expand Up @@ -285,11 +314,14 @@ mod test {
mod read_refresh_failure {
use std::sync::Arc;

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

#[tokio::test]
async fn read_refresh_failure() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));

let read_key = "foo";
let write_key = "bar";
Expand Down Expand Up @@ -328,11 +360,12 @@ mod test {

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

#[tokio::test]
async fn read_write_after_abort_transaction() {
let db = DB::new_random_path(Timestamp::new(10));
let db = DB::new_cleaned(&create_temp_dir(), Timestamp::new(10));
let key = "foo";

let txn_to_abort = db.begin_txn().await;
Expand All @@ -354,6 +387,7 @@ mod test {

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

use crate::{
Expand All @@ -363,7 +397,7 @@ mod test {

#[tokio::test]
async fn conflicting_writes() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));
let txn1 = db.begin_txn().await;
let txn2 = db.begin_txn().await;
println!("Txn1 is: {}", txn1);
Expand Down Expand Up @@ -466,13 +500,14 @@ mod test {

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

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

#[tokio::test]
async fn reading_its_txn_own_write() {
let db = DB::new_random_path(Timestamp::new(10));
let db = DB::new_cleaned(&create_temp_dir(), Timestamp::new(10));
db.run_txn(|txn_context| async move {
let key = "foo";
txn_context.write(key, 12).await.unwrap();
Expand All @@ -484,7 +519,7 @@ mod test {

#[tokio::test]
async fn writing_its_own_write() {
let db = DB::new_random_path(Timestamp::new(10));
let db = DB::new_cleaned(&create_temp_dir(), Timestamp::new(10));
db.run_txn(|txn_context| async move {
let key = "foo";
txn_context.write(key, 12).await;
Expand All @@ -497,7 +532,7 @@ mod test {

#[tokio::test]
async fn two_concurrent_writes_to_same_key() {
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let db = Arc::new(DB::new_cleaned(&create_temp_dir(), Timestamp::new(10)));

let db_1 = db.clone();
let key = "foo";
Expand Down

0 comments on commit d6c28c7

Please sign in to comment.