add tests and mocks

This commit is contained in:
mpostma
2021-03-23 16:19:01 +01:00
parent 3cc3637e2d
commit 46293546f3
9 changed files with 273 additions and 23 deletions

View File

@ -23,7 +23,7 @@ impl<D, S, I> UpdateActor<D, S, I>
where
D: AsRef<[u8]> + Sized + 'static,
S: UpdateStoreStore,
I: IndexActorHandle + 'static,
I: IndexActorHandle + Clone + Send + Sync + 'static,
{
pub fn new(
store: S,

View File

@ -24,7 +24,7 @@ where
update_store_size: usize,
) -> anyhow::Result<Self>
where
I: IndexActorHandle + 'static,
I: IndexActorHandle + Clone + Send + Sync + 'static,
{
let path = path.as_ref().to_owned().join("updates");
let (sender, receiver) = mpsc::channel(100);

View File

@ -23,6 +23,9 @@ pub type Result<T> = std::result::Result<T, UpdateError>;
type UpdateStore = update_store::UpdateStore<UpdateMeta, UpdateResult, String>;
type PayloadData<D> = std::result::Result<D, Box<dyn std::error::Error + Sync + Send + 'static>>;
#[cfg(test)]
use mockall::automock;
#[derive(Debug, Error)]
pub enum UpdateError {
#[error("error with update: {0}")]
@ -34,6 +37,7 @@ pub enum UpdateError {
}
#[async_trait::async_trait]
#[cfg_attr(test, automock(type Data=Vec<u8>;))]
pub trait UpdateActorHandle {
type Data: AsRef<[u8]> + Sized + 'static + Sync + Send;

View File

@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use uuid::Uuid;
use tokio::sync::RwLock;
use tokio::fs;
use tokio::sync::RwLock;
use uuid::Uuid;
use super::{Result, UpdateError, UpdateStore};
use crate::index_controller::IndexActorHandle;
use super::{UpdateStore, UpdateError, Result};
#[async_trait::async_trait]
pub trait UpdateStoreStore {
@ -25,11 +25,7 @@ pub struct MapUpdateStoreStore<I> {
}
impl<I: IndexActorHandle> MapUpdateStoreStore<I> {
pub fn new(
index_handle: I,
path: impl AsRef<Path>,
update_store_size: usize,
) -> Self {
pub fn new(index_handle: I, path: impl AsRef<Path>, update_store_size: usize) -> Self {
let db = Arc::new(RwLock::new(HashMap::new()));
let path = path.as_ref().to_owned();
Self {
@ -42,7 +38,10 @@ impl<I: IndexActorHandle> MapUpdateStoreStore<I> {
}
#[async_trait::async_trait]
impl<I: IndexActorHandle + 'static> UpdateStoreStore for MapUpdateStoreStore<I> {
impl<I> UpdateStoreStore for MapUpdateStoreStore<I>
where
I: IndexActorHandle + Clone + Send + Sync + 'static,
{
async fn get_or_create(&self, uuid: Uuid) -> Result<Arc<UpdateStore>> {
match self.db.write().await.entry(uuid) {
Entry::Vacant(e) => {