From 617c515ec1834501d4835076de3adcfb495d8ef9 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 10 Jun 2020 15:56:05 -0400 Subject: add basic error handling --- tests/common/mod.rs | 30 +++++++++++++++++------------- tests/rand_fail.rs | 6 +++--- 2 files changed, 20 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/common/mod.rs b/tests/common/mod.rs index fd98539..7a7e42a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -32,9 +32,11 @@ impl std::ops::Deref for FileContentEmul { fn deref(&self) -> &Self::Target {&self.0} } +type FailGen = std::iter::Iterator; + /// Emulate the a virtual file handle. pub struct WALFileEmul { - file: Rc + file: Rc, } impl WALFile for WALFileEmul { @@ -52,17 +54,18 @@ impl WALFile for WALFileEmul { Ok(()) } - fn write(&self, offset: WALPos, data: WALBytes) { + fn write(&self, offset: WALPos, data: WALBytes) -> Result<(), ()> { let offset = offset as usize; &self.file.borrow_mut()[offset..offset + data.len()].copy_from_slice(&data); + Ok(()) } - fn read(&self, offset: WALPos, length: usize) -> Option { + fn read(&self, offset: WALPos, length: usize) -> Result, ()> { let offset = offset as usize; let file = self.file.borrow(); - if offset + length > file.len() { None } + if offset + length > file.len() { Ok(None) } else { - Some((&file[offset..offset + length]).to_vec().into_boxed_slice()) + Ok(Some((&file[offset..offset + length]).to_vec().into_boxed_slice())) } } } @@ -87,14 +90,14 @@ impl<'a> WALStoreEmul<'a> { impl<'a> WALStore for WALStoreEmul<'a> { type FileNameIter = std::vec::IntoIter; - fn open_file(&mut self, filename: &str, touch: bool) -> Option> { + fn open_file(&mut self, filename: &str, touch: bool) -> Result, ()> { match self.0.files.entry(filename.to_string()) { - Entry::Occupied(e) => Some(Box::new(WALFileEmul { file: e.get().clone() })), + Entry::Occupied(e) => Ok(Box::new(WALFileEmul { file: e.get().clone() })), Entry::Vacant(e) => if touch { - Some(Box::new( + Ok(Box::new( WALFileEmul { file: e.insert(Rc::new(FileContentEmul::new())).clone() })) } else { - None + Err(()) } } } @@ -103,15 +106,16 @@ impl<'a> WALStore for WALStoreEmul<'a> { self.0.files.remove(filename).ok_or(()).and_then(|_| Ok(())) } - fn enumerate_files(&self) -> Self::FileNameIter { + fn enumerate_files(&self) -> Result { let mut logfiles = Vec::new(); for (fname, _) in self.0.files.iter() { logfiles.push(fname.clone()) } - logfiles.into_iter() + Ok(logfiles.into_iter()) } - fn apply_payload(&mut self, payload: WALBytes) { - println!("apply_payload(payload={})", std::str::from_utf8(&payload).unwrap()) + fn apply_payload(&mut self, payload: WALBytes) -> Result<(), ()> { + println!("apply_payload(payload={})", std::str::from_utf8(&payload).unwrap()); + Ok(()) } } diff --git a/tests/rand_fail.rs b/tests/rand_fail.rs index 988da05..7499694 100644 --- a/tests/rand_fail.rs +++ b/tests/rand_fail.rs @@ -7,7 +7,7 @@ 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); + let ret = wal.grow(&records).unwrap(); for ring_id in ret.iter() { println!("got ring id: {:?}", ring_id); } @@ -17,9 +17,9 @@ fn test(records: Vec, wal: &mut WALWriter) -> Box< #[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(); + let mut wal = WALLoader::new(common::WALStoreEmul::new(&mut state), 9, 8, 1000).recover().unwrap(); 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(); + let mut wal = WALLoader::new(common::WALStoreEmul::new(&mut state), 9, 8, 1000).recover().unwrap(); } -- cgit v1.2.3-70-g09d2