From 16fd2b55e8559f759c398408dbd9af28662fd161 Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Thu, 25 Mar 2021 09:24:57 -0600 Subject: [PATCH 1/2] add some tests --- patch-db/Cargo.toml | 2 ++ patch-db/src/test.rs | 58 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/patch-db/Cargo.toml b/patch-db/Cargo.toml index eb69ba3..b8a480f 100644 --- a/patch-db/Cargo.toml +++ b/patch-db/Cargo.toml @@ -21,4 +21,6 @@ thiserror = "1.0.23" tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "io-util", "macros"] } [dev-dependencies] +proptest = "1.0.0" serde = { version = "1.0.118", features = ["rc", "derive"] } +tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "rt-multi-thread", "io-util", "macros"] } diff --git a/patch-db/src/test.rs b/patch-db/src/test.rs index 6261bbc..fc967cb 100644 --- a/patch-db/src/test.rs +++ b/patch-db/src/test.rs @@ -1,7 +1,8 @@ use super::*; +use proptest::prelude::*; +use tokio::runtime::Builder; -#[tokio::test] -async fn basic() { +async fn init_test_env() -> PatchDb { let db = PatchDb::open("test.db").await.unwrap(); db.put( &JsonPointer::<&'static str>::default(), @@ -9,18 +10,63 @@ async fn basic() { a: "test1".to_string(), b: Child { a: "test2".to_string(), - b: 4, + b: 1, }, }, ) - .await - .unwrap(); + .await.unwrap(); + db +} + +async fn put_string_into_root(db: PatchDb, s: String) -> Arc { + println!("trying string: {}", s); + db.put( + &JsonPointer::<&'static str>::default(), + &s + ) + .await.unwrap() +} + + +#[tokio::test] +async fn basic() { + let db = init_test_env().await; let ptr: JsonPointer = "/b/b".parse().unwrap(); + let mut get_res: Value = db.get(&ptr).await.unwrap(); + assert_eq!(get_res, 1); db.put(&ptr, &"hello").await.unwrap(); - let get_res: Value = db.get(&ptr).await.unwrap(); + get_res = db.get(&ptr).await.unwrap(); assert_eq!(get_res, "hello"); } +#[tokio::test] +async fn transaction() { + let db = init_test_env().await; + let mut tx = db.begin(); + let ptr: JsonPointer = "/b/b".parse().unwrap(); + tx.put(&ptr, &(2 as usize)).await.unwrap(); + tx.put(&ptr, &(1 as usize)).await.unwrap(); + let _res = tx.commit().await.unwrap(); + println!("res = {:?}", _res); + +} + +proptest! { + #[test] + fn doesnt_crash(s in "\\PC*") { + // build runtime + let runtime = Builder::new_multi_thread() + .worker_threads(4) + .thread_name("test-doesnt-crash") + .thread_stack_size(3 * 1024 * 1024) + .build() + .unwrap(); + let db = runtime.block_on(init_test_env()); + let put_future = put_string_into_root(db, s); + runtime.block_on(put_future); + } +} + #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct Sample { a: String, From 2ea757027e90ecfb66eff071a15580dec14a0c0c Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Fri, 26 Mar 2021 09:12:39 -0600 Subject: [PATCH 2/2] add a proptest --- patch-db/src/test.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/patch-db/src/test.rs b/patch-db/src/test.rs index fc967cb..8f1a761 100644 --- a/patch-db/src/test.rs +++ b/patch-db/src/test.rs @@ -1,9 +1,10 @@ use super::*; use proptest::prelude::*; -use tokio::runtime::Builder; +use tokio::{fs, runtime::Builder}; -async fn init_test_env() -> PatchDb { - let db = PatchDb::open("test.db").await.unwrap(); +async fn init_db(db_name: String) -> PatchDb { + cleanup_db(&db_name).await; + let db = PatchDb::open(db_name).await.unwrap(); db.put( &JsonPointer::<&'static str>::default(), &Sample { @@ -18,6 +19,10 @@ async fn init_test_env() -> PatchDb { db } +async fn cleanup_db(db_name: &String) { + fs::remove_file(db_name).await.ok(); +} + async fn put_string_into_root(db: PatchDb, s: String) -> Arc { println!("trying string: {}", s); db.put( @@ -30,25 +35,26 @@ async fn put_string_into_root(db: PatchDb, s: String) -> Arc { #[tokio::test] async fn basic() { - let db = init_test_env().await; + let db = init_db("basic".to_string()).await; let ptr: JsonPointer = "/b/b".parse().unwrap(); let mut get_res: Value = db.get(&ptr).await.unwrap(); assert_eq!(get_res, 1); db.put(&ptr, &"hello").await.unwrap(); get_res = db.get(&ptr).await.unwrap(); assert_eq!(get_res, "hello"); + cleanup_db(&"basic".to_string()).await; } #[tokio::test] async fn transaction() { - let db = init_test_env().await; + let db = init_db("transaction".to_string()).await; let mut tx = db.begin(); let ptr: JsonPointer = "/b/b".parse().unwrap(); tx.put(&ptr, &(2 as usize)).await.unwrap(); tx.put(&ptr, &(1 as usize)).await.unwrap(); let _res = tx.commit().await.unwrap(); println!("res = {:?}", _res); - + cleanup_db(&"transaction".to_string()).await; } proptest! { @@ -61,9 +67,11 @@ proptest! { .thread_stack_size(3 * 1024 * 1024) .build() .unwrap(); - let db = runtime.block_on(init_test_env()); + let db = runtime.block_on(init_db("doesnt_crash".to_string())); let put_future = put_string_into_root(db, s); runtime.block_on(put_future); + runtime.block_on(cleanup_db(&"doesnt_crash".to_string())); + } }