Got the client and engine connecting.

This commit is contained in:
Jeff Baskin 2023-06-20 11:51:32 -04:00
parent dd1c45ddbe
commit ecfc8fdf90
3 changed files with 90 additions and 20 deletions

View File

@ -1,4 +1,4 @@
use super::{ErrorCode, MTTError, Store, ENTRY}; use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
use async_std::{channel::Receiver, path::PathBuf}; use async_std::{channel::Receiver, path::PathBuf};
pub struct Cache; pub struct Cache;
@ -11,21 +11,25 @@ impl Cache {
Self {} Self {}
} }
pub async fn listen(&self, listener: Receiver<String>) { pub async fn listen(&self, listener: Receiver<ToCache>) {
loop { loop {
listener.recv().await.unwrap(); match listener.recv().await.unwrap() {
ToCache::Get(data) => {
data.result.send(self.get(data.id)).await.unwrap();
}
}
} }
} }
pub fn get<S>(&self, id: S) -> Result<Store, MTTError> pub fn get<S>(&self, id: S) -> FromCache
where where
S: Into<String>, S: Into<String>,
{ {
let idd = id.into(); let idd = id.into();
if idd == ENTRY { if idd == ENTRY {
Ok(Store::new()) FromCache::Str(Store::new())
} else { } else {
Err(MTTError::from_code(ErrorCode::IDNotFound(idd))) FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd)))
} }
} }
} }
@ -39,9 +43,12 @@ mod engine {
async fn get_entry() { async fn get_entry() {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await; let cache = Cache::new(dir.path()).await;
let store = cache.get(ENTRY).unwrap();
let expected: Vec<String> = Vec::new(); let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected); 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_std::test]
@ -50,9 +57,9 @@ mod engine {
let cache = Cache::new(dir.path()).await; let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"]; let ids = ["bad1", "bad2"];
for id in ids { for id in ids {
match cache.get(id) { let output = cache.get(id);
Ok(_) => return Err(MTTError::new("should have errored")), match output {
Err(err) => match err.code { FromCache::Error(err) => match err.code {
ErrorCode::IDNotFound(_) => { ErrorCode::IDNotFound(_) => {
assert!( assert!(
err.to_string().contains(id), err.to_string().contains(id),
@ -63,6 +70,12 @@ mod engine {
} }
_ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))), _ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))),
}, },
_ => {
return Err(MTTError::new(format!(
"{:?} is not FromCache::Error",
output
)))
}
} }
} }
Ok(()) Ok(())
@ -71,8 +84,11 @@ mod engine {
#[cfg(test)] #[cfg(test)]
mod messages { mod messages {
use super::*; use super::{
use super::super::start_db; super::{start_db, CacheGet},
*,
};
use async_std::channel::unbounded;
use tempfile::tempdir; use tempfile::tempdir;
#[async_std::test] #[async_std::test]
@ -80,6 +96,34 @@ mod messages {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap(); let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone(); let in_s = mtt.to_cache.clone();
in_s.send(ENTRY.to_string()).await.unwrap(); 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),
}
} }
} }

View File

@ -13,6 +13,23 @@ use store::Store;
const ENTRY: &str = "EntryPoint"; const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
pub struct CacheGet {
id: String,
result: Sender<FromCache>,
}
#[derive(Debug)]
pub enum ToCache {
Get(CacheGet),
}
#[derive(Debug)]
pub enum FromCache {
Str(Store),
Error(MTTError),
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Data<D> { struct Data<D> {
id: Option<String>, id: Option<String>,
@ -33,20 +50,29 @@ impl<D> Data<D> {
#[derive(Clone)] #[derive(Clone)]
pub struct MoreThanText { pub struct MoreThanText {
to_cache: Sender<String>, to_cache: Sender<ToCache>,
entry: Data<Store>, entry: Data<Store>,
} }
impl MoreThanText { impl MoreThanText {
fn new(to_cache: Sender<String>) -> Self { fn new(to_cache: Sender<ToCache>) -> Self {
Self { Self {
to_cache: to_cache, to_cache: to_cache,
entry: Data::from_id(ENTRY), entry: Data::from_id(ENTRY),
} }
} }
async fn session(&self) -> Store { async fn session(&self) -> Result<Store, MTTError> {
Store::new() let (s, r) = unbounded();
let msg = CacheGet {
id: ENTRY.to_string(),
result: s,
};
self.to_cache.send(ToCache::Get(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Str(store) => Ok(store),
FromCache::Error(err) => Err(err),
}
} }
} }
@ -61,7 +87,7 @@ mod mtt {
let mtt = start_db(dir.path()).await.unwrap(); let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.entry.id, Some(ENTRY.to_string())); assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
assert!(mtt.entry.data.is_none()); assert!(mtt.entry.data.is_none());
let store = mtt.session().await; let store = mtt.session().await.unwrap();
let expected: Vec<String> = Vec::new(); let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected); assert_eq!(store.list(), expected);
} }

View File

@ -1,4 +1,4 @@
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct Store; pub struct Store;
impl Store { impl Store {