use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
use std::time::{Duration, Instant};
use nix::sys::event::{EvFlags, EventFilter, FilterFlag, KEvent};
use super::{fill_pipe_to_capacity, fionread, marker_pipe, spawn_marker_holder};
use crate::containment::marker_eof::{arm, block_until_drained, drain_test_hooks, interpret_read_event, probe};
use crate::containment::TreeDrain;
use crate::wait::backend::test_hooks;
use crate::wait::test_clock;
fn measure_pipe_capacity() -> usize {
let (_r, w) = marker_pipe();
let flags = rustix::fs::fcntl_getfl(&w).unwrap_or_else(|e| panic!("fcntl F_GETFL failed: {e}"));
rustix::fs::fcntl_setfl(&w, flags | rustix::fs::OFlags::NONBLOCK)
.unwrap_or_else(|e| panic!("fcntl F_SETFL O_NONBLOCK failed: {e}"));
let chunk = [0u8; 4096];
let mut total = 0usize;
loop {
match nix::unistd::write(&w, &chunk) {
Ok(got) => total += got,
Err(nix::errno::Errno::EAGAIN) => return total, Err(nix::errno::Errno::EINTR) => continue,
Err(e) => panic!("scratch pipe write failed: {e}"),
}
}
}
fn pipe_blksize(fd: BorrowedFd<'_>) -> isize {
let mut st: libc::stat = unsafe { std::mem::zeroed() };
let rc = unsafe { libc::fstat(fd.as_raw_fd(), &mut st) };
assert_eq!(rc, 0, "fstat failed: {}", std::io::Error::last_os_error());
st.st_blksize as isize
}
fn block_until_marker_ready_again(fd: BorrowedFd<'_>) {
let kq = arm(fd, false).expect("arm auxiliary kqueue");
let mut events = [KEvent::new(
0,
EventFilter::EVFILT_READ,
EvFlags::empty(),
FilterFlag::empty(),
0,
0,
)];
loop {
match kq.kevent(&[], &mut events, None) {
Ok(n) if n > 0 => return,
Ok(_) => continue,
Err(nix::errno::Errno::EINTR) => continue,
Err(e) => panic!("auxiliary kevent failed: {e}"),
}
}
}
#[skuld::test]
fn a_quiet_live_holder_resolves_in_one_real_blocking_kevent_call() {
let _hook_guard = test_hooks::HookGuard::install(|_round, _kq| {});
let (child, marker, _stdin) = spawn_marker_holder("exec cat >/dev/null");
let (_clock_guard, frozen_now) = test_clock::FrozenClockGuard::install();
let deadline = frozen_now.checked_add(Duration::from_millis(300)).expect("deadline");
let verdict =
block_until_drained(marker.as_fd(), Some(Some(deadline))).expect("bounded wait against a quiet holder");
assert_eq!(verdict, TreeDrain::MembersRemain);
assert_eq!(
test_hooks::kevent_calls(),
1,
"a quiet holder must resolve the wait in exactly one real, blocking kevent call — more \
would mean a busy-poll, not a genuine kernel block"
);
child.kill().expect("kill");
child.wait().expect("reap");
}
#[skuld::test]
fn block_until_drained_never_returns_before_a_real_deadline() {
let mut cmd = crate::Command::new();
cmd.executable("/bin/sh").args(["sh", "-c", "exec yes >&3"]);
cmd.fd(3, crate::Stdio::pipe_out()).expect("marker pipe");
let mut child = cmd.spawn().expect("spawn yes");
let marker = child.fd_read_end(3.into()).expect("marker read end");
let deadline = Instant::now()
.checked_add(Duration::from_millis(300))
.expect("deadline");
let verdict = block_until_drained(marker.as_fd(), Some(Some(deadline))).expect("bounded wait");
let now = Instant::now();
assert_eq!(
verdict,
TreeDrain::MembersRemain,
"a sustained writer must still report MembersRemain"
);
assert!(
now >= deadline,
"must never return before the deadline: now={now:?}, deadline={deadline:?}"
);
child.kill().expect("kill the sustained writer");
child.wait().expect("reap");
}
#[skuld::test]
fn a_sustained_writer_is_checked_against_the_deadline_every_round() {
let mut cmd = crate::Command::new();
cmd.executable("/bin/sh").args(["sh", "-c", "exec yes >&3"]);
cmd.fd(3, crate::Stdio::pipe_out()).expect("marker pipe");
let mut child = cmd.spawn().expect("spawn yes");
let marker = child.fd_read_end(3.into()).expect("marker read end");
let raw_fd = marker.as_raw_fd();
let (_clock_guard, frozen_now) = test_clock::FrozenClockGuard::install();
let one_year = Duration::from_secs(365 * 24 * 3600);
let deadline = frozen_now.checked_add(one_year).expect("deadline");
let drained_baseline = std::rc::Rc::new(std::cell::Cell::new(None::<u64>));
let drained_baseline_for_hook = std::rc::Rc::clone(&drained_baseline);
let _hook_guard = test_hooks::HookGuard::install(move |round, _kq| {
assert!(
round <= 1,
"must not start a third round after the deadline has elapsed — extra-round mutant, \
round={round}"
);
if round == 1 {
let fd = unsafe { BorrowedFd::borrow_raw(raw_fd) };
block_until_marker_ready_again(fd);
drained_baseline_for_hook.set(Some(drain_test_hooks::drained_bytes()));
test_clock::advance(one_year + Duration::from_secs(86_400));
}
});
let verdict = block_until_drained(marker.as_fd(), Some(Some(deadline))).expect("bounded wait");
assert_eq!(
verdict,
TreeDrain::MembersRemain,
"a sustained writer must still report MembersRemain"
);
let calls = test_hooks::kevent_calls();
assert_eq!(
calls, 2,
"exactly two real kevent calls: one round that saw the deadline as still open, then one \
more round whose already_elapsed check (from the frozen, mock-advanced clock) stops \
the loop — never a third round"
);
let requested = test_hooks::requested_timeouts();
assert_eq!(requested.len(), 2, "one requested timeout recorded per kevent call");
assert_eq!(
requested[0],
Some(one_year),
"round 0's requested timeout must be EXACTLY the frozen deadline's remaining duration, \
got {:?}",
requested[0]
);
assert_eq!(
requested[1],
Some(Duration::ZERO),
"round 1's requested timeout must reflect the mock-advanced (already past) deadline, \
proving it is derived from remaining(deadline) freshly each round, not cached once \
before the loop"
);
let baseline = drained_baseline
.get()
.expect("the round-1 hook must have run and recorded a baseline");
assert!(
baseline > 0,
"round 0 must have drained something — otherwise this test can't distinguish 'round 1 \
correctly drained nothing' from 'draining is broken everywhere'"
);
assert_eq!(
drain_test_hooks::drained_bytes(),
baseline,
"round 1 must not have drained any bytes once the deadline had elapsed (principle 13) — \
the cumulative drained-bytes counter moved after round 1's own precondition was set"
);
assert!(
test_hooks::last_event_data().expect("round 1 must have seen a real event") > 0,
"round 1 must have gone through interpret() with a genuine non-EOF event (elapsed=true), \
not merely resolved via a bare Ok(0)"
);
child.kill().expect("kill the sustained writer");
child.wait().expect("reap");
}
#[skuld::test]
fn an_event_that_arrives_after_the_clock_moves_past_the_deadline_mid_round_does_not_drain() {
let cap = measure_pipe_capacity();
let (child, marker, stdin) = spawn_marker_holder(&format!("yes | head -c {cap} >&3; exec cat >/dev/null"));
let (_clock_guard, frozen_now) = test_clock::FrozenClockGuard::install();
let deadline = frozen_now.checked_add(Duration::from_secs(60)).expect("deadline");
let _hook_guard = test_hooks::HookGuard::install_with_post_event(
|_round, _kq| {},
|round| {
if round == 0 {
test_clock::advance(Duration::from_secs(120));
}
},
);
let baseline = drain_test_hooks::drained_bytes();
let verdict = block_until_drained(marker.as_fd(), Some(Some(deadline))).expect("bounded wait");
assert_eq!(
verdict,
TreeDrain::MembersRemain,
"the writer is still alive and holding the descriptor — the event was non-EOF"
);
assert_eq!(
test_hooks::kevent_calls(),
1,
"the deadline is discovered elapsed as soon as the (only) event arrives — no second round"
);
assert_eq!(
drain_test_hooks::drained_bytes(),
baseline,
"an event that arrives after the deadline has passed, even if that only became true \
WHILE the kevent call was blocked, must not drain"
);
drop(stdin);
child.kill().expect("kill");
child.wait().expect("reap");
}
#[skuld::test]
fn a_spurious_ok0_under_a_frozen_clock_retries_until_the_clock_advances() {
let (child, marker, _stdin) = spawn_marker_holder("exec cat >/dev/null");
let (_clock_guard, frozen_now) = test_clock::FrozenClockGuard::install();
let one_year = Duration::from_secs(365 * 24 * 3600);
let deadline = frozen_now.checked_add(one_year).expect("deadline");
let _hook_guard = test_hooks::HookGuard::install(move |round, _kq| {
if round == 1 {
test_clock::advance(one_year + Duration::from_secs(86_400));
}
});
test_hooks::set_timeout_override(Duration::from_millis(20));
let verdict = block_until_drained(marker.as_fd(), Some(Some(deadline))).expect("bounded wait");
assert_eq!(verdict, TreeDrain::MembersRemain);
assert_eq!(
test_hooks::kevent_calls(),
2,
"round 0's real, short-overridden timeout genuinely expires while remaining(deadline) \
still reports the far future — spurious, retried; round 1, after the hook moves the \
clock, genuinely concludes — never more than two calls"
);
let requested = test_hooks::requested_timeouts();
assert_eq!(
requested[0],
Some(Duration::from_millis(20)),
"round 0's requested timeout must be the OVERRIDDEN value, proving the mismatch with \
remaining(deadline) was real, not incidental"
);
child.kill().expect("kill");
child.wait().expect("reap");
}
#[skuld::test]
fn an_unbounded_wait_against_a_sustained_writer_blocks_without_spending_cpu() {
let cap = measure_pipe_capacity();
let (child, marker, _stdin) = spawn_marker_holder(&format!("yes | head -c {cap} >&3; exec cat >/dev/null"));
let raw_fd = marker.as_raw_fd();
let mut child_opt = Some(child);
let _hook_guard = test_hooks::HookGuard::install(move |round, kq| {
assert!(
round <= 1,
"the wait should have concluded by round 1, got round={round}"
);
if round != 1 {
return;
}
let fd = unsafe { BorrowedFd::borrow_raw(raw_fd) };
let round0_data = test_hooks::last_event_data().expect("round 0 must have recorded a non-EOF event's data");
let blksize = pipe_blksize(fd);
assert_eq!(
round0_data, blksize,
"round 0's event must report exactly the marker pipe's own kernel-reported capacity"
);
assert_eq!(
fionread(fd) as isize,
round0_data,
"an unbounded wait must not drain any bytes in round 0 (decision: unbounded never \
drains) — FIONREAD dropped below round 0's own reported byte count"
);
let zero = libc::timespec { tv_sec: 0, tv_nsec: 0 };
let mut events = [KEvent::new(
0,
EventFilter::EVFILT_READ,
EvFlags::empty(),
FilterFlag::empty(),
0,
0,
)];
let n = kq
.kevent(&[], &mut events, Some(zero))
.expect("zero-timeout poll on the wait's own kqueue");
assert_eq!(
n, 0,
"EV_CLEAR must suppress a re-poll of the SAME unchanged level — got {n} events, \
EV_CLEAR appears unset on the armed knote"
);
let child = child_opt.take().expect("round 1 fires exactly once");
child.kill().expect("kill the sustained writer");
child.wait().expect("reap");
});
let verdict = block_until_drained(marker.as_fd(), None).expect("unbounded wait against a sustained writer");
assert_eq!(
verdict,
TreeDrain::AllMarkersClosed,
"killing the writer closes the marker descriptor, which must still be observed"
);
assert_eq!(
test_hooks::kevent_calls(),
2,
"round 0 (the undrained, non-EOF event) plus round 1 (the EOF that ends the wait) — \
exactly two real kevent calls, not a busy-poll"
);
}
#[skuld::test]
fn arm_sets_ev_clear_so_a_repeated_poll_without_a_new_edge_reports_nothing() {
let cap = measure_pipe_capacity();
let (child, marker, _stdin) = spawn_marker_holder(&format!("yes | head -c {cap} >&3; exec cat >/dev/null"));
let kq = arm(marker.as_fd(), false).expect("arm");
let mut events = [KEvent::new(
0,
EventFilter::EVFILT_READ,
EvFlags::empty(),
FilterFlag::empty(),
0,
0,
)];
let n = kq.kevent(&[], &mut events, None).expect("initial blocking poll");
assert_eq!(n, 1, "expected the initial crossing to be ready");
assert!(
!events[0].flags().contains(EvFlags::EV_EOF),
"the writer is still alive and holding the descriptor — this must not be EOF"
);
let zero = libc::timespec { tv_sec: 0, tv_nsec: 0 };
let n2 = kq
.kevent(&[], &mut events, Some(zero))
.expect("second, zero-timeout poll");
assert_eq!(
n2, 0,
"EV_CLEAR must suppress a re-poll of an unchanged level — got {n2} events"
);
child.kill().expect("kill");
child.wait().expect("reap");
}
#[skuld::test]
fn interpret_read_event_suppresses_drain_when_told_to() {
let cap = measure_pipe_capacity();
let (child, marker, _stdin) = spawn_marker_holder(&format!("yes | head -c {cap} >&3; exec cat >/dev/null"));
let kq = arm(marker.as_fd(), true).expect("arm");
let mut events = [KEvent::new(
0,
EventFilter::EVFILT_READ,
EvFlags::empty(),
FilterFlag::empty(),
0,
0,
)];
let n = kq.kevent(&[], &mut events, None).expect("blocking poll");
assert_eq!(n, 1, "expected the crossing to be ready");
let before = fionread(marker.as_fd());
let verdict = interpret_read_event(&events[0], marker.as_fd(), true).expect("interpret with suppress_drain=true");
let after = fionread(marker.as_fd());
assert_eq!(
verdict, None,
"a non-EOF event with drain suppressed must stay inconclusive"
);
assert_eq!(after, before, "suppress_drain=true must never consume any bytes");
child.kill().expect("kill");
child.wait().expect("reap");
}
#[skuld::test]
fn probe_never_drains_even_past_the_low_water_clamp() {
let (marker_r, marker_w) = std::io::pipe().expect("pipe");
let queued_before = fill_pipe_to_capacity(marker_r.as_fd(), marker_w.as_fd());
assert!(
queued_before > 0,
"the pipe must be filled before this test can prove probe() doesn't drain it"
);
let mut cmd = crate::Command::new();
cmd.executable("/bin/sh").args(["sh", "-c", "exec cat >/dev/null"]);
cmd.fd(0, crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.fd(
3,
crate::Stdio::from_file(std::fs::File::from(std::os::fd::OwnedFd::from(marker_w))),
)
.expect("marker pipe");
let child = cmd.spawn().expect("spawn /bin/sh");
let fd = marker_r.as_fd();
assert_eq!(
child.try_wait().expect("try_wait"),
None,
"the holder already exited before this test could observe it"
);
let baseline = drain_test_hooks::drained_bytes();
let verdict = probe(fd).expect("probe");
let queued_after = fionread(fd);
assert_eq!(
verdict,
TreeDrain::MembersRemain,
"the writer is still alive and holding the descriptor — the event was non-EOF"
);
assert_eq!(
queued_after, queued_before,
"probe must not consume any bytes from the pipe"
);
assert_eq!(
drain_test_hooks::drained_bytes(),
baseline,
"probe is a check at expiry (principle 13) and must never drain"
);
child.kill().expect("kill");
child.wait().expect("reap");
}