#[cfg(test)]
pub mod tests {
use std::process::Command;
use tokio::time::{sleep, Duration};
#[test]
fn test_basic_output() {
let output = Command::new("cargo")
.args([
"run",
"-p",
"channels-console-test",
"--example",
"basic",
"--features",
"channels-console",
])
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}",
output.status
);
assert!(!output.stderr.is_empty(), "Stderr is empty");
let all_expected = [
"examples/basic.rs",
"hello-there",
"unbounded",
"bounded[10]",
"oneshot",
"notified",
];
let stdout = String::from_utf8_lossy(&output.stdout);
for expected in all_expected {
assert!(
stdout.contains(expected),
"Expected:\n{expected}\n\nGot:\n{stdout}",
);
}
}
#[tokio::test]
async fn test_metrics_endpoint() {
let mut child = tokio::process::Command::new("cargo")
.args([
"run",
"-p",
"channels-console-test",
"--example",
"basic",
"--features",
"channels-console",
])
.spawn()
.expect("Failed to spawn command");
let mut json_text = String::new();
let mut last_error = None;
for _attempt in 0..4 {
sleep(Duration::from_millis(500)).await;
match ureq::get("http://127.0.0.1:6770/metrics").call() {
Ok(response) => {
json_text = response
.into_string()
.expect("Failed to read response body");
last_error = None;
break;
}
Err(e) => {
last_error = Some(format!("Request error: {}", e));
}
}
}
if let Some(error) = last_error {
panic!("Failed after 4 retries: {}", error);
}
let all_expected = ["basic.rs", "hello-there"];
for expected in all_expected {
assert!(
json_text.contains(expected),
"Expected:\n{expected}\n\nGot:\n{json_text}",
);
}
child.kill().await.expect("Failed to kill child process");
}
#[test]
fn test_basic_json_output() {
let output = Command::new("cargo")
.args([
"run",
"-p",
"channels-console-test",
"--example",
"basic_json",
"--features",
"channels-console",
])
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}",
output.status
);
let all_expected = [
"\"label\": \"examples/basic_json.rs:",
"\"label\": \"hello-there\"",
];
let stdout = String::from_utf8_lossy(&output.stdout);
for expected in all_expected {
assert!(
stdout.contains(expected),
"Expected:\n{expected}\n\nGot:\n{stdout}",
);
}
}
#[test]
fn test_closed_channels_output() {
let output = Command::new("cargo")
.args([
"run",
"-p",
"channels-console-test",
"--example",
"closed",
"--features",
"channels-console",
])
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}",
output.status
);
let stdout = String::from_utf8_lossy(&output.stdout);
let closed_count = stdout.matches("| closed").count();
assert_eq!(
closed_count, 2,
"Expected 'closed' state to appear 2 times in table (bounded and unbounded), found {}.\nOutput:\n{}",
closed_count, stdout
);
let notified_count = stdout.matches("| notified").count();
assert_eq!(
notified_count, 1,
"Expected 'notified' state to appear 1 time in table (oneshot), found {}.\nOutput:\n{}",
notified_count, stdout
);
}
#[test]
fn test_oneshot_closed_output() {
let output = Command::new("cargo")
.args([
"run",
"-p",
"channels-console-test",
"--example",
"oneshot_closed",
"--features",
"channels-console",
])
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
output.status.success(),
"Command failed with status: {}\nStdout:\n{}\nStderr:\n{}",
output.status,
stdout,
stderr
);
let all_expected = ["| closed |", "oneshot_closed.rs:"];
for expected in all_expected {
assert!(
stdout.contains(expected),
"Expected:\n{expected}\n\nGot:\n{stdout}",
);
}
}
}