#![cfg(feature = "redis")]
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> = match Cache::redis(redis_url).await {
Ok(c) => c,
Err(e) => {
println!("Skipping test: Failed to create cache (TLS required): {}", e);
return;
}
};
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;
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::redis(redis_url).await;
if let Ok(cache) = cache_result {
let operation_result = cache.get(&"test_key".to_string()).await;
assert!(
operation_result.is_err(),
"Should fail to connect to invalid Redis on first operation"
);
} else {
println!("Cache creation failed as expected: {:?}", cache_result);
}
}