use std::{path::Path, time::Duration};
use pretty_assertions::assert_eq;
use rho_protocol::{methods, StatusParams, StopParams, StopResponse};
use tempfile::TempDir;
use super::*;
use crate::daemon::{
client, lock,
paths::SocketFound,
shutdown::Shutdown,
test_support::{test_status, StubHost},
};
const SHUTDOWN_DEADLINE: Duration = Duration::from_secs(10);
fn prepared_paths(root: &Path) -> DaemonPaths {
let paths = DaemonPaths::resolve(root, |_| None).unwrap();
paths.prepare_runtime_dir().unwrap();
paths
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn daemon_serves_status_and_cleans_up_on_stop() {
let root = TempDir::new().unwrap();
let paths = prepared_paths(root.path());
let bound = bind(&paths, lock::acquire(&paths).unwrap()).unwrap();
let shutdown = Shutdown::default();
let server = tokio::spawn(async move {
bound
.serve(StubHost::new(&shutdown), shutdown)
.await
.release()
});
assert_eq!(paths.inspect_socket(), Ok(SocketFound::Private));
let client::Connection { client, hello, .. } =
client::connect(&paths, client::ConnectAs::Command.hello())
.await
.unwrap();
assert_eq!(hello.daemon.pid, std::process::id());
assert_eq!(hello.protocol, PROTOCOL_VERSION);
let status = client
.request::<methods::Status>(&StatusParams {})
.await
.unwrap();
assert_eq!(status, test_status());
assert_eq!(
lock::read_record(&paths).map(|record| record.pid),
Some(std::process::id())
);
let ack = client
.request::<methods::Stop>(&StopParams {})
.await
.unwrap();
assert_eq!(ack, StopResponse {});
tokio::time::timeout(SHUTDOWN_DEADLINE, server)
.await
.expect("server stops after rho/daemon/stop")
.unwrap()
.unwrap();
assert_eq!(paths.inspect_socket(), Ok(SocketFound::Missing));
assert!(!paths.info_file.exists(), "info file removed");
assert!(
lock::wait_until_released(&paths, SHUTDOWN_DEADLINE)
.await
.unwrap()
.is_some(),
"lock released"
);
}