Commit 097dee1e authored by goroutine's avatar goroutine

Merge pull request #2 from BusyJay/busyjay/fix-test-error

*: clean up
parents 46ac1b41 a22187df
......@@ -7,3 +7,7 @@ Cargo.lock
_rust_rocksdb*
*rlib
tags
.idea/
out/
*.iml
\ No newline at end of file
......@@ -25,4 +25,5 @@ path = "test/test.rs"
[dependencies]
libc = "0.1.8"
tempdir = "0.3.4"
clippy = "*"
......@@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t};
use libc::{c_char, c_int, c_void, size_t};
use std::ffi::CString;
use std::mem;
use std::slice;
......
......@@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t};
use libc::{self, c_char, c_int, c_void, size_t};
use std::ffi::CStr;
use std::str::from_utf8;
......@@ -92,7 +91,7 @@ pub fn error_message(ptr: *const i8) -> String {
let c_str = unsafe { CStr::from_ptr(ptr as *const _) };
let s = from_utf8(c_str.to_bytes()).unwrap().to_owned();
unsafe {
libc::free(ptr as *mut libc::c_void);
libc::free(ptr as *mut c_void);
}
s
}
......@@ -412,10 +411,16 @@ extern "C" {
}
#[test]
fn internal() {
unsafe {
#[cfg(test)]
mod test {
use super::*;
use libc::*;
use std::ffi::CString;
use tempdir::TempDir;
#[test]
fn internal() {
unsafe {
let opts = rocksdb_options_create();
assert!(!opts.0.is_null());
......@@ -423,8 +428,10 @@ fn internal() {
rocksdb_options_optimize_level_style_compaction(opts, 0);
rocksdb_options_set_create_if_missing(opts, true);
let rustpath = "_rust_rocksdb_internaltest";
let cpath = CString::new(rustpath).unwrap();
let rustpath = TempDir::new("_rust_rocksdb_internaltest")
.expect("");
let cpath = CString::new(rustpath.path().to_str().unwrap())
.unwrap();
let cpath_ptr = cpath.as_ptr();
let mut err: *const i8 = 0 as *const i8;
......@@ -467,4 +474,5 @@ fn internal() {
rocksdb_destroy_db(opts, cpath_ptr, err_ptr);
assert!(err.is_null());
}
}
}
......@@ -13,16 +13,22 @@
// limitations under the License.
//
#![feature(plugin)]
#![plugin(clippy)]
pub use ffi as rocksdb_ffi;
pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
WriteBatch};
pub use rocksdb_options::{BlockBasedOptions, Options, WriteOptions};
pub use merge_operator::MergeOperands;
extern crate libc;
#[cfg(test)]
extern crate tempdir;
pub mod rocksdb;
pub mod ffi;
pub mod rocksdb_options;
pub mod merge_operator;
pub mod comparator;
pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter,
self as rocksdb_ffi};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
WriteBatch};
pub use rocksdb_options::{BlockBasedOptions, Options, WriteOptions};
pub use merge_operator::MergeOperands;
......@@ -146,6 +146,7 @@ mod tests {
use rocksdb::{BlockBasedOptions, DB, Options};
use rocksdb::DBCompactionStyle::DBUniversal;
#[allow(dead_code)]
fn tuned_for_somebody_elses_disk(path: &str,
opts: &mut Options,
blockopts: &mut BlockBasedOptions)
......
......@@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t};
use libc::{self, c_char, c_int, c_void, size_t};
use std::ffi::CString;
use std::mem;
use std::ptr;
......@@ -153,9 +152,16 @@ impl<'a> Iterator for &'a mut MergeOperands {
}
}
#[allow(unused_variables)]
#[allow(dead_code)]
fn test_provided_merge(new_key: &[u8],
#[cfg(test)]
mod test {
use super::*;
use rocksdb_options::Options;
use rocksdb::{DB, DBVector, Writable};
use tempdir::TempDir;
#[allow(unused_variables)]
#[allow(dead_code)]
fn test_provided_merge(new_key: &[u8],
existing_val: Option<&[u8]>,
operands: &mut MergeOperands)
-> Vec<u8> {
......@@ -172,21 +178,16 @@ fn test_provided_merge(new_key: &[u8],
}
}
result
}
#[allow(dead_code)]
#[test]
fn mergetest() {
use rocksdb_options::Options;
use rocksdb::{DB, DBVector, Writable};
}
let path = "_rust_rocksdb_mergetest";
#[allow(dead_code)]
#[test]
fn mergetest() {
let path = TempDir::new("_rust_rocksdb_mergetest").expect("");
let mut opts = Options::new();
opts.create_if_missing(true);
opts.add_merge_operator("test operator", test_provided_merge);
{
let db = DB::open(&opts, path).unwrap();
let db = DB::open(&opts, path.path().to_str().unwrap()).unwrap();
let p = db.put(b"k1", b"a");
assert!(p.is_ok());
let _ = db.merge(b"k1", b"b");
......@@ -202,7 +203,7 @@ fn mergetest() {
None => println!("did not read valid utf-8 out of the db"),
}
}
Err(e) => println!("error reading value"),
Err(e) => println!("error reading value {:?}", e),
_ => panic!("value not present"),
}
......@@ -212,5 +213,4 @@ fn mergetest() {
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
}
assert!(DB::destroy(&opts, path).is_ok());
}
......@@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate libc;
use std::collections::BTreeMap;
use std::ffi::CString;
use std::fs;
......@@ -23,7 +20,7 @@ use std::path::Path;
use std::slice;
use std::str::from_utf8;
use self::libc::{c_void, size_t};
use libc::{self, c_int, c_void, size_t};
use rocksdb_ffi::{self, DBCFHandle, error_message};
use rocksdb_options::{Options, WriteOptions};
......@@ -309,7 +306,7 @@ impl DB {
let nfam = cfs_v.len();
unsafe {
db = rocksdb_ffi::rocksdb_open_column_families(opts.inner, cpath_ptr as *const _,
nfam as libc::c_int,
nfam as c_int,
cfnames.as_ptr() as *const _,
copts, handles, err_ptr);
}
......@@ -846,7 +843,7 @@ impl Deref for DBVector {
impl Drop for DBVector {
fn drop(&mut self) {
unsafe {
libc::free(self.base as *mut libc::c_void);
libc::free(self.base as *mut c_void);
}
}
}
......@@ -864,11 +861,17 @@ impl DBVector {
}
}
#[test]
fn external() {
let path = "_rust_rocksdb_externaltest";
{
let db = DB::open_default(path).unwrap();
#[cfg(test)]
mod test {
use super::*;
use rocksdb_options::*;
use std::str;
use tempdir::TempDir;
#[test]
fn external() {
let path = TempDir::new("_rust_rocksdb_externaltest").expect("");
let db = DB::open_default(path.path().to_str().unwrap()).unwrap();
let p = db.put(b"k1", b"v1111");
assert!(p.is_ok());
let r: Result<Option<DBVector>, String> = db.get(b"k1");
......@@ -876,33 +879,26 @@ fn external() {
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
}
let opts = Options::new();
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();
#[allow(unused_variables)]
#[test]
fn errors_do_stuff() {
let path = TempDir::new("_rust_rocksdb_error").expect("");
let path_str = path.path().to_str().unwrap();
let db = DB::open_default(path_str).unwrap();
let opts = Options::new();
// The DB will still be open when we try to destroy and the lock should fail
match DB::destroy(&opts, path) {
Err(ref s) => {
assert!(s ==
"IO error: lock _rust_rocksdb_error/LOCK: No locks \
available")
}
match DB::destroy(&opts, path_str) {
Err(ref s) => assert!(s.contains("LOCK: No locks available")),
Ok(_) => panic!("should fail"),
}
}
}
#[test]
fn writebatch_works() {
let path = TempDir::new("_rust_rocksdb_writebacktest").expect("");
let db = DB::open_default(path.path().to_str().unwrap()).unwrap();
#[test]
fn writebatch_works() {
let path = "_rust_rocksdb_writebacktest";
{
let db = DB::open_default(path).unwrap();
{
// test put
let batch = WriteBatch::new();
assert!(db.get(b"k1").unwrap().is_none());
......@@ -912,8 +908,7 @@ fn writebatch_works() {
assert!(p.is_ok());
let r: Result<Option<DBVector>, String> = db.get(b"k1");
assert!(r.unwrap().unwrap().to_utf8().unwrap() == "v1111");
}
{
// test delete
let batch = WriteBatch::new();
let _ = batch.delete(b"k1");
......@@ -921,16 +916,12 @@ fn writebatch_works() {
assert!(p.is_ok());
assert!(db.get(b"k1").unwrap().is_none());
}
}
let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok());
}
#[test]
fn iterator_test() {
let path = "_rust_rocksdb_iteratortest";
{
let db = DB::open_default(path).unwrap();
#[test]
fn iterator_test() {
let path = TempDir::new("_rust_rocksdb_iteratortest").expect("");
let db = DB::open_default(path.path().to_str().unwrap()).unwrap();
let p = db.put(b"k1", b"v1111");
assert!(p.is_ok());
let p = db.put(b"k2", b"v2222");
......@@ -940,10 +931,8 @@ fn iterator_test() {
let iter = db.iterator(IteratorMode::Start);
for (k, v) in iter {
println!("Hello {}: {}",
from_utf8(&*k).unwrap(),
from_utf8(&*v).unwrap());
str::from_utf8(&*k).unwrap(),
str::from_utf8(&*v).unwrap());
}
}
let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok());
}
......@@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate libc;
use self::libc::{c_int, size_t};
use libc::{c_int, size_t};
use std::ffi::CString;
use std::mem;
......
extern crate rocksdb;
extern crate tempdir;
mod test_iterator;
mod test_multithreaded;
......
......@@ -13,17 +13,19 @@
// limitations under the License.
//
use rocksdb::{DB, MergeOperands, Options, Writable};
use tempdir::TempDir;
#[test]
pub fn test_column_family() {
let path = "_rust_rocksdb_cftest";
let path = TempDir::new("_rust_rocksdb_cftest").expect("");
let path_str = path.path().to_str().unwrap();
// should be able to create column families
{
let mut opts = Options::new();
opts.create_if_missing(true);
opts.add_merge_operator("test operator", test_provided_merge);
let mut db = DB::open(&opts, path).unwrap();
let mut db = DB::open(&opts, path_str).unwrap();
let opts = Options::new();
match db.create_cf("cf1", &opts) {
Ok(_) => println!("cf1 created successfully"),
......@@ -37,7 +39,7 @@ pub fn test_column_family() {
{
let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge);
match DB::open(&opts, path) {
match DB::open(&opts, path_str) {
Ok(_) => {
panic!("should not have opened DB successfully without \
specifying column
......@@ -54,7 +56,7 @@ pub fn test_column_family() {
{
let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge);
match DB::open_cf(&opts, path, &["cf1"]) {
match DB::open_cf(&opts, path_str, &["cf1"]) {
Ok(_) => println!("successfully opened db with column family"),
Err(e) => panic!("failed to open db with column family: {}", e),
}
......@@ -63,7 +65,7 @@ pub fn test_column_family() {
{
let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge);
let db = match DB::open_cf(&opts, path, &["cf1"]) {
let db = match DB::open_cf(&opts, path_str, &["cf1"]) {
Ok(db) => {
println!("successfully opened db with column family");
db
......@@ -76,6 +78,9 @@ pub fn test_column_family() {
"v1");
let p = db.put_cf(cf1, b"k1", b"a");
assert!(p.is_ok());
/*
// TODO support family merge operator
// have not finished yet, following codes won't work.
db.merge_cf(cf1, b"k1", b"b").unwrap();
db.merge_cf(cf1, b"k1", b"c").unwrap();
db.merge_cf(cf1, b"k1", b"d").unwrap();
......@@ -98,6 +103,7 @@ pub fn test_column_family() {
// TODO assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
*/
}
// TODO should be able to use writebatch ops with a cf
{
......@@ -107,14 +113,12 @@ pub fn test_column_family() {
}
// should b able to drop a cf
{
let mut db = DB::open_cf(&Options::new(), path, &["cf1"]).unwrap();
let mut db = DB::open_cf(&Options::new(), path_str, &["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::new(), path).is_ok());
}
fn test_provided_merge(_: &[u8],
......
use rocksdb::{DB, Direction, IteratorMode, Options, Writable};
use rocksdb::{DB, Direction, IteratorMode, Writable};
use tempdir::TempDir;
fn cba(input: &Box<[u8]>) -> Box<[u8]> {
input.iter().cloned().collect::<Vec<_>>().into_boxed_slice()
......@@ -6,8 +7,8 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> {
#[test]
pub fn test_iterator() {
let path = "_rust_rocksdb_iteratortest";
{
let path = TempDir::new("_rust_rocksdb_iteratortest").expect("");
let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice();
let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice();
let k3: Box<[u8]> = b"k3".to_vec().into_boxed_slice();
......@@ -16,7 +17,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(path.path().to_str().unwrap()).unwrap();
let p = db.put(&*k1, &*v1);
assert!(p.is_ok());
let p = db.put(&*k2, &*v2);
......@@ -26,63 +27,52 @@ pub fn test_iterator() {
let expected = vec![(cba(&k1), cba(&v1)),
(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3))];
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
// Test that it's idempotent
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
// Test it in reverse a few times
{
let iterator1 = db.iterator(IteratorMode::End);
let mut tmp_vec = iterator1.collect::<Vec<_>>();
tmp_vec.reverse();
assert_eq!(tmp_vec, expected);
}
{
let iterator1 = db.iterator(IteratorMode::End);
let mut tmp_vec = iterator1.collect::<Vec<_>>();
tmp_vec.reverse();
assert_eq!(tmp_vec, expected);
}
{
let iterator1 = db.iterator(IteratorMode::End);
let mut tmp_vec = iterator1.collect::<Vec<_>>();
tmp_vec.reverse();
assert_eq!(tmp_vec, expected);
}
{
let iterator1 = db.iterator(IteratorMode::End);
let mut tmp_vec = iterator1.collect::<Vec<_>>();
tmp_vec.reverse();
assert_eq!(tmp_vec, expected);
}
{
let iterator1 = db.iterator(IteratorMode::End);
let mut tmp_vec = iterator1.collect::<Vec<_>>();
tmp_vec.reverse();
assert_eq!(tmp_vec, expected);
}
// Try it forward again
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
let old_iterator = db.iterator(IteratorMode::Start);
let p = db.put(&*k4, &*v4);
......@@ -91,28 +81,20 @@ pub fn test_iterator() {
(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3)),
(cba(&k4), cba(&v4))];
{
assert_eq!(old_iterator.collect::<Vec<_>>(), expected);
}
{
let iterator1 = db.iterator(IteratorMode::Start);
assert_eq!(iterator1.collect::<Vec<_>>(), expected2);
}
{
let iterator1 = db.iterator(IteratorMode::From(b"k2",
Direction::Forward));
let expected = vec![(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3)),
(cba(&k4), cba(&v4))];
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
{
let iterator1 = db.iterator(IteratorMode::From(b"k2",
Direction::Reverse));
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k1), cba(&v1))];
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
}
}
let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok());
}
use rocksdb::{DB, Options, Writable};
use rocksdb::{DB, Writable};
use std::thread;
use std::sync::Arc;
use tempdir::TempDir;
const N: usize = 100_000;
#[test]
pub fn test_multithreaded() {
let path = "_rust_rocksdb_multithreadtest";
{
let db = DB::open_default(path).unwrap();
let path = TempDir::new("_rust_rocksdb_multithreadtest").expect("");
let db = DB::open_default(path.path().to_str().unwrap()).unwrap();
let db = Arc::new(db);
db.put(b"key", b"value1").unwrap();
......@@ -46,6 +47,4 @@ pub fn test_multithreaded() {
j1.join().unwrap();
j2.join().unwrap();
j3.join().unwrap();
}
assert!(DB::destroy(&Options::new(), path).is_ok());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment