From 17588a76588222d11f2a6899bb09e25f777409e6 Mon Sep 17 00:00:00 2001 From: Martin Ek Date: Fri, 6 Apr 2018 15:32:01 +0200 Subject: [PATCH 1/5] Make sure DB is dropped after all tests --- tests/test_column_family.rs | 48 ++++--- tests/test_iterator.rs | 12 +- tests/test_multithreaded.rs | 10 +- tests/test_raw_iterator.rs | 230 +++++++++++++++++----------------- tests/test_rocksdb_options.rs | 15 ++- tests/test_slice_transform.rs | 65 +++++----- tests/util/mod.rs | 34 +++++ 7 files changed, 227 insertions(+), 187 deletions(-) create mode 100644 tests/util/mod.rs diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index 97ac54f..7922898 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -13,22 +13,24 @@ // limitations under the License. // extern crate rocksdb; +mod util; use rocksdb::{DB, MergeOperands, Options, ColumnFamilyDescriptor}; +use util::DBName; #[test] pub fn test_column_family() { - let path = "_rust_rocksdb_cftest"; + let n = DBName::new("_rust_rocksdb_cftest"); // should be able to create column families { let mut opts = Options::default(); opts.create_if_missing(true); opts.set_merge_operator("test operator", test_provided_merge, None); - let mut db = DB::open(&opts, path).unwrap(); + let mut db = DB::open(&opts, &n.name).unwrap(); let opts = Options::default(); match db.create_cf("cf1", &opts) { - Ok(_) => println!("cf1 created successfully"), + Ok(_db) => println!("cf1 created successfully"), Err(e) => { panic!("could not create column family: {}", e); } @@ -39,8 +41,8 @@ pub fn test_column_family() { { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - match DB::open(&opts, path) { - Ok(_) => { + match DB::open(&opts, &n.name) { + Ok(_db) => { panic!("should not have opened DB successfully without \ specifying column families") @@ -57,8 +59,8 @@ pub fn test_column_family() { { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - match DB::open_cf(&opts, path, &["cf1"]) { - Ok(_) => println!("successfully opened db with column family"), + match DB::open_cf(&opts, &n.name, &["cf1"]) { + Ok(_db) => println!("successfully opened db with column family"), Err(e) => panic!("failed to open db with column family: {}", e), } } @@ -66,7 +68,7 @@ pub fn test_column_family() { // should be able to list a cf { let opts = Options::default(); - let vec = DB::list_cf(&opts, path); + let vec = DB::list_cf(&opts, &n.name); match vec { Ok(vec) => assert_eq!(vec, vec!["default", "cf1"]), Err(e) => panic!("failed to drop column family: {}", e), @@ -81,19 +83,17 @@ pub fn test_column_family() { } // should b able to drop a cf { - let mut db = DB::open_cf(&Options::default(), path, &["cf1"]).unwrap(); + let mut db = DB::open_cf(&Options::default(), &n.name, &["cf1"]).unwrap(); match db.drop_cf("cf1") { Ok(_) => println!("cf1 successfully dropped."), Err(e) => panic!("failed to drop column family: {}", e), } } - - assert!(DB::destroy(&Options::default(), path).is_ok()); } #[test] fn test_create_missing_column_family() { - let path = "_rust_rocksdb_missing_cftest"; + let n = DBName::new("_rust_rocksdb_missing_cftest"); // should be able to create new column families when opening a new database { @@ -101,24 +101,22 @@ fn test_create_missing_column_family() { opts.create_if_missing(true); opts.create_missing_column_families(true); - match DB::open_cf(&opts, path, &["cf1"]) { - Ok(_) => println!("successfully created new column family"), + match DB::open_cf(&opts, &n.name, &["cf1"]) { + Ok(_db) => println!("successfully created new column family"), Err(e) => panic!("failed to create new column family: {}", e), } } - - assert!(DB::destroy(&Options::default(), path).is_ok()); } #[test] #[ignore] fn test_merge_operator() { - let path = "_rust_rocksdb_cftest_merge"; + let n = DBName::new("_rust_rocksdb_cftest_merge"); // TODO should be able to write, read, merge, batch, and iterate over a cf { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - let db = match DB::open_cf(&opts, path, &["cf1"]) { + let db = match DB::open_cf(&opts, &n.name, &["cf1"]) { Ok(db) => { println!("successfully opened db with column family"); db @@ -180,7 +178,7 @@ fn test_provided_merge(_: &[u8], #[test] pub fn test_column_family_with_options() { - let path = "_rust_rocksdb_cf_with_optionstest"; + let n = DBName::new("_rust_rocksdb_cf_with_optionstest"); { let mut cfopts = Options::default(); cfopts.set_max_write_buffer_number(16); @@ -191,8 +189,8 @@ pub fn test_column_family_with_options() { opts.create_missing_column_families(true); let cfs = vec![cf_descriptor]; - match DB::open_cf_descriptors(&opts, path, cfs) { - Ok(_) => println!("created db with column family descriptors succesfully"), + match DB::open_cf_descriptors(&opts, &n.name, cfs) { + Ok(_db) => println!("created db with column family descriptors succesfully"), Err(e) => { panic!("could not create new database with column family descriptors: {}", e); } @@ -207,13 +205,11 @@ pub fn test_column_family_with_options() { let opts = Options::default(); let cfs = vec![cf_descriptor]; - match DB::open_cf_descriptors(&opts, path, cfs) { - Ok(_) => println!("succesfully re-opened database with column family descriptorrs"), + match DB::open_cf_descriptors(&opts, &n.name, cfs) { + Ok(_db) => println!("succesfully re-opened database with column family descriptors"), Err(e) => { panic!("unable to re-open database with column family descriptors: {}", e); } } } - - assert!(DB::destroy(&Options::default(), path).is_ok()); -} \ No newline at end of file +} diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index 3cb368f..a069e9e 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -13,8 +13,10 @@ // limitations under the License. // extern crate rocksdb; +mod util; use rocksdb::{DB, Direction, IteratorMode, Options}; +use util::DBName; fn cba(input: &Box<[u8]>) -> Box<[u8]> { input.iter().cloned().collect::>().into_boxed_slice() @@ -22,7 +24,7 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> { #[test] pub fn test_iterator() { - let path = "_rust_rocksdb_iteratortest"; + let n = DBName::new("_rust_rocksdb_iteratortest"); { let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice(); let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice(); @@ -32,7 +34,7 @@ pub fn test_iterator() { let v2: Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); let v3: Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); let v4: Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); - let db = DB::open_default(path).unwrap(); + let db = DB::open_default(&n.name).unwrap(); let p = db.put(&*k1, &*v1); assert!(p.is_ok()); let p = db.put(&*k2, &*v2); @@ -153,15 +155,13 @@ pub fn test_iterator() { assert!(!iterator1.valid()); } } - let opts = Options::default(); - assert!(DB::destroy(&opts, path).is_ok()); } fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() } #[test] pub fn test_prefix_iterator() { - let path = "_rust_rocksdb_prefixiteratortest"; + let n = DBName::new("_rust_rocksdb_prefixiteratortest"); { let a1: Box<[u8]> = key(b"aaa1"); let a2: Box<[u8]> = key(b"aaa2"); @@ -174,7 +174,7 @@ pub fn test_prefix_iterator() { opts.create_if_missing(true); opts.set_prefix_extractor(prefix_extractor); - let db = DB::open(&opts, path).unwrap(); + let db = DB::open(&opts, &n.name).unwrap(); assert!(db.put(&*a1, &*a1).is_ok()); assert!(db.put(&*a2, &*a2).is_ok()); diff --git a/tests/test_multithreaded.rs b/tests/test_multithreaded.rs index 30ef752..3caf15f 100644 --- a/tests/test_multithreaded.rs +++ b/tests/test_multithreaded.rs @@ -13,18 +13,21 @@ // limitations under the License. // extern crate rocksdb; +mod util; -use rocksdb::{DB, Options}; +use rocksdb::DB; use std::thread; use std::sync::Arc; +use util::DBName; + const N: usize = 100_000; #[test] pub fn test_multithreaded() { - let path = "_rust_rocksdb_multithreadtest"; + let n = DBName::new("_rust_rocksdb_multithreadtest"); { - let db = DB::open_default(path).unwrap(); + let db = DB::open_default(&n.name).unwrap(); let db = Arc::new(db); db.put(b"key", b"value1").unwrap(); @@ -63,5 +66,4 @@ pub fn test_multithreaded() { j2.join().unwrap(); j3.join().unwrap(); } - assert!(DB::destroy(&Options::default(), path).is_ok()); } diff --git a/tests/test_raw_iterator.rs b/tests/test_raw_iterator.rs index 1591d90..5db968f 100644 --- a/tests/test_raw_iterator.rs +++ b/tests/test_raw_iterator.rs @@ -13,142 +13,144 @@ // limitations under the License. // extern crate rocksdb; +mod util; use rocksdb::DB; - - -fn setup_test_db(name: &str) -> DB { - use std::fs::remove_dir_all; - - let path = "_rust_rocksdb_rawiteratortest_".to_string() + name; - - match remove_dir_all(&path) { - Ok(_) => {} - Err(_) => {} // Don't care if tis fails - } - - DB::open_default(path).unwrap() -} - +use util::DBName; #[test] pub fn test_forwards_iteration() { - let db = setup_test_db("forwards_iteration"); - db.put(b"k1", b"v1").unwrap(); - db.put(b"k2", b"v2").unwrap(); - db.put(b"k3", b"v3").unwrap(); - db.put(b"k4", b"v4").unwrap(); - - let mut iter = db.raw_iterator(); - iter.seek_to_first(); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k1".to_vec())); - assert_eq!(iter.value(), Some(b"v1".to_vec())); - - iter.next(); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k2".to_vec())); - assert_eq!(iter.value(), Some(b"v2".to_vec())); - - iter.next(); // k3 - iter.next(); // k4 - iter.next(); // invalid! - - assert_eq!(iter.valid(), false); - assert_eq!(iter.key(), None); - assert_eq!(iter.value(), None); + let n = DBName::new("forwards_iteration"); + { + let db = DB::open_default(&n.name).unwrap(); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek_to_first(); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k1".to_vec())); + assert_eq!(iter.value(), Some(b"v1".to_vec())); + + iter.next(); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); + + iter.next(); // k3 + iter.next(); // k4 + iter.next(); // invalid! + + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + } } #[test] pub fn test_seek_last() { - let db = setup_test_db("backwards_iteration"); - db.put(b"k1", b"v1").unwrap(); - db.put(b"k2", b"v2").unwrap(); - db.put(b"k3", b"v3").unwrap(); - db.put(b"k4", b"v4").unwrap(); - - let mut iter = db.raw_iterator(); - iter.seek_to_last(); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k4".to_vec())); - assert_eq!(iter.value(), Some(b"v4".to_vec())); - - iter.prev(); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k3".to_vec())); - assert_eq!(iter.value(), Some(b"v3".to_vec())); - - iter.prev(); // k2 - iter.prev(); // k1 - iter.prev(); // invalid! - - assert_eq!(iter.valid(), false); - assert_eq!(iter.key(), None); - assert_eq!(iter.value(), None); + let n = DBName::new("backwards_iteration"); + { + let db = DB::open_default(&n.name).unwrap(); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek_to_last(); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k4".to_vec())); + assert_eq!(iter.value(), Some(b"v4".to_vec())); + + iter.prev(); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k3".to_vec())); + assert_eq!(iter.value(), Some(b"v3".to_vec())); + + iter.prev(); // k2 + iter.prev(); // k1 + iter.prev(); // invalid! + + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + } } #[test] pub fn test_seek() { - let db = setup_test_db("seek"); - db.put(b"k1", b"v1").unwrap(); - db.put(b"k2", b"v2").unwrap(); - db.put(b"k4", b"v4").unwrap(); - - let mut iter = db.raw_iterator(); - iter.seek(b"k2"); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k2".to_vec())); - assert_eq!(iter.value(), Some(b"v2".to_vec())); - - // Check it gets the next key when the key doesn't exist - iter.seek(b"k3"); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k4".to_vec())); - assert_eq!(iter.value(), Some(b"v4".to_vec())); + let n = DBName::new("seek"); + { + let db = DB::open_default(&n.name).unwrap(); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); + + // Check it gets the next key when the key doesn't exist + iter.seek(b"k3"); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k4".to_vec())); + assert_eq!(iter.value(), Some(b"v4".to_vec())); + } } #[test] pub fn test_seek_to_nonexistant() { - let db = setup_test_db("seek_to_nonexistant"); - db.put(b"k1", b"v1").unwrap(); - db.put(b"k3", b"v3").unwrap(); - db.put(b"k4", b"v4").unwrap(); - - let mut iter = db.raw_iterator(); - iter.seek(b"k2"); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k3".to_vec())); - assert_eq!(iter.value(), Some(b"v3".to_vec())); + let n = DBName::new("seek_to_nonexistant"); + { + let db = DB::open_default(&n.name).unwrap(); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k3".to_vec())); + assert_eq!(iter.value(), Some(b"v3".to_vec())); + } } #[test] pub fn test_seek_for_prev() { - let db = setup_test_db("seek_for_prev"); - db.put(b"k1", b"v1").unwrap(); - db.put(b"k2", b"v2").unwrap(); - db.put(b"k4", b"v4").unwrap(); - - let mut iter = db.raw_iterator(); - iter.seek(b"k2"); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k2".to_vec())); - assert_eq!(iter.value(), Some(b"v2".to_vec())); - - // Check it gets the previous key when the key doesn't exist - iter.seek_for_prev(b"k3"); - - assert_eq!(iter.valid(), true); - assert_eq!(iter.key(), Some(b"k2".to_vec())); - assert_eq!(iter.value(), Some(b"v2".to_vec())); + let n = DBName::new("seek_for_prev"); + { + let db = DB::open_default(&n.name).unwrap(); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); + + // Check it gets the previous key when the key doesn't exist + iter.seek_for_prev(b"k3"); + + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); + } } diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 12c9555..53db156 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -13,15 +13,18 @@ // limitations under the License. // extern crate rocksdb; +mod util; use rocksdb::{DB, Options}; +use util::DBName; #[test] fn test_set_num_levels() { - let path = "_rust_rocksdb_test_set_num_levels"; - let mut opts = Options::default(); - opts.create_if_missing(true); - opts.set_num_levels(2); - let db = DB::open(&opts, path).unwrap(); - drop(db); + let n = DBName::new("_rust_rocksdb_test_set_num_levels"); + { + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.set_num_levels(2); + let _db = DB::open(&opts, &n.name).unwrap(); + } } diff --git a/tests/test_slice_transform.rs b/tests/test_slice_transform.rs index 832681a..cb1e9b7 100644 --- a/tests/test_slice_transform.rs +++ b/tests/test_slice_transform.rs @@ -1,48 +1,51 @@ extern crate rocksdb; +mod util; use rocksdb::{DB, Options, SliceTransform}; +use util::DBName; #[test] pub fn test_slice_transform() { + let n = DBName::new("_rust_rocksdb_slicetransform_test"); + { + let a1: Box<[u8]> = key(b"aaa1"); + let a2: Box<[u8]> = key(b"aaa2"); + let b1: Box<[u8]> = key(b"bbb1"); + let b2: Box<[u8]> = key(b"bbb2"); - let path = "_rust_rocksdb_slicetransform_test"; - let a1: Box<[u8]> = key(b"aaa1"); - let a2: Box<[u8]> = key(b"aaa2"); - let b1: Box<[u8]> = key(b"bbb1"); - let b2: Box<[u8]> = key(b"bbb2"); + fn first_three(k: &[u8]) -> Vec { + k.iter().take(3).cloned().collect() + } - fn first_three(k: &[u8]) -> Vec { - k.iter().take(3).cloned().collect() - } + let prefix_extractor = SliceTransform::create("first_three", first_three, None); - let prefix_extractor = SliceTransform::create("first_three", first_three, None); + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.set_prefix_extractor(prefix_extractor); - let mut opts = Options::default(); - opts.create_if_missing(true); - opts.set_prefix_extractor(prefix_extractor); + let db = DB::open(&opts, &n.name).unwrap(); - let db = DB::open(&opts, path).unwrap(); + assert!(db.put(&*a1, &*a1).is_ok()); + assert!(db.put(&*a2, &*a2).is_ok()); + assert!(db.put(&*b1, &*b1).is_ok()); + assert!(db.put(&*b2, &*b2).is_ok()); - assert!(db.put(&*a1, &*a1).is_ok()); - assert!(db.put(&*a2, &*a2).is_ok()); - assert!(db.put(&*b1, &*b1).is_ok()); - assert!(db.put(&*b2, &*b2).is_ok()); + fn cba(input: &Box<[u8]>) -> Box<[u8]> { + input.iter().cloned().collect::>().into_boxed_slice() + } - fn cba(input: &Box<[u8]>) -> Box<[u8]> { - input.iter().cloned().collect::>().into_boxed_slice() - } + fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() } - fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() } + { + let expected = vec![(cba(&a1), cba(&a1)), (cba(&a2), cba(&a2))]; + let a_iterator = db.prefix_iterator(b"aaa"); + assert_eq!(a_iterator.collect::>(), expected) + } - { - let expected = vec![(cba(&a1), cba(&a1)), (cba(&a2), cba(&a2))]; - let a_iterator = db.prefix_iterator(b"aaa"); - assert_eq!(a_iterator.collect::>(), expected) - } - - { - let expected = vec![(cba(&b1), cba(&b1)), (cba(&b2), cba(&b2))]; - let b_iterator = db.prefix_iterator(b"bbb"); - assert_eq!(b_iterator.collect::>(), expected) + { + let expected = vec![(cba(&b1), cba(&b1)), (cba(&b2), cba(&b2))]; + let b_iterator = db.prefix_iterator(b"bbb"); + assert_eq!(b_iterator.collect::>(), expected) + } } } diff --git a/tests/util/mod.rs b/tests/util/mod.rs new file mode 100644 index 0000000..66a87e9 --- /dev/null +++ b/tests/util/mod.rs @@ -0,0 +1,34 @@ +extern crate rocksdb; + +use std::time::{SystemTime, UNIX_EPOCH}; + +use rocksdb::{DB, Options}; + +// Ensures that DB::Destroy is called for this database when DBName is dropped. +pub struct DBName { + pub name: String, +} + +impl DBName { + // Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse + // an old database in case of panics prior to Drop being called. + pub fn new(prefix: &str) -> DBName { + let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + let name = format!( + "{}.{}.{}", + prefix, + current_time.as_secs(), + current_time.subsec_nanos() + ); + + DBName { name } + } +} + +impl Drop for DBName { + fn drop(&mut self) { + let opts = Options::default(); + DB::destroy(&opts, &self.name).unwrap(); + } +} + From 3f3851e72083081eafd2114ab42d58f7adc3e610 Mon Sep 17 00:00:00 2001 From: Tyler Neely Date: Tue, 17 Jul 2018 16:09:31 +0200 Subject: [PATCH 2/5] Update test_iterator.rs more completely resolve conflict --- tests/test_iterator.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index 6019f3f..fabfe07 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -193,8 +193,6 @@ pub fn test_prefix_iterator() { assert_eq!(b_iterator.collect::>(), expected) } } - let opts = Options::default(); - assert!(DB::destroy(&opts, path).is_ok()); } #[test] From 0a1eff183301109b9276b7ae1547fdb9b22fcfe0 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Tue, 27 Nov 2018 13:33:23 -0600 Subject: [PATCH 3/5] Renaming DBName to DBPath and implementing AsRef on DBPath... --- tests/test_column_family.rs | 28 ++++++++++++++-------------- tests/test_iterator.rs | 10 +++++----- tests/test_multithreaded.rs | 6 +++--- tests/test_raw_iterator.rs | 22 +++++++++++----------- tests/test_rocksdb_options.rs | 6 +++--- tests/test_slice_transform.rs | 6 +++--- tests/util/mod.rs | 25 ++++++++++++++++--------- 7 files changed, 55 insertions(+), 48 deletions(-) diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index 7922898..dba89da 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -16,18 +16,18 @@ extern crate rocksdb; mod util; use rocksdb::{DB, MergeOperands, Options, ColumnFamilyDescriptor}; -use util::DBName; +use util::DBPath; #[test] pub fn test_column_family() { - let n = DBName::new("_rust_rocksdb_cftest"); + let n = DBPath::new("_rust_rocksdb_cftest"); // should be able to create column families { let mut opts = Options::default(); opts.create_if_missing(true); opts.set_merge_operator("test operator", test_provided_merge, None); - let mut db = DB::open(&opts, &n.name).unwrap(); + let mut db = DB::open(&opts, &n).unwrap(); let opts = Options::default(); match db.create_cf("cf1", &opts) { Ok(_db) => println!("cf1 created successfully"), @@ -41,7 +41,7 @@ pub fn test_column_family() { { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - match DB::open(&opts, &n.name) { + match DB::open(&opts, &n) { Ok(_db) => { panic!("should not have opened DB successfully without \ specifying column @@ -59,7 +59,7 @@ pub fn test_column_family() { { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - match DB::open_cf(&opts, &n.name, &["cf1"]) { + match DB::open_cf(&opts, &n, &["cf1"]) { Ok(_db) => println!("successfully opened db with column family"), Err(e) => panic!("failed to open db with column family: {}", e), } @@ -68,7 +68,7 @@ pub fn test_column_family() { // should be able to list a cf { let opts = Options::default(); - let vec = DB::list_cf(&opts, &n.name); + let vec = DB::list_cf(&opts, &n); match vec { Ok(vec) => assert_eq!(vec, vec!["default", "cf1"]), Err(e) => panic!("failed to drop column family: {}", e), @@ -83,7 +83,7 @@ pub fn test_column_family() { } // should b able to drop a cf { - let mut db = DB::open_cf(&Options::default(), &n.name, &["cf1"]).unwrap(); + let mut db = DB::open_cf(&Options::default(), &n, &["cf1"]).unwrap(); match db.drop_cf("cf1") { Ok(_) => println!("cf1 successfully dropped."), Err(e) => panic!("failed to drop column family: {}", e), @@ -93,7 +93,7 @@ pub fn test_column_family() { #[test] fn test_create_missing_column_family() { - let n = DBName::new("_rust_rocksdb_missing_cftest"); + let n = DBPath::new("_rust_rocksdb_missing_cftest"); // should be able to create new column families when opening a new database { @@ -101,7 +101,7 @@ fn test_create_missing_column_family() { opts.create_if_missing(true); opts.create_missing_column_families(true); - match DB::open_cf(&opts, &n.name, &["cf1"]) { + match DB::open_cf(&opts, &n, &["cf1"]) { Ok(_db) => println!("successfully created new column family"), Err(e) => panic!("failed to create new column family: {}", e), } @@ -111,12 +111,12 @@ fn test_create_missing_column_family() { #[test] #[ignore] fn test_merge_operator() { - let n = DBName::new("_rust_rocksdb_cftest_merge"); + let n = DBPath::new("_rust_rocksdb_cftest_merge"); // TODO should be able to write, read, merge, batch, and iterate over a cf { let mut opts = Options::default(); opts.set_merge_operator("test operator", test_provided_merge, None); - let db = match DB::open_cf(&opts, &n.name, &["cf1"]) { + let db = match DB::open_cf(&opts, &n, &["cf1"]) { Ok(db) => { println!("successfully opened db with column family"); db @@ -178,7 +178,7 @@ fn test_provided_merge(_: &[u8], #[test] pub fn test_column_family_with_options() { - let n = DBName::new("_rust_rocksdb_cf_with_optionstest"); + let n = DBPath::new("_rust_rocksdb_cf_with_optionstest"); { let mut cfopts = Options::default(); cfopts.set_max_write_buffer_number(16); @@ -189,7 +189,7 @@ pub fn test_column_family_with_options() { opts.create_missing_column_families(true); let cfs = vec![cf_descriptor]; - match DB::open_cf_descriptors(&opts, &n.name, cfs) { + match DB::open_cf_descriptors(&opts, &n, cfs) { Ok(_db) => println!("created db with column family descriptors succesfully"), Err(e) => { panic!("could not create new database with column family descriptors: {}", e); @@ -205,7 +205,7 @@ pub fn test_column_family_with_options() { let opts = Options::default(); let cfs = vec![cf_descriptor]; - match DB::open_cf_descriptors(&opts, &n.name, cfs) { + match DB::open_cf_descriptors(&opts, &n, cfs) { Ok(_db) => println!("succesfully re-opened database with column family descriptors"), Err(e) => { panic!("unable to re-open database with column family descriptors: {}", e); diff --git a/tests/test_iterator.rs b/tests/test_iterator.rs index fabfe07..fb735f7 100644 --- a/tests/test_iterator.rs +++ b/tests/test_iterator.rs @@ -16,7 +16,7 @@ extern crate rocksdb; mod util; use rocksdb::{DB, Direction, IteratorMode, MemtableFactory, Options}; -use util::DBName; +use util::DBPath; fn cba(input: &Box<[u8]>) -> Box<[u8]> { input.iter().cloned().collect::>().into_boxed_slice() @@ -24,7 +24,7 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> { #[test] pub fn test_iterator() { - let n = DBName::new("_rust_rocksdb_iteratortest"); + let n = DBPath::new("_rust_rocksdb_iteratortest"); { let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice(); let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice(); @@ -34,7 +34,7 @@ pub fn test_iterator() { let v2: Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); let v3: Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); let v4: Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); let p = db.put(&*k1, &*v1); assert!(p.is_ok()); let p = db.put(&*k2, &*v2); @@ -161,7 +161,7 @@ fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() } #[test] pub fn test_prefix_iterator() { - let n = DBName::new("_rust_rocksdb_prefixiteratortest"); + let n = DBPath::new("_rust_rocksdb_prefixiteratortest"); { let a1: Box<[u8]> = key(b"aaa1"); let a2: Box<[u8]> = key(b"aaa2"); @@ -174,7 +174,7 @@ pub fn test_prefix_iterator() { opts.create_if_missing(true); opts.set_prefix_extractor(prefix_extractor); - let db = DB::open(&opts, &n.name).unwrap(); + let db = DB::open(&opts, &n).unwrap(); assert!(db.put(&*a1, &*a1).is_ok()); assert!(db.put(&*a2, &*a2).is_ok()); diff --git a/tests/test_multithreaded.rs b/tests/test_multithreaded.rs index 3caf15f..38e5d1b 100644 --- a/tests/test_multithreaded.rs +++ b/tests/test_multithreaded.rs @@ -18,16 +18,16 @@ mod util; use rocksdb::DB; use std::thread; use std::sync::Arc; -use util::DBName; +use util::DBPath; const N: usize = 100_000; #[test] pub fn test_multithreaded() { - let n = DBName::new("_rust_rocksdb_multithreadtest"); + let n = DBPath::new("_rust_rocksdb_multithreadtest"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); let db = Arc::new(db); db.put(b"key", b"value1").unwrap(); diff --git a/tests/test_raw_iterator.rs b/tests/test_raw_iterator.rs index 5db968f..080bb7e 100644 --- a/tests/test_raw_iterator.rs +++ b/tests/test_raw_iterator.rs @@ -16,13 +16,13 @@ extern crate rocksdb; mod util; use rocksdb::DB; -use util::DBName; +use util::DBPath; #[test] pub fn test_forwards_iteration() { - let n = DBName::new("forwards_iteration"); + let n = DBPath::new("forwards_iteration"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); db.put(b"k1", b"v1").unwrap(); db.put(b"k2", b"v2").unwrap(); db.put(b"k3", b"v3").unwrap(); @@ -54,9 +54,9 @@ pub fn test_forwards_iteration() { #[test] pub fn test_seek_last() { - let n = DBName::new("backwards_iteration"); + let n = DBPath::new("backwards_iteration"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); db.put(b"k1", b"v1").unwrap(); db.put(b"k2", b"v2").unwrap(); db.put(b"k3", b"v3").unwrap(); @@ -88,9 +88,9 @@ pub fn test_seek_last() { #[test] pub fn test_seek() { - let n = DBName::new("seek"); + let n = DBPath::new("seek"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); db.put(b"k1", b"v1").unwrap(); db.put(b"k2", b"v2").unwrap(); db.put(b"k4", b"v4").unwrap(); @@ -114,9 +114,9 @@ pub fn test_seek() { #[test] pub fn test_seek_to_nonexistant() { - let n = DBName::new("seek_to_nonexistant"); + let n = DBPath::new("seek_to_nonexistant"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); db.put(b"k1", b"v1").unwrap(); db.put(b"k3", b"v3").unwrap(); db.put(b"k4", b"v4").unwrap(); @@ -132,9 +132,9 @@ pub fn test_seek_to_nonexistant() { #[test] pub fn test_seek_for_prev() { - let n = DBName::new("seek_for_prev"); + let n = DBPath::new("seek_for_prev"); { - let db = DB::open_default(&n.name).unwrap(); + let db = DB::open_default(&n).unwrap(); db.put(b"k1", b"v1").unwrap(); db.put(b"k2", b"v2").unwrap(); db.put(b"k4", b"v4").unwrap(); diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 53db156..24c71b3 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -16,15 +16,15 @@ extern crate rocksdb; mod util; use rocksdb::{DB, Options}; -use util::DBName; +use util::DBPath; #[test] fn test_set_num_levels() { - let n = DBName::new("_rust_rocksdb_test_set_num_levels"); + let n = DBPath::new("_rust_rocksdb_test_set_num_levels"); { let mut opts = Options::default(); opts.create_if_missing(true); opts.set_num_levels(2); - let _db = DB::open(&opts, &n.name).unwrap(); + let _db = DB::open(&opts, n).unwrap(); } } diff --git a/tests/test_slice_transform.rs b/tests/test_slice_transform.rs index cb1e9b7..484ad6f 100644 --- a/tests/test_slice_transform.rs +++ b/tests/test_slice_transform.rs @@ -2,11 +2,11 @@ extern crate rocksdb; mod util; use rocksdb::{DB, Options, SliceTransform}; -use util::DBName; +use util::DBPath; #[test] pub fn test_slice_transform() { - let n = DBName::new("_rust_rocksdb_slicetransform_test"); + let n = DBPath::new("_rust_rocksdb_slicetransform_test"); { let a1: Box<[u8]> = key(b"aaa1"); let a2: Box<[u8]> = key(b"aaa2"); @@ -23,7 +23,7 @@ pub fn test_slice_transform() { opts.create_if_missing(true); opts.set_prefix_extractor(prefix_extractor); - let db = DB::open(&opts, &n.name).unwrap(); + let db = DB::open(&opts, &n).unwrap(); assert!(db.put(&*a1, &*a1).is_ok()); assert!(db.put(&*a2, &*a2).is_ok()); diff --git a/tests/util/mod.rs b/tests/util/mod.rs index 66a87e9..196897f 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -1,34 +1,41 @@ extern crate rocksdb; use std::time::{SystemTime, UNIX_EPOCH}; +use std::path::{PathBuf, Path}; use rocksdb::{DB, Options}; -// Ensures that DB::Destroy is called for this database when DBName is dropped. -pub struct DBName { - pub name: String, +// Ensures that DB::Destroy is called for this database when DBPath is dropped. +pub struct DBPath { + path: PathBuf } -impl DBName { +impl DBPath { // Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse // an old database in case of panics prior to Drop being called. - pub fn new(prefix: &str) -> DBName { + pub fn new(prefix: &str) -> DBPath { let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); - let name = format!( + let path = format!( "{}.{}.{}", prefix, current_time.as_secs(), current_time.subsec_nanos() ); - DBName { name } + DBPath { path: PathBuf::from(path) } } } -impl Drop for DBName { +impl Drop for DBPath { fn drop(&mut self) { let opts = Options::default(); - DB::destroy(&opts, &self.name).unwrap(); + DB::destroy(&opts, &self.path).unwrap(); + } +} + +impl AsRef for DBPath { + fn as_ref(&self) -> &Path { + &self.path } } From 9108205a6d58e60cb666dfaf530566c0565c3838 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Tue, 27 Nov 2018 14:54:51 -0600 Subject: [PATCH 4/5] Test fix: Ensuring db cleanup doesn't happen until after test completion... --- tests/test_rocksdb_options.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 24c71b3..9429a6a 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -25,6 +25,6 @@ fn test_set_num_levels() { let mut opts = Options::default(); opts.create_if_missing(true); opts.set_num_levels(2); - let _db = DB::open(&opts, n).unwrap(); + let _db = DB::open(&opts, &n).unwrap(); } } From 16183467efe454b76dd2dffe1f8667d80766a892 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Wed, 28 Nov 2018 07:10:48 -0600 Subject: [PATCH 5/5] Changing comments to doc-comments [skip ci] --- tests/util/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/util/mod.rs b/tests/util/mod.rs index 196897f..e94413e 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -5,14 +5,14 @@ use std::path::{PathBuf, Path}; use rocksdb::{DB, Options}; -// Ensures that DB::Destroy is called for this database when DBPath is dropped. +/// Ensures that DB::Destroy is called for this database when DBPath is dropped. pub struct DBPath { path: PathBuf } impl DBPath { - // Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse - // an old database in case of panics prior to Drop being called. + /// Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse + /// an old database in case of panics prior to Drop being called. pub fn new(prefix: &str) -> DBPath { let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let path = format!(