130 lines
3.6 KiB
Rust
Raw Normal View History

2023-06-20 11:51:32 -04:00
use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
2023-05-29 15:42:32 -04:00
use async_std::{channel::Receiver, path::PathBuf};
pub struct Cache;
impl Cache {
pub async fn new<P>(_dir: P) -> Self
where
P: Into<PathBuf>,
{
Self {}
}
2023-06-20 11:51:32 -04:00
pub async fn listen(&self, listener: Receiver<ToCache>) {
2023-05-29 15:42:32 -04:00
loop {
2023-06-20 11:51:32 -04:00
match listener.recv().await.unwrap() {
ToCache::Get(data) => {
data.result.send(self.get(data.id)).await.unwrap();
}
}
2023-05-29 15:42:32 -04:00
}
}
2023-06-17 12:01:58 -04:00
2023-06-20 11:51:32 -04:00
pub fn get<S>(&self, id: S) -> FromCache
2023-06-17 12:01:58 -04:00
where
S: Into<String>,
{
let idd = id.into();
if idd == ENTRY {
2023-06-20 11:51:32 -04:00
FromCache::Str(Store::new())
2023-06-17 12:01:58 -04:00
} else {
2023-06-20 11:51:32 -04:00
FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd)))
2023-06-17 12:01:58 -04:00
}
}
2023-05-29 15:42:32 -04:00
}
#[cfg(test)]
mod engine {
use super::*;
use tempfile::tempdir;
#[async_std::test]
2023-06-17 12:01:58 -04:00
async fn get_entry() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let expected: Vec<String> = Vec::new();
2023-06-20 11:51:32 -04:00
let result = cache.get(ENTRY);
match result {
FromCache::Str(store) => assert_eq!(store.list(), expected),
_ => assert!(false, "{:?} should be FromCache::Str", result),
}
2023-06-17 12:01:58 -04:00
}
#[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 {
2023-06-20 11:51:32 -04:00
let output = cache.get(id);
match output {
FromCache::Error(err) => match err.code {
2023-06-17 12:01:58 -04:00
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))),
},
2023-06-20 11:51:32 -04:00
_ => {
return Err(MTTError::new(format!(
"{:?} is not FromCache::Error",
output
)))
}
2023-06-17 12:01:58 -04:00
}
}
Ok(())
}
}
#[cfg(test)]
mod messages {
2023-06-20 11:51:32 -04:00
use super::{
super::{start_db, CacheGet},
*,
};
use async_std::channel::unbounded;
2023-06-17 12:01:58 -04:00
use tempfile::tempdir;
#[async_std::test]
async fn get_the_store() {
2023-05-29 15:42:32 -04:00
let dir = tempdir().unwrap();
2023-06-17 12:01:58 -04:00
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
2023-06-20 11:51:32 -04:00
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),
}
2023-05-29 15:42:32 -04:00
}
}