Commit 3058b606 by tglman

add helper to control creation and drop of the database in tests, cleaned up duplicate code

parent b3631d64
Pipeline #14677510 passed with stage
extern crate persy;
use std::sync::{Mutex, Condvar};
use persy::PRes;
use persy::{PRes, Persy, Config};
use std::fs;
#[allow(dead_code)]
pub struct CountDown {
lock: Mutex<u64>,
cond: Condvar,
}
#[allow(dead_code)]
impl CountDown {
pub fn new(count: u64) -> CountDown {
CountDown {
......@@ -33,3 +36,23 @@ impl CountDown {
Ok(())
}
}
#[allow(dead_code)]
pub fn create_and_drop<F>(name: &str, test: F)
where F: FnOnce(&Persy)
{
create_and_drop_with_config(name, Config::new(), test);
}
#[allow(dead_code)]
pub fn create_and_drop_with_config<F>(name: &str, config: Config, test: F)
where F: FnOnce(&Persy)
{
let file = format!("./{}.persy", name);
Persy::create(file.clone()).expect(&format!("file '{}' do not exist", file));
{
let persy = Persy::open(file.clone(), config).unwrap();
test(&persy);
}
fs::remove_file(file).unwrap();
}
......@@ -6,7 +6,7 @@ use persy::{Persy, Config};
use std::fs;
use std::thread;
use std::sync::Arc;
use helpers::CountDown;
use helpers::{CountDown, create_and_drop};
#[test]
fn create() {
......@@ -53,12 +53,7 @@ fn create_open() {
#[test]
fn test_rollback() {
{
Persy::create("./rollback.persy").unwrap();
}
{
let persy = Persy::open("./rollback.persy", Config::new()).unwrap();
create_and_drop("rollback", |persy| {
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let finalizer = persy.prepare_commit(tx).unwrap();
......@@ -75,19 +70,12 @@ fn test_rollback() {
} else {
assert!(true);
}
}
fs::remove_file("./rollback.persy").unwrap();
});
}
#[test]
fn test_rollback_precommit() {
{
Persy::create("./rollback_pre.persy").unwrap();
}
{
let persy = Persy::open("./rollback_pre.persy", Config::new()).unwrap();
create_and_drop("rollback_pre", |persy| {
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let finalizer = persy.prepare_commit(tx).unwrap();
......@@ -105,20 +93,13 @@ fn test_rollback_precommit() {
} else {
assert!(true);
}
}
fs::remove_file("./rollback_pre.persy").unwrap();
});
}
#[test]
fn test_rollback_update() {
{
Persy::create("./rollback_update.persy").unwrap();
}
{
let persy = Persy::open("./rollback_update.persy", Config::new()).unwrap();
create_and_drop("rollback_update", |persy| {
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let rec_data: String = "something".into();
......@@ -151,8 +132,7 @@ fn test_rollback_update() {
} else {
assert!(false);
}
}
fs::remove_file("./rollback_update.persy").unwrap();
});
}
#[test()]
......
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