use std::{collections::BTreeSet, time::Duration};
use clap::Parser;
use pretty_assertions::assert_eq;
use rho_protocol::{BuildId, Client, ClientHello, ClientInfo, DaemonFeature, DaemonInfo};
use super::{ask_to_stop, rerun_args, AfterStop};
use crate::{
cli::{Cli, Command, DaemonCommand},
daemon::{test_support::StubHost, Ending, Shutdown},
};
#[test]
fn rerun_arguments_run_the_same_daemon_with_the_handoff() {
let handoff = "7:9".to_owned();
let unload_idle_after = Duration::from_millis(1500);
let args =
std::iter::once("rho".into()).chain(rerun_args(unload_idle_after, Some(handoff.clone())));
let parsed = Cli::try_parse_from(args).unwrap().command;
let Some(Command::Daemon {
command:
Some(DaemonCommand::Run {
unload_idle_after: parsed_idle,
exec_handoff: Some(parsed_handoff),
}),
}) = parsed
else {
panic!("not `rho daemon run` with a handoff: {parsed:?}");
};
assert_eq!(
(parsed_idle, parsed_handoff),
(Some(unload_idle_after), handoff)
);
}
#[tokio::test]
async fn a_replacement_stops_a_daemon_that_cannot_be_told_so() {
let notice = || BTreeSet::from([DaemonFeature::StoppingNotice]);
let cases = [
(
"earlier daemon",
BTreeSet::new(),
AfterStop::Replacement,
Ending::Stop,
),
(
"replacement",
notice(),
AfterStop::Replacement,
Ending::Replace,
),
("plain stop", notice(), AfterStop::Nothing, Ending::Stop),
];
for (name, features, after, ending) in cases {
let shutdown = Shutdown::default();
let host = StubHost::naming(&shutdown, features);
let (stream, served) = tokio::io::duplex(64 * 1024);
let info = DaemonInfo {
version: "test".into(),
pid: 0,
build: BuildId::of_this_crate(),
};
tokio::spawn(async move {
rho_protocol::serve_connection(served, &info, host, std::future::pending()).await
});
let hello = ClientHello::command(ClientInfo {
name: "daemon-cli-test".into(),
version: "0".into(),
});
let (client, _, _) = Client::handshake(stream, hello).await.unwrap();
let asked = ask_to_stop(&client, after)
.await
.map_err(|error| error.to_string());
assert_eq!((asked, shutdown.ending()), (Ok(()), ending), "{name}");
}
}