Implement tests

This commit is contained in:
Valentin Brandl 2019-08-07 22:19:41 +02:00
parent 1e806ef4f0
commit 501257684e
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -148,10 +148,83 @@ impl<T> CacheEntry<T> {
CacheEntry(Instant::now() + duration, value) CacheEntry(Instant::now() + duration, value)
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Cache, CacheResult};
use std::time::Duration;
#[test] #[test]
fn it_works() { fn always_invalid() {
assert_eq!(2 + 2, 4); let key = 0;
let value = 1;
let mut cache = Cache::new(Duration::from_secs(0));
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Invalid, cache.get(&key));
}
#[test]
fn valid() {
let key = 0;
let value = 1;
let mut cache = Cache::new(Duration::from_secs(100));
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Cached(&value), cache.get(&key));
}
#[test]
fn wait_for_invalidation() {
let key = 0;
let value = 1;
let dur = Duration::from_millis(500);
let mut cache = Cache::new(dur);
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Cached(&value), cache.get(&key));
std::thread::sleep(dur);
assert_eq!(CacheResult::Invalid, cache.get(&key));
}
#[test]
fn invalidate() {
let key = 0;
let value = 1;
let dur = Duration::from_secs(100);
let mut cache = Cache::new(dur);
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Cached(&value), cache.get(&key));
assert!(cache.invalidate(&key));
assert_eq!(CacheResult::Empty, cache.get(&key));
}
#[test]
fn invalidate_wait() {
let key = 0;
let value = 1;
let dur = Duration::from_millis(500);
let mut cache = Cache::new(dur);
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Cached(&value), cache.get(&key));
std::thread::sleep(dur);
assert_eq!(CacheResult::Invalid, cache.get(&key));
assert!(cache.invalidate(&key));
assert_eq!(CacheResult::Empty, cache.get(&key));
}
#[test]
fn clear() {
let key = 0;
let value = 1;
let dur = Duration::from_secs(0);
let mut cache = Cache::new(dur);
assert_eq!(CacheResult::Empty, cache.get(&key));
cache.store(key, value);
assert_eq!(CacheResult::Invalid, cache.get(&key));
cache.clear();
assert_eq!(CacheResult::Empty, cache.get(&key));
} }
} }