#![cfg(feature = "daemon")]
use running_process::daemon::client::DaemonClient;
use running_process::daemon::paths;
use running_process::daemon::pipe_session::PipeSpawnRequest;
use running_process::daemon::pty_session::PtySpawnRequest;
use running_process::daemon::server::DaemonServer;
use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};
const FAST_TERMINATE_BUDGET: Duration = Duration::from_millis(200);
fn testbin_path(name: &str) -> PathBuf {
let output = Command::new(env!("CARGO"))
.args([
"build",
"-p",
"testbins",
"--bin",
name,
"--message-format=json",
])
.stderr(std::process::Stdio::inherit())
.output()
.expect("cargo build failed");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if !line.contains("\"compiler-artifact\"") || !line.contains(name) {
continue;
}
if let Ok(v) = serde_json::from_str::<serde_json::Value>(line) {
if v["reason"] == "compiler-artifact"
&& v["target"]["kind"]
.as_array()
.is_some_and(|a| a.iter().any(|k| k == "bin"))
{
if let Some(exe) = v["executable"].as_str() {
let p = PathBuf::from(exe);
let deadline = Instant::now() + Duration::from_secs(5);
while !p.exists() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(50));
}
return p;
}
}
}
}
panic!("could not find binary artifact for {name}");
}
fn start_server(scope: &str) -> (tokio::task::JoinHandle<()>, String) {
let socket = paths::socket_path(Some(scope));
let db = paths::db_path(Some(scope)).to_string_lossy().into_owned();
let server = DaemonServer::new(
socket.clone(),
db,
"fast-ctrl-c-test".to_string(),
scope.to_string(),
std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.into_owned(),
)
.expect("DaemonServer::new");
let handle = tokio::spawn(async move {
server.run().await.expect("server.run");
});
(handle, socket)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn pty_terminate_returns_under_fast_budget() {
let scope = format!("fast-pty-{}", line!());
let (_handle, socket) = start_server(&scope);
tokio::time::sleep(Duration::from_millis(300)).await;
let sleeper = tokio::task::spawn_blocking(|| testbin_path("testbin-sleeper"))
.await
.expect("testbin");
let socket_for_test = socket.clone();
tokio::task::spawn_blocking(move || {
let mut client = DaemonClient::connect_to(&socket_for_test).expect("connect");
let req = PtySpawnRequest::new([sleeper.to_string_lossy().into_owned()])
.with_originator("fast-pty");
let session = client.spawn_pty_session(&req).expect("spawn");
let started = Instant::now();
client
.terminate_pty_session(&session.session_id, 2000)
.expect("terminate");
let elapsed = started.elapsed();
assert!(
elapsed < FAST_TERMINATE_BUDGET,
"PTY terminate RPC took {elapsed:?}, expected < {FAST_TERMINATE_BUDGET:?}; \
this is the #130 M4 fast-Ctrl+C invariant"
);
let exit_deadline = Instant::now() + Duration::from_secs(15);
loop {
let listed = client.list_pty_sessions("").expect("list");
if let Some(entry) = listed.iter().find(|s| s.session_id == session.session_id) {
if entry.exited {
return;
}
}
if Instant::now() >= exit_deadline {
panic!("PTY session did not exit within 15s after terminate");
}
std::thread::sleep(Duration::from_millis(200));
}
})
.await
.expect("blocking task");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn pipe_terminate_returns_under_fast_budget() {
let scope = format!("fast-pipe-{}", line!());
let (_handle, socket) = start_server(&scope);
tokio::time::sleep(Duration::from_millis(300)).await;
let env_reporter = tokio::task::spawn_blocking(|| testbin_path("testbin-env-reporter"))
.await
.expect("testbin");
let socket_for_test = socket.clone();
tokio::task::spawn_blocking(move || {
let mut client = DaemonClient::connect_to(&socket_for_test).expect("connect");
let req = PipeSpawnRequest::new([env_reporter.to_string_lossy().into_owned()])
.with_originator("fast-pipe");
let session = client.spawn_pipe_session(&req).expect("spawn");
let started = Instant::now();
client
.terminate_pipe_session(&session.session_id, 2000)
.expect("terminate");
let elapsed = started.elapsed();
assert!(
elapsed < FAST_TERMINATE_BUDGET,
"pipe terminate RPC took {elapsed:?}, expected < {FAST_TERMINATE_BUDGET:?}; \
this is the #130 M4 fast-Ctrl+C invariant"
);
let exit_deadline = Instant::now() + Duration::from_secs(15);
loop {
let listed = client.list_pipe_sessions("").expect("list");
if let Some(entry) = listed.iter().find(|s| s.session_id == session.session_id) {
if entry.exited {
return;
}
}
if Instant::now() >= exit_deadline {
panic!("pipe session did not exit within 15s after terminate");
}
std::thread::sleep(Duration::from_millis(200));
}
})
.await
.expect("blocking task");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn long_grace_ms_does_not_block_the_client() {
let scope = format!("fast-grace-{}", line!());
let (_handle, socket) = start_server(&scope);
tokio::time::sleep(Duration::from_millis(300)).await;
let sleeper = tokio::task::spawn_blocking(|| testbin_path("testbin-sleeper"))
.await
.expect("testbin");
let socket_for_test = socket.clone();
tokio::task::spawn_blocking(move || {
let mut client = DaemonClient::connect_to(&socket_for_test).expect("connect");
let session = client
.spawn_pty_session(
&PtySpawnRequest::new([sleeper.to_string_lossy().into_owned()])
.with_originator("fast-grace"),
)
.expect("spawn");
let started = Instant::now();
client
.terminate_pty_session(&session.session_id, 30_000)
.expect("terminate");
let elapsed = started.elapsed();
assert!(
elapsed < FAST_TERMINATE_BUDGET,
"PTY terminate with 30s grace took {elapsed:?}, expected < {FAST_TERMINATE_BUDGET:?}"
);
client
.terminate_pty_session(&session.session_id, 0)
.expect("force terminate");
})
.await
.expect("blocking task");
}