#![cfg(feature = "memory")]
testing_aids::init_tracing!();
use std::time::Duration;
use cachet::{Cache, CacheEntry};
use serial_test::serial;
use testing_aids::TEST_TIMEOUT;
use testing_aids::tracing_logs::write_to_stdout_and_buffer;
use tick::Clock;
#[cfg_attr(miri, ignore)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial]
async fn memory_size_eviction_emits_telemetry() {
let capture = write_to_stdout_and_buffer();
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
.snapshot()
.iter()
.any(|line| line.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.snapshot().join("\n")
);
}