#![allow(dead_code)]
use std::path::PathBuf;
use std::time::Duration;
use kindling_client::{ClientConfig, Spawner, Transport};
use kindling_server::{serve, ServerConfig};
use tempfile::TempDir;
use tokio::task::JoinHandle;
pub fn schema_version_u32() -> u32 {
kindling_store::schema_version().version as u32
}
pub struct TestDaemon {
pub socket_path: PathBuf,
pub kindling_home: PathBuf,
_home: TempDir,
handle: JoinHandle<Result<(), kindling_server::ServerError>>,
}
impl TestDaemon {
pub async fn start() -> Self {
let (config, home, socket_path) = temp_server_config();
let handle = tokio::spawn(async move { serve(config).await });
wait_for_socket(&socket_path).await;
Self {
socket_path,
kindling_home: home.path().to_path_buf(),
_home: home,
handle,
}
}
pub fn client(&self, project_root: &str) -> kindling_client::Client {
let cfg = ClientConfig {
socket_path: self.socket_path.clone(),
port_path: self.kindling_home.join("k.port"),
project_root: project_root.to_string(),
expected_schema_version: schema_version_u32(),
connect_timeout: Duration::from_secs(2),
poll_interval: Duration::from_millis(10),
spawn: Spawner::custom(|| panic!("spawner must not be called when daemon is running")),
transport: Transport::Uds,
spawn_log_path: None,
};
kindling_client::Client::with_config(cfg)
}
pub fn client_with_schema(&self, project_root: &str, expected: u32) -> kindling_client::Client {
let cfg = ClientConfig {
socket_path: self.socket_path.clone(),
port_path: self.kindling_home.join("k.port"),
project_root: project_root.to_string(),
expected_schema_version: expected,
connect_timeout: Duration::from_secs(2),
poll_interval: Duration::from_millis(10),
spawn: Spawner::custom(|| panic!("spawner must not be called when daemon is running")),
transport: Transport::Uds,
spawn_log_path: None,
};
kindling_client::Client::with_config(cfg)
}
}
pub fn temp_server_config() -> (ServerConfig, TempDir, PathBuf) {
let home = tempfile::tempdir().unwrap();
let home_path = home.path().to_path_buf();
let socket_path = home_path.join("k.sock");
let config = ServerConfig {
socket_path: socket_path.clone(),
kindling_home: home_path.clone(),
pid_path: home_path.join("k.pid"),
port_path: home_path.join("k.port"),
idle_timeout: Duration::from_secs(3600),
transport: kindling_server::Transport::default(),
};
(config, home, socket_path)
}
pub async fn wait_for_socket(socket_path: &std::path::Path) {
for _ in 0..400 {
if socket_path.exists() {
return;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
panic!("daemon socket never appeared: {}", socket_path.display());
}