From 9c36ae1c7b067ed8f1898f59f28dd99a2bba222c Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Mon, 3 Jul 2023 14:30:41 +0200 Subject: [PATCH] Test mux store... kinda --- src/origin/store.rs | 18 +++++ src/origin/store/mux.rs | 142 ++++++++++++++++++++++++++++++---------- 2 files changed, 124 insertions(+), 36 deletions(-) diff --git a/src/origin/store.rs b/src/origin/store.rs index 9070d57..db5a279 100644 --- a/src/origin/store.rs +++ b/src/origin/store.rs @@ -50,3 +50,21 @@ pub trait Store { fn get(&self, descr: origin::Descr) -> Result, GetError>; fn all_descrs(&self) -> Result, AllDescrsError>; } + +pub fn new_mock() -> sync::Arc> { + sync::Arc::new(sync::Mutex::new(MockStore::new())) +} + +impl Store for sync::Arc> { + fn sync(&self, descr: origin::Descr, limits: Limits) -> Result<(), SyncError> { + self.lock().unwrap().sync(descr, limits) + } + + fn get(&self, descr: origin::Descr) -> Result, GetError> { + self.lock().unwrap().get(descr) + } + + fn all_descrs(&self) -> Result, AllDescrsError> { + self.lock().unwrap().all_descrs() + } +} diff --git a/src/origin/store/mux.rs b/src/origin/store/mux.rs index 7afe201..f8fda8a 100644 --- a/src/origin/store/mux.rs +++ b/src/origin/store/mux.rs @@ -28,13 +28,13 @@ where { fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> { (self.mapping_fn)(&descr) - .or_unexpected_while(format!("fetching store for {:?}", &descr))? + .or_unexpected_while(format!("mapping {:?} to store", &descr))? .sync(descr, limits) } fn get(&self, descr: origin::Descr) -> Result, store::GetError> { (self.mapping_fn)(&descr) - .or_unexpected_while(format!("fetching store for {:?}", &descr))? + .or_unexpected_while(format!("mapping {:?} to store", &descr))? .get(descr) } @@ -49,50 +49,120 @@ where } } -/* #[cfg(test)] mod tests { use crate::origin::{self, store}; use mockall::predicate; - use std::{cell, rc}; + use std::sync; + + struct Harness { + descr_a: origin::Descr, + descr_b: origin::Descr, + descr_unknown: origin::Descr, + store_a: sync::Arc>, + store_b: sync::Arc>, + store: Box, + } + + impl Harness { + fn new() -> Harness { + let descr_a = origin::Descr::Git { + url: "A".to_string(), + branch_name: "A".to_string(), + }; + + let descr_b = origin::Descr::Git { + url: "B".to_string(), + branch_name: "B".to_string(), + }; + + let store_a = store::new_mock(); + let store_b = store::new_mock(); + + Harness { + descr_a: descr_a.clone(), + descr_b: descr_b.clone(), + descr_unknown: origin::Descr::Git { + url: "X".to_string(), + branch_name: "X".to_string(), + }, + store_a: store_a.clone(), + store_b: store_b.clone(), + store: Box::from(super::Store::new( + { + let store_a = store_a.clone(); + let store_b = store_b.clone(); + move |descr| match descr { + &origin::Descr::Git { ref url, .. } if url == "A" => { + Some(store_a.clone()) + } + &origin::Descr::Git { ref url, .. } if url == "B" => { + Some(store_b.clone()) + } + _ => None, + } + }, + vec![store_a.clone(), store_b.clone()], + )), + } + } + } #[test] - fn basic() { - let descrA = origin::Descr::Git { - url: "A".to_string(), - branch_name: "A".to_string(), - }; + fn sync() { + let h = Harness::new(); - //let descrB = origin::Descr::Git { - // url: "B".to_string(), - // branch_name: "B".to_string(), - //}; - - let sA_mock = rc::Rc::new(store::MockStore::new()); - - //let mut sB = store::MockStore::new(); - - let s = { - let sA: rc::Rc = sA_mock.clone(); - let sA = sA.as_ref(); - - super::new( - |descr| match descr { - &origin::Descr::Git { ref url, .. } if url == "A" => Some(sA), - //&origin::Descr::Git { ref url, .. } if url == "B" => Some(sB), - _ => None, - }, - //vec![sA], - ) - }; - - sA_mock + h.store_a + .lock() + .unwrap() .expect_sync() - .with(predicate::eq(descrA.clone()), predicate::always()) + .with(predicate::eq(h.descr_a.clone()), predicate::always()) .times(1) .return_const(Ok::<(), store::SyncError>(())); - assert_eq!(Ok(()), s.sync(descrA, store::Limits {})) + assert_eq!(Ok(()), h.store.sync(h.descr_a.clone(), store::Limits {})); + + h.store_b + .lock() + .unwrap() + .expect_sync() + .with(predicate::eq(h.descr_b.clone()), predicate::always()) + .times(1) + .return_const(Ok::<(), store::SyncError>(())); + + assert_eq!(Ok(()), h.store.sync(h.descr_b.clone(), store::Limits {})); + + assert!(h + .store + .sync(h.descr_unknown.clone(), store::Limits {}) + .is_err()); + } + + #[test] + fn all_descrs() { + let h = Harness::new(); + + h.store_a + .lock() + .unwrap() + .expect_all_descrs() + .times(1) + .return_const(Ok::, store::AllDescrsError>(vec![h + .descr_a + .clone()])); + + h.store_b + .lock() + .unwrap() + .expect_all_descrs() + .times(1) + .return_const(Ok::, store::AllDescrsError>(vec![h + .descr_b + .clone()])); + + assert_eq!( + Ok(vec![h.descr_a.clone(), h.descr_b.clone()]), + h.store.all_descrs(), + ) } } -*/