use std::time::Duration;
use amalgam::{Cache, CacheEvent, EntryOptions};
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_or_set_value_sets_then_returns_existing() {
let cache: Cache<i32> = Cache::new();
let first = cache.get_or_set_value("k", 7, None).await.unwrap();
assert_eq!(first, 7);
let second = cache.get_or_set_value("k", 999, None).await.unwrap();
assert_eq!(second, 7);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn size_eviction_emits_eviction_event() {
let cache: Cache<i32> = Cache::builder().max_capacity(1).build();
let mut events = cache.events().subscribe();
let long = EntryOptions::new(Duration::from_secs(3600));
for i in 0..20 {
cache
.set_full(format!("k{i}"), i, Some(long.clone()), Box::from([]))
.await;
}
cache.run_pending_tasks().await;
let mut saw_eviction = false;
for _ in 0..64 {
match events.try_recv() {
Ok(CacheEvent::Eviction { .. }) => {
saw_eviction = true;
break;
}
Ok(_) => {}
Err(_) => break,
}
}
assert!(
saw_eviction,
"a size-based eviction fired an Eviction event"
);
}