#![cfg(feature = "memory")]
use std::time::Duration;
use cachet::{Cache, CacheEntry};
use testing_aids::{LogCapture, TEST_TIMEOUT};
use tick::Clock;
use tracing_subscriber::Registry;
use tracing_subscriber::layer::SubscriberExt;
#[cfg_attr(miri, ignore)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn memory_size_eviction_emits_telemetry() {
let capture = LogCapture::new();
let subscriber = Registry::default().with(tracing_subscriber::fmt::layer().with_writer(capture.clone()).with_ansi(false));
tracing::subscriber::set_global_default(subscriber).expect("no other global subscriber should be installed in this test binary");
let clock = Clock::new_tokio();
let cache: Cache<String, i32> = Cache::builder::<String, i32>(clock)
.name("eviction-test")
.enable_logs()
.memory_with(|b| b.max_capacity(2).with_eviction_telemetry())
.build();
let deadline = std::time::Instant::now() + TEST_TIMEOUT;
let mut i: i32 = 0;
while std::time::Instant::now() < deadline {
for _ in 0..256 {
cache.insert(format!("k{i}"), CacheEntry::new(i)).await.unwrap();
i += 1;
}
if capture.output().contains(cachet::telemetry::attributes::EVENT_EVICTION) {
return;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
panic!(
"expected `{}` event after exceeding max_capacity; captured output:\n{}",
cachet::telemetry::attributes::EVENT_EVICTION,
capture.output()
);
}