use crate::common;
use common::{cleanup_service, generate_unique_service_name, is_redis_available, setup_logging};
use oxcache::Cache;
use std::time::Instant;
#[tokio::test]
async fn test_backfill_latency() {
if !is_redis_available().await {
println!("跳过 test_backfill_latency: Redis不可用");
return;
}
setup_logging();
let service_name = generate_unique_service_name("perf_backfill_test");
let redis_url = "redis://127.0.0.1:6379";
let cache: Cache<String, String> = Cache::tiered(1000, redis_url)
.await
.expect("Failed to create tiered cache");
let key = "perf_key".to_string();
let val = "perf_value".to_string();
cache.set(&key, &val).await.unwrap();
cache.clear().await.unwrap();
cache.set(&key, &val).await.unwrap();
let start = Instant::now();
let res: Option<String> = cache.get(&key).await.unwrap();
let duration = start.elapsed();
assert_eq!(res, Some(val));
println!("Backfill latency: {:?}", duration);
if duration.as_millis() >= 5 {
println!(
"WARNING: Backfill latency {}ms exceeds 5ms target",
duration.as_millis()
);
} else {
assert!(duration.as_millis() < 5, "Backfill latency too high");
}
cache.shutdown().await.expect("Shutdown failed");
cleanup_service(&service_name).await;
}
#[tokio::test]
async fn test_redis_outage_resilience() {
let redis_url = "redis://127.0.0.1:12345";
let cache_result: Result<Cache<String, String>, oxcache::CacheError> =
Cache::tiered(100, redis_url).await;
assert!(
cache_result.is_err(),
"Should fail to connect to invalid Redis"
);
let error = cache_result.unwrap_err();
println!("Expected error: {:?}", error);
}