From 1a2c2682746f103cc73b7aaad0057e7ce3853c87 Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Sun, 27 Jan 2019 22:09:10 -0600 Subject: [PATCH] Moving tests to take advantage of DBPath (ensuring cleanup)... --- src/compaction_filter.rs | 33 +----- src/db.rs | 171 +------------------------------ tests/test_checkpoint.rs | 27 ++--- tests/test_compationfilter.rs | 51 ++++++++++ tests/test_db.rs | 183 ++++++++++++++++++++++++++++++++++ tests/util/mod.rs | 2 +- 6 files changed, 244 insertions(+), 223 deletions(-) create mode 100644 tests/test_compationfilter.rs create mode 100644 tests/test_db.rs diff --git a/src/compaction_filter.rs b/src/compaction_filter.rs index 0c63b2a..a6cc9ea 100644 --- a/src/compaction_filter.rs +++ b/src/compaction_filter.rs @@ -100,35 +100,4 @@ where 0 } } -} - -#[cfg(test)] -#[allow(unused_variables)] -fn test_filter(level: u32, key: &[u8], value: &[u8]) -> Decision { - use self::Decision::*; - match key.first() { - Some(&b'_') => Remove, - Some(&b'%') => Change(b"secret"), - _ => Keep, - } -} - -#[test] -fn compaction_filter_test() { - use {Options, DB}; - - let path = "_rust_rocksdb_filtertest"; - let mut opts = Options::default(); - opts.create_if_missing(true); - opts.set_compaction_filter("test", test_filter); - { - let db = DB::open(&opts, path).unwrap(); - let _ = db.put(b"k1", b"a"); - let _ = db.put(b"_k", b"b"); - let _ = db.put(b"%k", b"c"); - db.compact_range(None, None); - assert_eq!(&*db.get(b"k1").unwrap().unwrap(), b"a"); - assert!(db.get(b"_k").unwrap().is_none()); - assert_eq!(&*db.get(b"%k").unwrap().unwrap(), b"secret"); - } -} +} \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index d3eaec6..c07863d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1404,173 +1404,4 @@ impl DBVector { pub fn to_utf8(&self) -> Option<&str> { str::from_utf8(self.deref()).ok() } -} - -#[test] -fn test_db_vector() { - use std::mem; - let len: size_t = 4; - let data: *mut u8 = unsafe { mem::transmute(libc::calloc(len, mem::size_of::())) }; - let v = unsafe { DBVector::from_c(data, len) }; - let ctrl = [0u8, 0, 0, 0]; - assert_eq!(&*v, &ctrl[..]); -} - -#[test] -fn external() { - let path = "_rust_rocksdb_externaltest"; - { - let db = DB::open_default(path).unwrap(); - let p = db.put(b"k1", b"v1111"); - assert!(p.is_ok()); - let r: Result, Error> = db.get(b"k1"); - assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); - assert!(db.delete(b"k1").is_ok()); - assert!(db.get(b"k1").unwrap().is_none()); - } - let opts = Options::default(); - let result = DB::destroy(&opts, path); - assert!(result.is_ok()); -} - -#[test] -fn errors_do_stuff() { - let path = "_rust_rocksdb_error"; - let _db = DB::open_default(path).unwrap(); - let opts = Options::default(); - // The DB will still be open when we try to destroy it and the lock should fail. - match DB::destroy(&opts, path) { - Err(s) => { - let message = s.to_string(); - assert!(message.find("IO error:").is_some()); - assert!(message.find("_rust_rocksdb_error/LOCK:").is_some()); - } - Ok(_) => panic!("should fail"), - } -} - -#[test] -fn writebatch_works() { - let path = "_rust_rocksdb_writebacktest"; - { - let db = DB::open_default(path).unwrap(); - { - // test put - let mut batch = WriteBatch::default(); - assert!(db.get(b"k1").unwrap().is_none()); - assert_eq!(batch.len(), 0); - assert!(batch.is_empty()); - let _ = batch.put(b"k1", b"v1111"); - assert_eq!(batch.len(), 1); - assert!(!batch.is_empty()); - assert!(db.get(b"k1").unwrap().is_none()); - let p = db.write(batch); - assert!(p.is_ok()); - let r: Result, Error> = db.get(b"k1"); - assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); - } - { - // test delete - let mut batch = WriteBatch::default(); - let _ = batch.delete(b"k1"); - assert_eq!(batch.len(), 1); - assert!(!batch.is_empty()); - let p = db.write(batch); - assert!(p.is_ok()); - assert!(db.get(b"k1").unwrap().is_none()); - } - { - // test size_in_bytes - let mut batch = WriteBatch::default(); - let before = batch.size_in_bytes(); - let _ = batch.put(b"k1", b"v1234567890"); - let after = batch.size_in_bytes(); - assert!(before + 10 <= after); - } - } - let opts = Options::default(); - assert!(DB::destroy(&opts, path).is_ok()); -} - -#[test] -fn iterator_test() { - let path = "_rust_rocksdb_iteratortest"; - { - let db = DB::open_default(path).unwrap(); - let p = db.put(b"k1", b"v1111"); - assert!(p.is_ok()); - let p = db.put(b"k2", b"v2222"); - assert!(p.is_ok()); - let p = db.put(b"k3", b"v3333"); - assert!(p.is_ok()); - let iter = db.iterator(IteratorMode::Start); - for (k, v) in iter { - println!( - "Hello {}: {}", - str::from_utf8(&*k).unwrap(), - str::from_utf8(&*v).unwrap() - ); - } - } - let opts = Options::default(); - assert!(DB::destroy(&opts, path).is_ok()); -} - -#[test] -fn snapshot_test() { - let path = "_rust_rocksdb_snapshottest"; - { - let db = DB::open_default(path).unwrap(); - let p = db.put(b"k1", b"v1111"); - assert!(p.is_ok()); - - let snap = db.snapshot(); - let r: Result, Error> = snap.get(b"k1"); - assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); - - let p = db.put(b"k2", b"v2222"); - assert!(p.is_ok()); - - assert!(db.get(b"k2").unwrap().is_some()); - assert!(snap.get(b"k2").unwrap().is_none()); - } - let opts = Options::default(); - assert!(DB::destroy(&opts, path).is_ok()); -} - -#[test] -fn set_option_test() { - let path = "_rust_rocksdb_set_optionstest"; - { - let db = DB::open_default(path).unwrap(); - // set an option to valid values - assert!(db - .set_options(&[("disable_auto_compactions", "true")]) - .is_ok()); - assert!(db - .set_options(&[("disable_auto_compactions", "false")]) - .is_ok()); - // invalid names/values should result in an error - assert!(db - .set_options(&[("disable_auto_compactions", "INVALID_VALUE")]) - .is_err()); - assert!(db - .set_options(&[("INVALID_NAME", "INVALID_VALUE")]) - .is_err()); - // option names/values must not contain NULLs - assert!(db - .set_options(&[("disable_auto_compactions", "true\0")]) - .is_err()); - assert!(db - .set_options(&[("disable_auto_compactions\0", "true")]) - .is_err()); - // empty options are not allowed - assert!(db.set_options(&[]).is_err()); - // multiple options can be set in a single API call - let multiple_options = [ - ("paranoid_file_checks", "true"), - ("report_bg_io_stats", "true"), - ]; - db.set_options(&multiple_options).unwrap(); - } -} +} \ No newline at end of file diff --git a/tests/test_checkpoint.rs b/tests/test_checkpoint.rs index 854a1d1..998f256 100644 --- a/tests/test_checkpoint.rs +++ b/tests/test_checkpoint.rs @@ -13,18 +13,17 @@ // limitations under the License. // extern crate rocksdb; +mod util; use rocksdb::{checkpoint::Checkpoint, Options, DB}; -use std::fs::remove_dir_all; +use util::DBPath; #[test] pub fn test_single_checkpoint() { const PATH_PREFIX: &str = "_rust_rocksdb_cp_single_"; // Create DB with some data - let db_path = format!("{}db1", PATH_PREFIX); - - let _ = remove_dir_all(&db_path); + let db_path = DBPath::new(&format!("{}db1", PATH_PREFIX)); let mut opts = Options::default(); opts.create_if_missing(true); @@ -37,8 +36,7 @@ pub fn test_single_checkpoint() { // Create checkpoint let cp1 = Checkpoint::new(&db).unwrap(); - let cp1_path = format!("{}cp1", PATH_PREFIX); - let _ = remove_dir_all(&cp1_path); + let cp1_path = DBPath::new(&format!("{}cp1", PATH_PREFIX)); cp1.create_checkpoint(&cp1_path).unwrap(); // Verify checkpoint @@ -48,9 +46,6 @@ pub fn test_single_checkpoint() { assert_eq!(*cp.get(b"k2").unwrap().unwrap(), *b"v2"); assert_eq!(*cp.get(b"k3").unwrap().unwrap(), *b"v3"); assert_eq!(*cp.get(b"k4").unwrap().unwrap(), *b"v4"); - - let _ = remove_dir_all(&db_path); - let _ = remove_dir_all(&cp1_path); } #[test] @@ -58,8 +53,7 @@ pub fn test_multi_checkpoints() { const PATH_PREFIX: &str = "_rust_rocksdb_cp_multi_"; // Create DB with some data - let db_path = format!("{}db1", PATH_PREFIX); - let _ = remove_dir_all(&db_path); + let db_path = DBPath::new(&format!("{}db1", PATH_PREFIX)); let mut opts = Options::default(); opts.create_if_missing(true); @@ -72,8 +66,7 @@ pub fn test_multi_checkpoints() { // Create first checkpoint let cp1 = Checkpoint::new(&db).unwrap(); - let cp1_path = format!("{}cp1", PATH_PREFIX); - let _ = remove_dir_all(&cp1_path); + let cp1_path = DBPath::new(&format!("{}cp1", PATH_PREFIX)); cp1.create_checkpoint(&cp1_path).unwrap(); // Verify checkpoint @@ -84,8 +77,6 @@ pub fn test_multi_checkpoints() { assert_eq!(*cp.get(b"k3").unwrap().unwrap(), *b"v3"); assert_eq!(*cp.get(b"k4").unwrap().unwrap(), *b"v4"); - let _ = remove_dir_all(&cp1_path); - // Change some existing keys db.put(b"k1", b"modified").unwrap(); db.put(b"k2", b"changed").unwrap(); @@ -96,8 +87,7 @@ pub fn test_multi_checkpoints() { // Create another checkpoint let cp2 = Checkpoint::new(&db).unwrap(); - let cp2_path = format!("{}cp2", PATH_PREFIX); - let _ = remove_dir_all(&cp2_path); + let cp2_path = DBPath::new(&format!("{}cp2", PATH_PREFIX)); cp2.create_checkpoint(&cp2_path).unwrap(); // Verify second checkpoint @@ -107,7 +97,4 @@ pub fn test_multi_checkpoints() { assert_eq!(*cp.get(b"k2").unwrap().unwrap(), *b"changed"); assert_eq!(*cp.get(b"k5").unwrap().unwrap(), *b"v5"); assert_eq!(*cp.get(b"k6").unwrap().unwrap(), *b"v6"); - - let _ = remove_dir_all(&db_path); - let _ = remove_dir_all(&cp2_path); } diff --git a/tests/test_compationfilter.rs b/tests/test_compationfilter.rs new file mode 100644 index 0000000..5cd4dc0 --- /dev/null +++ b/tests/test_compationfilter.rs @@ -0,0 +1,51 @@ +// Copyright 2014 Tyler Neely +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +extern crate rocksdb; + +mod util; + +use rocksdb::{CompactionDecision, DB, Options}; +use util::DBPath; + +#[cfg(test)] +#[allow(unused_variables)] +fn test_filter(level: u32, key: &[u8], value: &[u8]) -> CompactionDecision { + use self::CompactionDecision::*; + match key.first() { + Some(&b'_') => Remove, + Some(&b'%') => Change(b"secret"), + _ => Keep, + } +} + +#[test] +fn compaction_filter_test() { + use {Options, DB}; + + let path = DBPath::new("_rust_rocksdb_filtertest"); + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.set_compaction_filter("test", test_filter); + { + let db = DB::open(&opts, &path).unwrap(); + let _ = db.put(b"k1", b"a"); + let _ = db.put(b"_k", b"b"); + let _ = db.put(b"%k", b"c"); + db.compact_range(None, None); + assert_eq!(&*db.get(b"k1").unwrap().unwrap(), b"a"); + assert!(db.get(b"_k").unwrap().is_none()); + assert_eq!(&*db.get(b"%k").unwrap().unwrap(), b"secret"); + } +} \ No newline at end of file diff --git a/tests/test_db.rs b/tests/test_db.rs new file mode 100644 index 0000000..01f5b92 --- /dev/null +++ b/tests/test_db.rs @@ -0,0 +1,183 @@ +// Copyright 2014 Tyler Neely +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +extern crate rocksdb; +extern crate libc; + +mod util; + +use libc::{size_t}; + +use rocksdb::{DB, DBVector, Error, IteratorMode, Options, WriteBatch}; +use std::str; +use util::DBPath; + +#[test] +fn test_db_vector() { + use std::mem; + let len: size_t = 4; + let data: *mut u8 = unsafe { mem::transmute(libc::calloc(len, mem::size_of::())) }; + let v = unsafe { DBVector::from_c(data, len) }; + let ctrl = [0u8, 0, 0, 0]; + assert_eq!(&*v, &ctrl[..]); +} + +#[test] +fn external() { + let path = DBPath::new("_rust_rocksdb_externaltest"); + let db = DB::open_default(&path).unwrap(); + let p = db.put(b"k1", b"v1111"); + assert!(p.is_ok()); + let r: Result, Error> = db.get(b"k1"); + assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); + assert!(db.delete(b"k1").is_ok()); + assert!(db.get(b"k1").unwrap().is_none()); +} + +#[test] +fn errors_do_stuff() { + let path = DBPath::new("_rust_rocksdb_error"); + let _db = DB::open_default(&path).unwrap(); + let opts = Options::default(); + // The DB will still be open when we try to destroy it and the lock should fail. + match DB::destroy(&opts, &path) { + Err(s) => { + let message = s.to_string(); + assert!(message.find("IO error:").is_some()); + assert!(message.find("_rust_rocksdb_error").is_some()); + assert!(message.find("/LOCK:").is_some()); + } + Ok(_) => panic!("should fail"), + } +} + +#[test] +fn writebatch_works() { + let path = DBPath::new("_rust_rocksdb_writebacktest"); + { + let db = DB::open_default(&path).unwrap(); + { + // test put + let mut batch = WriteBatch::default(); + assert!(db.get(b"k1").unwrap().is_none()); + assert_eq!(batch.len(), 0); + assert!(batch.is_empty()); + let _ = batch.put(b"k1", b"v1111"); + assert_eq!(batch.len(), 1); + assert!(!batch.is_empty()); + assert!(db.get(b"k1").unwrap().is_none()); + let p = db.write(batch); + assert!(p.is_ok()); + let r: Result, Error> = db.get(b"k1"); + assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); + } + { + // test delete + let mut batch = WriteBatch::default(); + let _ = batch.delete(b"k1"); + assert_eq!(batch.len(), 1); + assert!(!batch.is_empty()); + let p = db.write(batch); + assert!(p.is_ok()); + assert!(db.get(b"k1").unwrap().is_none()); + } + { + // test size_in_bytes + let mut batch = WriteBatch::default(); + let before = batch.size_in_bytes(); + let _ = batch.put(b"k1", b"v1234567890"); + let after = batch.size_in_bytes(); + assert!(before + 10 <= after); + } + } +} + +#[test] +fn iterator_test() { + let path = DBPath::new("_rust_rocksdb_iteratortest"); + { + let db = DB::open_default(&path).unwrap(); + let p = db.put(b"k1", b"v1111"); + assert!(p.is_ok()); + let p = db.put(b"k2", b"v2222"); + assert!(p.is_ok()); + let p = db.put(b"k3", b"v3333"); + assert!(p.is_ok()); + let iter = db.iterator(IteratorMode::Start); + for (k, v) in iter { + println!( + "Hello {}: {}", + str::from_utf8(&*k).unwrap(), + str::from_utf8(&*v).unwrap() + ); + } + } +} + +#[test] +fn snapshot_test() { + let path = DBPath::new("_rust_rocksdb_snapshottest"); + { + let db = DB::open_default(&path).unwrap(); + let p = db.put(b"k1", b"v1111"); + assert!(p.is_ok()); + + let snap = db.snapshot(); + let r: Result, Error> = snap.get(b"k1"); + assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111"); + + let p = db.put(b"k2", b"v2222"); + assert!(p.is_ok()); + + assert!(db.get(b"k2").unwrap().is_some()); + assert!(snap.get(b"k2").unwrap().is_none()); + } +} + +#[test] +fn set_option_test() { + let path = DBPath::new("_rust_rocksdb_set_optionstest"); + { + let db = DB::open_default(&path).unwrap(); + // set an option to valid values + assert!(db + .set_options(&[("disable_auto_compactions", "true")]) + .is_ok()); + assert!(db + .set_options(&[("disable_auto_compactions", "false")]) + .is_ok()); + // invalid names/values should result in an error + assert!(db + .set_options(&[("disable_auto_compactions", "INVALID_VALUE")]) + .is_err()); + assert!(db + .set_options(&[("INVALID_NAME", "INVALID_VALUE")]) + .is_err()); + // option names/values must not contain NULLs + assert!(db + .set_options(&[("disable_auto_compactions", "true\0")]) + .is_err()); + assert!(db + .set_options(&[("disable_auto_compactions\0", "true")]) + .is_err()); + // empty options are not allowed + assert!(db.set_options(&[]).is_err()); + // multiple options can be set in a single API call + let multiple_options = [ + ("paranoid_file_checks", "true"), + ("report_bg_io_stats", "true"), + ]; + db.set_options(&multiple_options).unwrap(); + } +} \ No newline at end of file diff --git a/tests/util/mod.rs b/tests/util/mod.rs index 1e028ba..5987bbe 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -39,4 +39,4 @@ impl AsRef for DBPath { fn as_ref(&self) -> &Path { &self.path } -} +} \ No newline at end of file