Make sure DB is dropped after all tests

master
Martin Ek 7 years ago
parent 701669d076
commit 17588a7658
  1. 46
      tests/test_column_family.rs
  2. 12
      tests/test_iterator.rs
  3. 10
      tests/test_multithreaded.rs
  4. 42
      tests/test_raw_iterator.rs
  5. 9
      tests/test_rocksdb_options.rs
  6. 9
      tests/test_slice_transform.rs
  7. 34
      tests/util/mod.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());
}

@ -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::<Vec<_>>().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());

@ -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());
}

@ -13,27 +13,16 @@
// 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");
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();
@ -59,12 +48,15 @@ pub fn test_forwards_iteration() {
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");
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();
@ -90,12 +82,15 @@ pub fn test_seek_last() {
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");
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();
@ -113,12 +108,15 @@ pub fn test_seek() {
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");
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();
@ -129,11 +127,14 @@ pub fn test_seek_to_nonexistant() {
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");
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();
@ -151,4 +152,5 @@ pub fn test_seek_for_prev() {
assert_eq!(iter.valid(), true);
assert_eq!(iter.key(), Some(b"k2".to_vec()));
assert_eq!(iter.value(), Some(b"v2".to_vec()));
}
}

@ -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 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, path).unwrap();
drop(db);
let _db = DB::open(&opts, &n.name).unwrap();
}
}

@ -1,11 +1,13 @@
extern crate rocksdb;
mod util;
use rocksdb::{DB, Options, SliceTransform};
use util::DBName;
#[test]
pub fn test_slice_transform() {
let path = "_rust_rocksdb_slicetransform_test";
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");
@ -21,7 +23,7 @@ pub fn test_slice_transform() {
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());
@ -45,4 +47,5 @@ pub fn test_slice_transform() {
let b_iterator = db.prefix_iterator(b"bbb");
assert_eq!(b_iterator.collect::<Vec<_>>(), expected)
}
}
}

@ -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();
}
}
Loading…
Cancel
Save