#[cfg(all(test, feature = "hotpath"))]
pub mod tests {
use hotpath::json::JsonReport;
use std::process::Command;
fn redis_available() -> bool {
let addr = "127.0.0.1:6390".parse().unwrap();
let timeout = std::time::Duration::from_millis(500);
std::net::TcpStream::connect_timeout(&addr, timeout).is_ok()
}
#[test]
fn test_redis_json_output() {
if !redis_available() {
assert!(
std::env::var_os("CI").is_none(),
"no Redis on localhost:6390 - the CI redis service is required"
);
eprintln!(
"skipping test_redis_json_output: no Redis on localhost:6390 \
(start it with `docker compose up -d redis`)"
);
return;
}
let output = Command::new("cargo")
.args([
"run",
"-p",
"test-io",
"--example",
"basic_redis_io",
"--features",
"hotpath",
])
.env("HOTPATH_OUTPUT_FORMAT", "json")
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}\nstderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
let json_start = stdout.find('{').expect("No JSON report in output");
let report: JsonReport = serde_json::Deserializer::from_str(&stdout[json_start..])
.into_iter::<JsonReport>()
.next()
.expect("No JSON value in output")
.expect("Failed to parse JSON report");
let io = report.io.expect("No io section in report");
let redis = io
.data
.iter()
.find(|e| e.label == "redis")
.expect("No 'redis' entry in io section");
assert!(redis.type_name.contains("TcpStream"));
assert_eq!(redis.write.count, 4);
assert_eq!(redis.write.bytes, (49 + 31 + 14 + 31) as u64);
assert_eq!(redis.write.errors, 0);
assert!(redis.read.count >= 4);
assert_eq!(redis.read.bytes, (5 + 18 + 7 + 4) as u64);
assert_eq!(redis.read.errors, 0);
assert!(redis.read.total_ns > 0, "Reads should be timed");
}
}