#[cfg(all(test, feature = "hotpath"))]
pub mod tests {
use std::process::Command;
use hotpath::json::{JsonChannelsList, JsonReport};
fn parse_channels(stdout: &str) -> JsonChannelsList {
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");
report.channels.expect("No channels section in report")
}
fn run_example(name: &str) -> String {
let output = Command::new("cargo")
.args([
"run",
"-p",
"test-channels-asc",
"--example",
name,
"--features",
"hotpath",
])
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}",
output.status
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[test]
fn test_wrap_exact_queue_depth() {
let stdout = run_example("wrap_asc");
let channels = parse_channels(&stdout);
let entry = channels
.data
.iter()
.find(|c| c.label == "wrap-queue")
.expect("wrap-queue channel not found");
assert!(entry.wrap, "channel should be endpoint-wrapped");
assert_eq!(entry.sent_count, 50, "expected 50 sends");
assert_eq!(
entry.received_count, 0,
"expected 0 receives at report time"
);
assert_eq!(
entry.queue_size,
Some(50),
"expected exact queue depth of 50"
);
assert_eq!(
entry.max_queue_size,
Some(50),
"expected max queue depth of 50"
);
}
#[test]
fn test_wrap_receiver_dropped_closes() {
let stdout = run_example("wrap_closed_asc");
let channels = parse_channels(&stdout);
let entry = channels
.data
.iter()
.find(|c| c.label == "recv-dropped")
.expect("recv-dropped channel not found");
assert!(entry.wrap, "channel should be endpoint-wrapped");
assert_eq!(
entry.state, "closed",
"expected closed state after receiver drop"
);
}
#[test]
fn test_wrap_receiver_clone_dropped_closes_with_sender_alive() {
let stdout = run_example("wrap_recv_clone_closed_asc");
let channels = parse_channels(&stdout);
let entry = channels
.data
.iter()
.find(|c| c.label == "recv-clone-dropped")
.expect("recv-clone-dropped channel not found");
assert!(entry.wrap, "channel should be endpoint-wrapped");
assert_eq!(
entry.state, "closed",
"expected closed state after all receivers dropped while senders alive"
);
}
#[test]
fn test_wrap_processing_histogram() {
let stdout = run_example("wrap_latency_asc");
let channels = parse_channels(&stdout);
assert_eq!(channels.percentiles, vec![50.0, 95.0]);
let wrap = channels
.data
.iter()
.find(|c| c.label == "wrap-latency")
.expect("wrap-latency channel not found");
assert!(wrap.wrap, "channel should be endpoint-wrapped");
let proc_avg = wrap
.proc_avg
.as_deref()
.expect("wrap channel should report proc_avg in JSON");
assert!(!proc_avg.is_empty(), "proc_avg should not be empty");
assert_ne!(
proc_avg, "0ns",
"expected non-zero send->receive latency (~20ms held in channel)"
);
assert!(
wrap.proc_percentiles.contains_key("p50"),
"expected p50 latency percentile in JSON, got {:?}",
wrap.proc_percentiles
);
assert!(
wrap.proc_percentiles.contains_key("p95"),
"expected p95 latency percentile in JSON, got {:?}",
wrap.proc_percentiles
);
let proxy = channels
.data
.iter()
.find(|c| c.label == "proxy-latency")
.expect("proxy-latency channel not found");
assert!(!proxy.wrap, "channel should be a proxy channel");
assert!(
proxy.proc_avg.is_none(),
"proxy channel must not report proc_avg"
);
assert!(
proxy.proc_percentiles.is_empty(),
"proxy channel must not report latency percentiles"
);
}
}