Writes out contents of the cache.
This commit is contained in:
parent
8db7a6d9bf
commit
947adf6c1d
@ -1,7 +1,7 @@
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
use async_std::{
|
use async_std::{
|
||||||
fs::{create_dir, write},
|
fs::{create_dir, read, write},
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
@ -69,7 +69,7 @@ impl MoreThanText {
|
|||||||
async fn add_entry(&self, entry: CacheEntry) -> String {
|
async fn add_entry(&self, entry: CacheEntry) -> String {
|
||||||
let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
|
let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
|
||||||
let file = Path::new(&self.dir).join(&id);
|
let file = Path::new(&self.dir).join(&id);
|
||||||
write(file, "a").await.unwrap();
|
write(file, entry.to_bytes()).await.unwrap();
|
||||||
let mut cache = self.cache.lock().await;
|
let mut cache = self.cache.lock().await;
|
||||||
cache.insert(id.clone(), entry);
|
cache.insert(id.clone(), entry);
|
||||||
return id;
|
return id;
|
||||||
@ -171,15 +171,17 @@ mod cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn retrieve_cache() {
|
async fn store_cache() {
|
||||||
let mtt = MTT::new().await;
|
let mtt = MTT::new().await;
|
||||||
let data = "something";
|
let data = "something";
|
||||||
let expected = CacheEntry::Raw(data.to_string());
|
let expected = CacheEntry::Raw(data.to_string());
|
||||||
let id = mtt.db.add_entry(expected).await;
|
let id = mtt.db.add_entry(expected.clone()).await;
|
||||||
let output = mtt.db.get_entry(&id).await.unwrap();
|
let output = mtt.db.get_entry(&id).await.unwrap();
|
||||||
assert_eq!(output.to_string(), data);
|
assert_eq!(output.to_string(), data);
|
||||||
let dfile = mtt.dir.path().join(DATA).join(&id);
|
let dfile = mtt.dir.path().join(DATA).join(&id);
|
||||||
assert!(dfile.is_file(), "Cache file should exist.");
|
assert!(dfile.is_file(), "Cache file should exist.");
|
||||||
|
let content = read(dfile).await.unwrap();
|
||||||
|
assert_eq!(content, expected.to_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
|
Loading…
Reference in New Issue
Block a user