Lutra/crates/proc/tests/process_tree.rs
danny ebbc89832c F3: suspended Phpactor start, ConPTY shell in a job, cancel superseded completions, syntax proptest
- lutra_proc::spawn_suspended: CREATE_SUSPENDED, join the kill-on-close
  job, resume via a Tool Help thread snapshot. Opt-in for long-lived
  children (Phpactor); git, php -l and the probe keep spawn because the
  snapshot costs ~60 ms per start.
- lutra_proc::adopt + Guard (Windows): the terminal's ConPTY shell joins a
  job, so processes it started end with the tab or with Lutra.
- lsp.completion cancels the previous pending completion on the same
  server ($/cancelRequest); the cancelled command resolves empty.
- Syntax proptests compare leaf kinds, outline and folding instead of the
  S-expression (tree-sitter-php 0.25.1 keeps a union_type wrapper after
  an undo); Malte's seed added.
- docs/development.md: data dirs, target dir inside the repo, parallel
  builds, deleting ui/node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:28:54 +01:00

137 lines
4.4 KiB
Rust

//! Process trees started through `lutra_proc` die with their owner.
//!
//! Each test starts `lutra-proc-helper` in a tree role: the helper starts a
//! sleeping grandchild that holds its standard output, prints `ready`
//! and sleeps. Reading that output to end of file proves that every
//! process holding it, the grandchild included, has exited. Without the
//! groups the grandchild sleeps for two minutes and the read times out.
#![allow(clippy::unwrap_used)]
use std::io::{BufRead, BufReader, Read, Write};
use std::process::{ChildStdout, Command, Stdio};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
/// How long a test waits for a tree to go away.
const GONE_WITHIN: Duration = Duration::from_secs(15);
/// The helper in `role`, with its output piped.
fn helper(role: &str) -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_lutra-proc-helper"));
command
.arg(role)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null());
command
}
/// Waits for the helper's `ready` line and returns the rest of its output.
fn ready(stdout: ChildStdout) -> BufReader<ChildStdout> {
let mut reader = BufReader::new(stdout);
let mut line = String::new();
reader.read_line(&mut line).unwrap();
assert_eq!(line.trim(), "ready");
reader
}
/// Asserts that the output reaches end of file within [`GONE_WITHIN`],
/// i.e. that no process holding it is left.
fn assert_gone(mut reader: BufReader<ChildStdout>) {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let mut rest = Vec::new();
let _ = reader.read_to_end(&mut rest);
let _ = tx.send(());
});
assert!(
rx.recv_timeout(GONE_WITHIN).is_ok(),
"a grandchild still holds the pipe after {GONE_WITHIN:?}"
);
}
#[test]
fn hard_kill_of_the_parent_takes_its_guarded_child() {
let mut parent = helper("tree-proc").spawn().unwrap();
let reader = ready(parent.stdout.take().unwrap());
parent.kill().unwrap();
parent.wait().unwrap();
assert_gone(reader);
}
#[test]
fn hard_kill_works_when_the_parent_is_in_a_group_itself() {
let mut parent = lutra_proc::spawn(helper("tree-proc")).unwrap();
let reader = ready(parent.stdout.take().unwrap());
std::process::Child::kill(&mut parent).unwrap();
parent.wait().unwrap();
assert_gone(reader);
drop(parent);
}
#[test]
fn kill_takes_the_whole_tree() {
let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap();
let reader = ready(parent.stdout.take().unwrap());
parent.kill().unwrap();
parent.wait().unwrap();
assert_gone(reader);
}
#[test]
fn drop_takes_the_whole_tree() {
let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap();
let reader = ready(parent.stdout.take().unwrap());
drop(parent);
assert_gone(reader);
}
#[test]
fn a_child_that_exits_reports_its_status() {
let mut child = lutra_proc::spawn(helper("unknown-role")).unwrap();
let status = child.wait().unwrap();
assert_eq!(status.code(), Some(2));
child.kill().unwrap();
}
#[test]
fn a_missing_program_is_an_error() {
let error = lutra_proc::spawn(Command::new("lutra-proc-no-such-program")).unwrap_err();
assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
}
#[test]
fn a_suspended_start_runs_and_takes_the_whole_tree() {
let mut child = lutra_proc::spawn_suspended(helper("unknown-role")).unwrap();
assert_eq!(child.wait().unwrap().code(), Some(2));
let mut parent = lutra_proc::spawn_suspended(helper("tree-plain")).unwrap();
let reader = ready(parent.stdout.take().unwrap());
drop(parent);
assert_gone(reader);
}
#[test]
fn hard_kill_works_when_a_suspended_start_parent_guards_its_child() {
let mut parent = lutra_proc::spawn_suspended(helper("tree-proc")).unwrap();
let reader = ready(parent.stdout.take().unwrap());
std::process::Child::kill(&mut parent).unwrap();
parent.wait().unwrap();
assert_gone(reader);
}
#[cfg(windows)]
#[test]
fn dropping_an_adopt_guard_takes_the_tree_started_after_it() {
use std::os::windows::io::AsRawHandle;
let mut command = helper("tree-on-input");
command.stdin(Stdio::piped());
let mut parent = command.spawn().unwrap();
let guard = lutra_proc::adopt(parent.as_raw_handle()).unwrap();
parent.stdin.take().unwrap().write_all(b"go\n").unwrap();
let reader = ready(parent.stdout.take().unwrap());
drop(guard);
assert_gone(reader);
parent.wait().unwrap();
}