From 395bd19d51c5f5e0bfd3b5897ddce5f6bef5ec79 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 25 Aug 2020 17:35:17 -0400 Subject: simplify API by moving `recover_func` to `load()` --- tests/common/mod.rs | 70 ++++++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 44 deletions(-) (limited to 'tests/common') diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 769f422..9ec9e8e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,9 +1,8 @@ #[cfg(test)] - #[allow(dead_code)] use async_trait::async_trait; use growthring::wal::{ - WALBytes, WALFile, WALLoader, WALPos, WALRingId, WALStore + WALBytes, WALFile, WALLoader, WALPos, WALRingId, WALStore, }; use indexmap::{map::Entry, IndexMap}; use rand::Rng; @@ -111,37 +110,25 @@ impl WALStoreEmulState { } /// Emulate the persistent storage state. -pub struct WALStoreEmul<'a, G, F> +pub struct WALStoreEmul<'a, G> where G: FailGen, - F: FnMut(WALBytes, WALRingId), { state: RefCell<&'a mut WALStoreEmulState>, fgen: Rc, - recover: RefCell, } -impl<'a, G: FailGen, F: FnMut(WALBytes, WALRingId)> WALStoreEmul<'a, G, F> { - pub fn new( - state: &'a mut WALStoreEmulState, - fgen: Rc, - recover: F, - ) -> Self { +impl<'a, G: FailGen> WALStoreEmul<'a, G> { + pub fn new(state: &'a mut WALStoreEmulState, fgen: Rc) -> Self { let state = RefCell::new(state); - let recover = RefCell::new(recover); - WALStoreEmul { - state, - fgen, - recover, - } + WALStoreEmul { state, fgen } } } #[async_trait(?Send)] -impl<'a, G, F> WALStore for WALStoreEmul<'a, G, F> +impl<'a, G> WALStore for WALStoreEmul<'a, G> where G: 'static + FailGen, - F: FnMut(WALBytes, WALRingId), { type FileNameIter = std::vec::IntoIter; @@ -194,23 +181,6 @@ where } Ok(logfiles.into_iter()) } - - fn apply_payload( - &self, - payload: WALBytes, - ringid: WALRingId, - ) -> Result<(), ()> { - if self.fgen.next_fail() { - return Err(()); - } - /* - println!("apply_payload(payload=0x{}, ringid={:?})", - hex::encode(&payload), - ringid); - */ - (&mut *self.recover.borrow_mut())(payload, ringid); - Ok(()) - } } pub struct SingleFailGen { @@ -336,7 +306,9 @@ impl PaintStrokes { } impl growthring::wal::Record for PaintStrokes { - fn serialize(&self) -> WALBytes { self.to_bytes() } + fn serialize(&self) -> WALBytes { + self.to_bytes() + } } #[test] @@ -461,7 +433,9 @@ impl Canvas { } else { None } - } else { None } + } else { + None + } } pub fn is_same(&self, other: &Canvas) -> bool { @@ -538,7 +512,14 @@ impl PaintingSim { ) -> Result<(), ()> { let mut rng = ::seed_from_u64(self.seed); - let mut wal = loader.load(WALStoreEmul::new(state, fgen.clone(), |_, _| {}))?; + let mut wal = + loader.load(WALStoreEmul::new(state, fgen.clone()), |_, _| { + if fgen.next_fail() { + Err(()) + } else { + Ok(()) + } + })?; for _ in 0..self.n { let pss = (0..self.m) .map(|_| { @@ -597,7 +578,8 @@ impl PaintingSim { pub fn get_walloader(&self) -> WALLoader { let mut loader = WALLoader::new(); - loader.file_nbit(self.file_nbit) + loader + .file_nbit(self.file_nbit) .block_nbit(self.block_nbit) .cache_size(self.file_cache); loader @@ -634,16 +616,16 @@ impl PaintingSim { let mut last_idx = 0; let mut napplied = 0; canvas.clear_queued(); - wal.load(WALStoreEmul::new( - state, - Rc::new(ZeroFailGen), + wal.load( + WALStoreEmul::new(state, Rc::new(ZeroFailGen)), |payload, ringid| { let s = PaintStrokes::from_bytes(&payload); canvas.prepaint(&s, &ringid); last_idx = *ringid_map.get(&ringid).unwrap() + 1; napplied += 1; + Ok(()) }, - )) + ) .unwrap(); println!("last = {}/{}, applied = {}", last_idx, ops.len(), napplied); canvas.paint_all(); -- cgit v1.2.3-70-g09d2