Skip to content
67 changes: 63 additions & 4 deletions packages/cli/src/browser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,75 @@ fn launches_wait_for_a_free_slot() {
let waited = gate::acquire_in(directory.path(), &cancel, Duration::from_millis(200)).unwrap();
assert!(!waited.held());
assert!(started.elapsed() >= Duration::from_millis(200));
// A released slot is taken by the next launch.
// A released slot is taken by the next launch once it is free. A child another thread forks holds a copy of the
// slot's descriptor, and so its lock, until it execs (the descriptor closes on exec): while one does, the dropped
// slots stay locked, and the next launch waits for them.
#[cfg(unix)]
{
let child = ForkedChild::spawn();
drop(held);
assert!(
!gate::acquire_in(directory.path(), &cancel, Duration::ZERO).unwrap().held(),
"the forked child holds both slots"
);
child.exec();
}
#[cfg(not(unix))]
drop(held);
assert!(gate::acquire_in(directory.path(), &cancel, Duration::ZERO).unwrap().held());
let _held: Vec<_> = (0..2)
.map(|_| gate::acquire_in(directory.path(), &cancel, Duration::ZERO).unwrap())
assert!(gate::acquire_in(directory.path(), &cancel, Duration::from_secs(10)).unwrap().held());
let taken: Vec<_> = (0..2)
.map(|_| gate::acquire_in(directory.path(), &cancel, Duration::from_secs(10)).unwrap())
.collect();
assert!(taken.iter().all(|slot| slot.held()));
cancel.store(true, Ordering::SeqCst);
assert!(gate::acquire_in(directory.path(), &cancel, Duration::from_secs(60)).is_err());
}

/// A child forked from another thread and held between fork and exec, as a test thread's spawn can be: until it
/// execs, it holds a copy of every descriptor this process had open when it forked.
#[cfg(unix)]
struct ForkedChild {
release: std::io::PipeWriter,
thread: std::thread::JoinHandle<()>,
}

#[cfg(unix)]
impl ForkedChild {
/// Returns once the child has forked; it waits in `pre_exec` until `exec`.
fn spawn() -> ForkedChild {
use std::io::Read;
use std::os::fd::AsRawFd;
use std::os::unix::process::CommandExt;
let (mut ready, ready_writer) = std::io::pipe().unwrap();
let (release_reader, release) = std::io::pipe().unwrap();
let (signal, wait) = (ready_writer.as_raw_fd(), release_reader.as_raw_fd());
let thread = std::thread::spawn(move || {
let mut command = std::process::Command::new("true");
// SAFETY: the child only writes to and reads from pipes it inherited, which is async-signal-safe.
unsafe {
command.pre_exec(move || {
let mut byte = [1u8];
libc::write(signal, byte.as_ptr().cast(), 1);
libc::read(wait, byte.as_mut_ptr().cast(), 1);
Ok(())
});
}
command.status().unwrap();
drop((ready_writer, release_reader));
});
ready.read_exact(&mut [0u8]).unwrap();
ForkedChild { release, thread }
}

/// Lets the child exec, closing its copies of the descriptors, and waits for it.
fn exec(self) {
use std::io::Write;
let ForkedChild { mut release, thread } = self;
release.write_all(&[1]).unwrap();
thread.join().unwrap();
}
}

/// A stand-in browser that records each start in `starts`, then runs `body`.
#[cfg(unix)]
fn fake_browser(directory: &Path, body: &str) -> (std::path::PathBuf, std::path::PathBuf) {
Expand Down
Loading