Add test for BGSAVE

next
Sayan Nandan 3 years ago
parent d7cd1bfb70
commit 76d184663a

@ -27,3 +27,68 @@
//! This module contains automated tests for queries
mod kvengine;
mod bgsave {
use crate::config::BGSave;
use crate::coredb::{htable::HTable, CoreDB, Data};
use crate::dbnet::Terminator;
use crate::diskstore;
use std::fs;
use std::sync::Arc;
use tokio::sync::broadcast;
use tokio::time::{self, Duration};
#[tokio::test]
async fn test_bgsave() {
// pre-initialize our maps for comparison
let mut map_should_be_with_one = HTable::new();
map_should_be_with_one.insert(
String::from("sayan"),
Data::from_string("is testing bgsave".to_owned()),
);
#[allow(non_snake_case)]
let DUR_WITH_EPSILON: Duration = Duration::from_millis(1500) + Duration::from_secs(10);
let (signal, _) = broadcast::channel(1);
let datahandle = CoreDB::new_empty(Arc::new(None));
let flock = diskstore::flock::FileLock::lock("bgsave_test_1.bin").unwrap();
let bgsave_configuration = BGSave::Enabled(10);
let handle = tokio::spawn(diskstore::bgsave_scheduler(
datahandle.clone(),
bgsave_configuration,
flock,
Terminator::new(signal.subscribe()),
));
// sleep for 10 seconds with epsilon 1.5s
time::sleep(DUR_WITH_EPSILON).await;
// we should get an empty map
let saved = diskstore::test_deserialize(fs::read("bgsave_test_1.bin").unwrap()).unwrap();
assert!(saved.len() == 0);
// now let's quickly write some data
{
datahandle.acquire_write().unwrap().get_mut_ref().insert(
String::from("sayan"),
Data::from_string("is testing bgsave".to_owned()),
);
}
// sleep for 10 seconds with epsilon 1.5s
time::sleep(DUR_WITH_EPSILON).await;
// we should get a map with the one key
let saved = diskstore::test_deserialize(fs::read("bgsave_test_1.bin").unwrap()).unwrap();
assert_eq!(saved, map_should_be_with_one);
// now let's remove all the data
{
datahandle.acquire_write().unwrap().get_mut_ref().clear();
}
// sleep for 10 seconds with epsilon 1.5s
time::sleep(DUR_WITH_EPSILON).await;
let saved = diskstore::test_deserialize(fs::read("bgsave_test_1.bin").unwrap()).unwrap();
assert!(saved.len() == 0);
// drop the signal; all waiting tasks can now terminate
drop(signal);
handle.await.unwrap().unlock().unwrap();
// check the file again after unlocking
let saved = diskstore::test_deserialize(fs::read("bgsave_test_1.bin").unwrap()).unwrap();
assert!(saved.len() == 0);
fs::remove_file("bgsave_test_1.bin").unwrap();
}
}

Loading…
Cancel
Save