Got the client and engine connecting.
This commit is contained in:
parent
dd1c45ddbe
commit
ecfc8fdf90
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,23 @@ use store::Store;
|
||||
|
||||
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)]
|
||||
struct Data<D> {
|
||||
id: Option<String>,
|
||||
@ -33,20 +50,29 @@ impl<D> Data<D> {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MoreThanText {
|
||||
to_cache: Sender<String>,
|
||||
to_cache: Sender<ToCache>,
|
||||
entry: Data<Store>,
|
||||
}
|
||||
|
||||
impl MoreThanText {
|
||||
fn new(to_cache: Sender<String>) -> Self {
|
||||
fn new(to_cache: Sender<ToCache>) -> Self {
|
||||
Self {
|
||||
to_cache: to_cache,
|
||||
entry: Data::from_id(ENTRY),
|
||||
}
|
||||
}
|
||||
|
||||
async fn session(&self) -> Store {
|
||||
Store::new()
|
||||
async fn session(&self) -> Result<Store, MTTError> {
|
||||
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();
|
||||
assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
|
||||
assert!(mtt.entry.data.is_none());
|
||||
let store = mtt.session().await;
|
||||
let store = mtt.session().await.unwrap();
|
||||
let expected: Vec<String> = Vec::new();
|
||||
assert_eq!(store.list(), expected);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Store;
|
||||
|
||||
impl Store {
|
||||
|
Loading…
Reference in New Issue
Block a user