From 350f2638cb3e15df231381b8fd286595058b7a54 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 10 Jun 2020 22:50:38 -0400 Subject: add closure to WALStoreEmul --- tests/common/mod.rs | 26 +++++++++++++++++++------- tests/rand_fail.rs | 11 ++++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ce27e16..a8dab51 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -89,18 +89,29 @@ impl WALStoreEmulState { } /// Emulate the persistent storage state. -pub struct WALStoreEmul<'a, G: FailGen> { +pub struct WALStoreEmul<'a, G, F> +where + G: FailGen, + F: Fn(WALBytes, WALPos) { state: &'a mut WALStoreEmulState, - fgen: Rc + fgen: Rc, + recover: F } -impl<'a, G: FailGen> WALStoreEmul<'a, G> { - pub fn new(state: &'a mut WALStoreEmulState, fail_gen: G) -> Self { - WALStoreEmul { state, fgen: Rc::new(fail_gen) } +impl<'a, G: FailGen, F: Fn(WALBytes, WALPos)> WALStoreEmul<'a, G, F> { + pub fn new(state: &'a mut WALStoreEmulState, fail_gen: G, + recover: F) -> Self { + WALStoreEmul { + state, + fgen: Rc::new(fail_gen), + recover + } } } -impl<'a, G: 'static + FailGen> WALStore for WALStoreEmul<'a, G> { +impl<'a, G, F> WALStore for WALStoreEmul<'a, G, F> +where + G: 'static + FailGen, F: Fn(WALBytes, WALPos) { type FileNameIter = std::vec::IntoIter; fn open_file(&mut self, filename: &str, touch: bool) -> Result, ()> { @@ -122,7 +133,7 @@ impl<'a, G: 'static + FailGen> WALStore for WALStoreEmul<'a, G> { } fn remove_file(&mut self, filename: &str) -> Result<(), ()> { - println!("remove {}", filename); + println!("remove_file(filename={})", filename); if self.fgen.next_fail() { return Err(()) } self.state.files.remove(filename).ok_or(()).and_then(|_| Ok(())) } @@ -141,6 +152,7 @@ impl<'a, G: 'static + FailGen> WALStore for WALStoreEmul<'a, G> { println!("apply_payload(payload=0x{}, wal_off={})", hex::encode(&payload), wal_off); + (self.recover)(payload, wal_off); Ok(()) } } diff --git a/tests/rand_fail.rs b/tests/rand_fail.rs index 0cd519c..6c2c3c1 100644 --- a/tests/rand_fail.rs +++ b/tests/rand_fail.rs @@ -41,21 +41,22 @@ fn run(n: usize, m: usize, k: usize, Ok(()) } -fn check(canvas: &mut Canvas, wal: &mut WALLoader, trace: &Vec) -> bool { +fn check(canvas: &mut Canvas, wal: &mut WALLoader, trace: &Vec) -> bool { true } #[test] fn test_rand_fail() { - let fgen = SingleFailGen::new(100000); + let fgen = SingleFailGen::new(100); let n = 100; let m = 10; let k = 100; let mut rng = rand::thread_rng(); let mut state = WALStoreEmulState::new(); - let mut wal = WALLoader::new(WALStoreEmul::new(&mut state, fgen), 9, 8, 1000).recover().unwrap(); + let mut wal = WALLoader::new(WALStoreEmul::new(&mut state, fgen, |_, _|{}), 9, 8, 1000).recover().unwrap(); let mut trace: Vec = Vec::new(); let mut canvas = Canvas::new(1000); - run(n, m, k, &mut canvas, &mut wal, &mut trace, &mut rng).unwrap(); - WALLoader::new(common::WALStoreEmul::new(&mut state, common::ZeroFailGen), 9, 8, 1000).recover().unwrap(); + run(n, m, k, &mut canvas, &mut wal, &mut trace, &mut rng); //.unwrap(); + let mut wal = WALLoader::new(WALStoreEmul::new(&mut state, common::ZeroFailGen, |payload, wal_off|{}), 9, 8, 1000); + assert!(check(&mut canvas, &mut wal, &trace)); } -- cgit v1.2.3-70-g09d2