2023-06-17 12:01:58 -04:00
|
|
|
use super::{ErrorCode, MTTError, Store, 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 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn listen(&self, listener: Receiver<String>) {
|
|
|
|
loop {
|
|
|
|
listener.recv().await.unwrap();
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 12:01:58 -04:00
|
|
|
|
|
|
|
pub fn get<S>(&self, id: S) -> Result<Store, MTTError>
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
let idd = id.into();
|
|
|
|
if idd == ENTRY {
|
|
|
|
Ok(Store::new())
|
|
|
|
} else {
|
|
|
|
Err(MTTError::from_code(ErrorCode::IDNotFound(idd)))
|
|
|
|
}
|
|
|
|
}
|
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 store = cache.get(ENTRY).unwrap();
|
|
|
|
let expected: Vec<String> = Vec::new();
|
|
|
|
assert_eq!(store.list(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
match cache.get(id) {
|
|
|
|
Ok(_) => return Err(MTTError::new("should have errored")),
|
|
|
|
Err(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))),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod messages {
|
|
|
|
use super::*;
|
|
|
|
use super::super::start_db;
|
|
|
|
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();
|
|
|
|
in_s.send(ENTRY.to_string()).await.unwrap();
|
2023-05-29 15:42:32 -04:00
|
|
|
}
|
|
|
|
}
|