use crate::common::{get_redis_url_insecure, is_redis_available, setup_logging};
use oxcache::Cache;
use std::time::Duration;
#[tokio::test]
async fn test_batch_write_performance_writes_and_verifies_100_items() {
if !is_redis_available().await {
println!("Skipping test: Redis not available");
return;
}
setup_logging();
let redis_url = get_redis_url_insecure();
let cache: Cache<String, i32> = match Cache::redis(&redis_url).await {
Ok(c) => c,
Err(e) => {
println!("Skipping test: Failed to create Redis cache: {}", e);
return;
}
};
let _ = cache.delete(&"batch_key_50".to_string()).await;
let mut write_errors = 0;
for i in 0..100 {
match cache.set(&format!("batch_key_{}", i), &i).await {
Ok(_) => {}
Err(e) => {
write_errors += 1;
if write_errors > 10 {
println!("Skipping test: Too many write errors: {}", e);
return;
}
}
}
}
if write_errors > 0 {
println!("Warning: {} write errors occurred", write_errors);
}
tokio::time::sleep(Duration::from_millis(500)).await;
let mut value = None;
for attempt in 0..3 {
match cache.get(&"batch_key_50".to_string()).await {
Ok(v) => {
value = v;
break;
}
Err(e) if attempt < 2 => {
tokio::time::sleep(Duration::from_millis(100)).await;
}
Err(e) => {
println!("Failed to read value after 3 attempts: {}", e);
}
}
}
assert!(value.is_some(), "Value should exist after batch write");
assert_eq!(value.unwrap(), 50);
for i in 0..100 {
let _ = cache.delete(&format!("batch_key_{}", i)).await;
}
let _ = cache.shutdown().await;
}