From 415578aa48a12cac01d763b7c08384dbcd46911b Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 10 Jun 2020 14:40:36 -0400 Subject: WIP: random failure test (with emulated storage) --- tests/common/mod.rs | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/rand_fail.rs | 25 +++++++++++ 2 files changed, 142 insertions(+) create mode 100644 tests/common/mod.rs create mode 100644 tests/rand_fail.rs (limited to 'tests') diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..fd98539 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,117 @@ +#[cfg(test)] +#[allow(dead_code)] + +extern crate growthring; +use growthring::wal::{WALFile, WALStore, WALPos, WALBytes}; +use std::collections::{HashMap, hash_map::Entry}; +use std::cell::RefCell; +use std::rc::Rc; + +/* +thread_local! { + //pub static RNG: RefCell = RefCell::new(::from_seed([0; 32])); + pub static RNG: RefCell = RefCell::new(rand::thread_rng()); +} + +pub fn gen_rand_letters(i: usize) -> String { + //let mut rng = rand::thread_rng(); + RNG.with(|rng| { + (0..i).map(|_| (rng.borrow_mut().gen::() % 26 + 'a' as u8) as char).collect() + }) +} +*/ + +struct FileContentEmul(RefCell>); + +impl FileContentEmul { + pub fn new() -> Self { FileContentEmul(RefCell::new(Vec::new())) } +} + +impl std::ops::Deref for FileContentEmul { + type Target = RefCell>; + fn deref(&self) -> &Self::Target {&self.0} +} + +/// Emulate the a virtual file handle. +pub struct WALFileEmul { + file: Rc +} + +impl WALFile for WALFileEmul { + fn allocate(&self, offset: WALPos, length: usize) -> Result<(), ()> { + let offset = offset as usize; + if offset + length > self.file.borrow().len() { + self.file.borrow_mut().resize(offset + length, 0) + } + for v in &mut self.file.borrow_mut()[offset..offset + length] { *v = 0 } + Ok(()) + } + + fn truncate(&self, length: usize) -> Result<(), ()> { + self.file.borrow_mut().resize(length, 0); + Ok(()) + } + + fn write(&self, offset: WALPos, data: WALBytes) { + let offset = offset as usize; + &self.file.borrow_mut()[offset..offset + data.len()].copy_from_slice(&data); + } + + fn read(&self, offset: WALPos, length: usize) -> Option { + let offset = offset as usize; + let file = self.file.borrow(); + if offset + length > file.len() { None } + else { + Some((&file[offset..offset + length]).to_vec().into_boxed_slice()) + } + } +} + +pub struct WALStoreEmulState{ + files: HashMap>, +} + +impl WALStoreEmulState { + pub fn new() -> Self { WALStoreEmulState { files: HashMap::new() } } +} + +/// Emulate the persistent storage state. +pub struct WALStoreEmul<'a>(&'a mut WALStoreEmulState); + +impl<'a> WALStoreEmul<'a> { + pub fn new(state: &'a mut WALStoreEmulState) -> Self { + WALStoreEmul(state) + } +} + +impl<'a> WALStore for WALStoreEmul<'a> { + type FileNameIter = std::vec::IntoIter; + + fn open_file(&mut self, filename: &str, touch: bool) -> Option> { + match self.0.files.entry(filename.to_string()) { + Entry::Occupied(e) => Some(Box::new(WALFileEmul { file: e.get().clone() })), + Entry::Vacant(e) => if touch { + Some(Box::new( + WALFileEmul { file: e.insert(Rc::new(FileContentEmul::new())).clone() })) + } else { + None + } + } + } + + fn remove_file(&mut self, filename: &str) -> Result<(), ()> { + self.0.files.remove(filename).ok_or(()).and_then(|_| Ok(())) + } + + fn enumerate_files(&self) -> Self::FileNameIter { + let mut logfiles = Vec::new(); + for (fname, _) in self.0.files.iter() { + logfiles.push(fname.clone()) + } + logfiles.into_iter() + } + + fn apply_payload(&mut self, payload: WALBytes) { + println!("apply_payload(payload={})", std::str::from_utf8(&payload).unwrap()) + } +} diff --git a/tests/rand_fail.rs b/tests/rand_fail.rs new file mode 100644 index 0000000..988da05 --- /dev/null +++ b/tests/rand_fail.rs @@ -0,0 +1,25 @@ +#[cfg(test)] + +extern crate growthring; +use growthring::wal::{WALLoader, WALWriter, WALRingId, WALBytes}; + +mod common; + +fn test(records: Vec, wal: &mut WALWriter) -> Box<[WALRingId]> { + let records: Vec = records.into_iter().map(|s| s.into_bytes().into_boxed_slice()).collect(); + let ret = wal.grow(&records); + for ring_id in ret.iter() { + println!("got ring id: {:?}", ring_id); + } + ret +} + +#[test] +fn test_rand_fail() { + let mut state = common::WALStoreEmulState::new(); + let mut wal = WALLoader::new(common::WALStoreEmul::new(&mut state), 9, 8, 1000).recover(); + for _ in 0..3 { + test(["hi", "hello", "lol"].iter().map(|s| s.to_string()).collect::>(), &mut wal); + } + let mut wal = WALLoader::new(common::WALStoreEmul::new(&mut state), 9, 8, 1000).recover(); +} -- cgit v1.2.3