Browse Source

Add simple test for cache

master
Adrian Heine 2 years ago
parent
commit
e40e65bd62
  1. 77
      src/setup/cache.rs

77
src/setup/cache.rs

@ -74,3 +74,80 @@ where
.map_err(std::convert::Into::into) .map_err(std::convert::Into::into)
} }
} }
#[cfg(test)]
mod test {
use super::{Add, AddResult, Cache, Logger};
use crate::async_utils::{join, run};
use crate::resources::{FromArtifact, FromResource, Resource};
use async_trait::async_trait;
use std::fmt::Debug;
use std::rc::{Rc, Weak};
#[derive(Debug, PartialEq, Eq, Hash)]
struct TestResource<T>(&'static str, T);
impl<T> Resource for TestResource<T> {
type Artifact = ();
}
#[derive(Debug, Hash, PartialEq, Eq)]
enum Resources {
A(Rc<TestResource<&'static str>>),
B(Rc<TestResource<()>>),
}
impl FromResource<TestResource<&'static str>> for Resources {
fn from_resource(
inner: &Rc<TestResource<&'static str>>,
) -> (Self, Weak<TestResource<&'static str>>) {
(Self::A(Rc::clone(&inner)), Rc::downgrade(&inner))
}
}
impl FromResource<TestResource<()>> for Resources {
fn from_resource(inner: &Rc<TestResource<()>>) -> (Self, Weak<TestResource<()>>) {
(Self::B(Rc::clone(&inner)), Rc::downgrade(&inner))
}
}
#[derive(Clone)]
struct Artifacts;
impl<V> FromArtifact<TestResource<V>> for Artifacts {
fn from_artifact(_from: ()) -> Self {
Self
}
#[allow(clippy::unused_unit)]
fn into_artifact(self) -> () {
#[allow(clippy::unused_unit)]
()
}
}
struct Inner;
#[async_trait(?Send)]
impl<T: Debug + 'static> Add<TestResource<T>> for Inner {
async fn add(
&self,
logger: &Rc<Logger>,
resource: Rc<TestResource<T>>,
force_run: bool,
) -> AddResult<TestResource<T>> {
Ok(((), resource.0 != "reached"))
}
}
#[test]
fn test() {
let log = Rc::new(slog::Logger::root(slog::Discard, slog::o!()));
let cache: Cache<_, Resources, Artifacts> = Cache::new(Inner);
run(async {
let reached = cache.add(&log, Rc::new(TestResource("reached", ())), false);
let a = cache.add(&log, Rc::new(TestResource("a", ())), false);
let b = cache.add(&log, Rc::new(TestResource("b", ())), false);
let (reached, a, b) = join!(reached, a, b);
assert_eq!(((), false), reached.unwrap());
assert_eq!(((), true), a.unwrap());
assert_eq!(((), true), b.unwrap());
})
}
}
Loading…
Cancel
Save