Continuing channel setup.

This commit is contained in:
2023-06-17 12:01:58 -04:00
parent 30ea8d978c
commit dd1c45ddbe
2 changed files with 81 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
use super::{ErrorCode, MTTError, Store, ENTRY};
use async_std::{channel::Receiver, path::PathBuf};
pub struct Cache;
@@ -15,6 +16,18 @@ impl Cache {
listener.recv().await.unwrap();
}
}
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)))
}
}
}
#[cfg(test)]
@@ -23,8 +36,50 @@ mod engine {
use tempfile::tempdir;
#[async_std::test]
async fn create() {
async fn get_entry() {
let dir = tempdir().unwrap();
Cache::new(dir.path()).await;
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() {
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();
}
}