use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY}; use async_std::{channel::Receiver, path::PathBuf}; pub struct Cache; impl Cache { pub async fn new

(_dir: P) -> Self where P: Into, { Self {} } pub async fn listen(&self, listener: Receiver) { loop { match listener.recv().await.unwrap() { ToCache::Get(data) => { data.result.send(self.get(data.id)).await.unwrap(); } } } } pub fn get(&self, id: S) -> FromCache where S: Into, { let idd = id.into(); if idd == ENTRY { FromCache::Str(Store::new()) } else { FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))) } } } #[cfg(test)] mod engine { use super::*; use tempfile::tempdir; #[async_std::test] async fn get_entry() { let dir = tempdir().unwrap(); let cache = Cache::new(dir.path()).await; let expected: Vec = Vec::new(); let result = cache.get(ENTRY); match result { FromCache::Str(store) => assert_eq!(store.list(), expected), _ => assert!(false, "{:?} should be FromCache::Str", result), } } #[async_std::test] async fn get_bad_entry() -> Result<(), MTTError> { let dir = tempdir().unwrap(); let cache = Cache::new(dir.path()).await; let ids = ["bad1", "bad2"]; for id in ids { let output = cache.get(id); match output { FromCache::Error(err) => match err.code { ErrorCode::IDNotFound(_) => { assert!( err.to_string().contains(id), "Had error: {}, Did not contain: {}", err.to_string(), id ); } _ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))), }, _ => { return Err(MTTError::new(format!( "{:?} is not FromCache::Error", output ))) } } } Ok(()) } } #[cfg(test)] mod messages { use super::{ super::{start_db, CacheGet}, *, }; use async_std::channel::unbounded; use tempfile::tempdir; #[async_std::test] async fn get_the_store() { let dir = tempdir().unwrap(); let mtt = start_db(dir.path()).await.unwrap(); let in_s = mtt.to_cache.clone(); let (out_s, out_r) = unbounded(); let msg = CacheGet { id: ENTRY.to_string(), result: out_s, }; in_s.send(ToCache::Get(msg)).await.unwrap(); let result = out_r.recv().await.unwrap(); match result { FromCache::Str(_) => (), _ => assert!(false, "{:?} is not FromCache::Str", result), } } #[async_std::test] async fn get_bad_id() { let dir = tempdir().unwrap(); let mtt = start_db(dir.path()).await.unwrap(); let in_s = mtt.to_cache.clone(); let (out_s, out_r) = unbounded(); let msg = CacheGet { id: "bad_id!".to_string(), result: out_s, }; in_s.send(ToCache::Get(msg)).await.unwrap(); let output = out_r.recv().await.unwrap(); match output { FromCache::Error(_) => (), _ => assert!(false, "{:?} is not FromCache::Error", output), } } }