use crate::cache::{Cache, CacheWrite, Storage};
use crate::errors::*;
use futures::channel::mpsc;
use futures_locks::Mutex;
use std::sync::Arc;
use std::time::Duration;
pub struct MockStorage {
rx: Arc<Mutex<mpsc::UnboundedReceiver<Result<Cache>>>>,
tx: mpsc::UnboundedSender<Result<Cache>>,
}
impl MockStorage {
pub(crate) fn new() -> MockStorage {
let (tx, rx) = mpsc::unbounded();
Self {
tx,
rx: Arc::new(Mutex::new(rx)),
}
}
pub(crate) fn next_get(&self, res: Result<Cache>) {
self.tx.unbounded_send(res).unwrap();
}
}
#[async_trait]
impl Storage for MockStorage {
async fn get(&self, _key: &str) -> Result<Cache> {
let next = self.rx.lock().await.try_next().unwrap();
next.expect("MockStorage get called but no get results available")
}
async fn put(&self, _key: &str, _entry: CacheWrite) -> Result<Duration> {
Ok(Duration::from_secs(0))
}
fn location(&self) -> String {
"Mock Storage".to_string()
}
async fn current_size(&self) -> Result<Option<u64>> {
Ok(None)
}
async fn max_size(&self) -> Result<Option<u64>> {
Ok(None)
}
}