Got the client and engine connecting.

This commit is contained in:
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};
pub struct Cache;
@@ -11,21 +11,25 @@ impl Cache {
Self {}
}
pub async fn listen(&self, listener: Receiver<String>) {
pub async fn listen(&self, listener: Receiver<ToCache>) {
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
S: Into<String>,
{
let idd = id.into();
if idd == ENTRY {
Ok(Store::new())
FromCache::Str(Store::new())
} 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() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let store = cache.get(ENTRY).unwrap();
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]
@@ -50,9 +57,9 @@ mod engine {
let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"];
for id in ids {
match cache.get(id) {
Ok(_) => return Err(MTTError::new("should have errored")),
Err(err) => match err.code {
let output = cache.get(id);
match output {
FromCache::Error(err) => match err.code {
ErrorCode::IDNotFound(_) => {
assert!(
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 FromCache::Error",
output
)))
}
}
}
Ok(())
@@ -71,8 +84,11 @@ mod engine {
#[cfg(test)]
mod messages {
use super::*;
use super::super::start_db;
use super::{
super::{start_db, CacheGet},
*,
};
use async_std::channel::unbounded;
use tempfile::tempdir;
#[async_std::test]
@@ -80,6 +96,34 @@ mod messages {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
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),
}
}
}