Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 29 additions & 55 deletions cli/atomic_fs/src/lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ mod tests {
"exclusive acquisition reports the holder instead of waiting"
);
drop(shared);
eventually("releasing the shared lease frees the generation", || {
try_acquire_exclusive(&root, GenerationUse::Environment, HEX_A)
.expect("probe after release")
});
free(
&lease_path(&root, GenerationUse::Environment, HEX_A).expect("path"),
"releasing the shared lease frees the generation",
);
scratch.close().expect("cleanup");
}

Expand All @@ -286,9 +286,7 @@ mod tests {
let held = try_lock_file(&path, true).expect("lock").expect("free");
assert!(try_lock_file(&path, false).expect("probe").is_none());
drop(held);
eventually("release of the held path", || {
try_lock_file(&path, false).expect("probe")
});
free(&path, "release of the held path");
scratch.close().expect("cleanup");
}

Expand All @@ -307,14 +305,12 @@ mod tests {
drop(first);
assert!(try_lock_file(&path, false).expect("probe").is_none());
drop(second);
let exclusive = eventually("release of both shared holders", || {
try_lock_file(&path, false).expect("probe")
});
let exclusive = dx_test_scratch::wait::locked(&path, RELEASE)
.unwrap_or_else(|error| panic!("release of both shared holders: {error}"));
assert!(try_lock_file_shared(&path, false).expect("probe").is_none());
drop(exclusive);
eventually("release of the exclusive holder", || {
try_lock_file_shared(&path, false).expect("probe")
});
dx_test_scratch::wait::locked_shared(&path, RELEASE)
.unwrap_or_else(|error| panic!("release of the exclusive holder: {error}"));
scratch.close().expect("cleanup");
}

Expand Down Expand Up @@ -350,9 +346,7 @@ mod tests {
Err(io::ErrorKind::WouldBlock)
);
drop(first);
eventually("release of the first holder", || {
acquire_exclusive_at(&path, Duration::from_millis(100)).ok()
});
free(&path, "release of the first holder");
}

#[test]
Expand Down Expand Up @@ -396,12 +390,9 @@ mod tests {
"a handed-off shared handle keeps the generation locked"
);
drop(file);
eventually(
free(
&lease_path(&root, GenerationUse::Environment, HEX_A).expect("path"),
"the generation frees once the handed-off handle drops",
|| {
(!shared_held(&root, GenerationUse::Environment, HEX_A).expect("final probe"))
.then_some(())
},
);
scratch.close().expect("cleanup");
}
Expand All @@ -412,49 +403,32 @@ mod tests {
.arg("--exact")
.env("DX_LEASE_CHILD", mode)
.env("DX_LEASE_ROOT", root)
.stdin(std::process::Stdio::piped())
.spawn()
.expect("spawn lease child")
}

const RELEASE: Duration = Duration::from_secs(30);

fn wait_for(path: &Path, what: &str) {
let start = std::time::Instant::now();
while !path.exists() {
// LCOV_EXCL_START - reason: test timeout, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
assert!(
start.elapsed() <= Duration::from_secs(30),
"timed out waiting for {what}"
);
// LCOV_EXCL_STOP - reason: end test timeout, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
std::thread::sleep(Duration::from_millis(25));
}
dx_test_scratch::wait::path_appears(path, RELEASE)
.unwrap_or_else(|error| panic!("{what}: {error}"));
}

fn eventually<T>(what: &str, mut probe: impl FnMut() -> Option<T>) -> T {
let start = std::time::Instant::now();
loop {
if let Some(value) = probe() {
return value;
} // LCOV_EXCL_LINE - reason: the probe succeeds on the first poll, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
// LCOV_EXCL_START - reason: test timeout, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
assert!(
start.elapsed() <= Duration::from_secs(30),
"timed out waiting for {what}"
);
std::thread::sleep(Duration::from_millis(25));
// LCOV_EXCL_STOP - reason: end test timeout, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
}
fn free(path: &Path, what: &str) {
dx_test_scratch::wait::lock_free(path, RELEASE)
.unwrap_or_else(|error| panic!("{what}: {error}"));
}

fn child_main(root: &Path, mode: &str) {
let ready = root.join("child-ready");
let release = root.join("child-release");
match mode {
"die" => {
// LCOV_EXCL_START - reason: runs only in the spawned child, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
let _held = acquire_shared(root, GenerationUse::Generated, HEX_B, Duration::ZERO)
.expect("child acquires");
std::fs::write(&ready, b"ready").expect("signal ready");
wait_for(&release, "release");
drop(io::Read::read_to_end(&mut io::stdin(), &mut Vec::new()));
}
// LCOV_EXCL_STOP - reason: end runs only in the spawned child, issue: 8104, policy: docs/cli/commands/build-test-coverage.md
"contend" => {
Expand Down Expand Up @@ -502,10 +476,10 @@ mod tests {
child.kill().expect("kill the holder");
let status = child.wait().expect("reap the holder");
assert!(!status.success(), "the holder dies abnormally");
eventually("a killed holder must release its lease", || {
(!shared_held(&root, GenerationUse::Generated, HEX_B).expect("release probe"))
.then_some(())
});
free(
&lease_path(&root, GenerationUse::Generated, HEX_B).expect("path"),
"a killed holder must release its lease",
);
scratch.close().expect("cleanup");
}

Expand Down Expand Up @@ -542,10 +516,10 @@ mod tests {
"a shared acquire during an exclusive cleanup hold fails instead of waiting forever"
);
drop(exclusive);
eventually("release of the exclusive cleanup hold", || {
(!shared_held(&root, GenerationUse::Generated, HEX_B).expect("release probe"))
.then_some(())
});
free(
&lease_path(&root, GenerationUse::Generated, HEX_B).expect("path"),
"release of the exclusive cleanup hold",
);
std::fs::remove_file(root.join("child-verdict")).expect("reset the verdict");
let mut admitted = spawn_child(
"shared_acquire_fails_closed_while_cleanup_holds_exclusive",
Expand Down
3 changes: 2 additions & 1 deletion cli/http/src/lib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ fn answer(stream: TcpStream, reply: &Reply, log: &Mutex<Vec<String>>, full: &Mut
drop(out.write_all(body));
drop(out.flush());
if let Reply::Stall(..) = reply {
std::thread::sleep(Duration::from_secs(30));
drop(out.set_read_timeout(Some(Duration::from_secs(30))));
drop(std::io::copy(&mut reader, &mut std::io::sink()));
}
}

Expand Down
24 changes: 10 additions & 14 deletions cli/launcher/src/launch_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,16 @@ fn run_scrubs_unset_variables() {
.unwrap();
let launcher = dx_testing::env_file("DX_EXEC_LAUNCHER");
let app = dir.join("app.exe");
std::fs::copy(&launcher, &app).unwrap();
let mut tries = 0;
let status = loop {
match std::process::Command::new(&app)
.env("EXEC_LAUNCHER_UNSET_PROBE", "1")
.status()
{
Err(error) if error.kind() == std::io::ErrorKind::ExecutableFileBusy && tries < 50 => {
tries += 1;
std::thread::sleep(std::time::Duration::from_millis(20));
}
other => break other.unwrap(),
}
};
let copied = std::process::Command::new("cp")
.arg(&launcher)
.arg(&app)
.status()
.unwrap();
assert!(copied.success(), "cp the launcher: {copied}");
let status = std::process::Command::new(&app)
.env("EXEC_LAUNCHER_UNSET_PROBE", "1")
.status()
.unwrap();
assert_eq!(status.code(), Some(0));
}

Expand Down
44 changes: 36 additions & 8 deletions cli/lock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,16 +628,44 @@ mod tests {
#[derive(Default)]
struct Counting {
asked: std::sync::Mutex<Vec<String>>,
in_flight: AtomicUsize,
peak: AtomicUsize,
flight: std::sync::Mutex<Flight>,
changed: std::sync::Condvar,
overlap: usize,
}

#[derive(Default)]
struct Flight {
now: usize,
peak: usize,
}

impl Counting {
fn overlapping(overlap: usize) -> Self {
Self {
overlap,
..Self::default()
}
}

fn peak(&self) -> usize {
self.flight.lock().expect("flight").peak
}
}

impl Fetch for Counting {
fn sha256(&self, url: &str) -> Result<String, Error> {
let now = self.in_flight.fetch_add(1, Ordering::SeqCst) + 1;
self.peak.fetch_max(now, Ordering::SeqCst);
std::thread::sleep(std::time::Duration::from_millis(20));
self.in_flight.fetch_sub(1, Ordering::SeqCst);
let mut flight = self.flight.lock().expect("flight");
flight.now += 1;
flight.peak = flight.peak.max(flight.now);
self.changed.notify_all();
let (mut flight, _) = self
.changed
.wait_timeout_while(flight, std::time::Duration::from_secs(10), |flight| {
flight.peak < self.overlap
})
.expect("flight");
flight.now -= 1;
drop(flight);
self.asked.lock().expect("asked").push(url.to_owned());
if url.ends_with("/missing") {
return Err(Error::new("HTTP 404"));
Expand Down Expand Up @@ -674,9 +702,9 @@ mod tests {
.map(|n| package(&format!("p{n}"), &format!("https://example.com/p{n}")))
.collect();
let mut lock = lock_of(packages, BTreeMap::new());
let fetch = Counting::default();
let fetch = Counting::overlapping(2);
fill_all_digests(&mut lock, None, &fetch).expect("filled");
let peak = fetch.peak.load(Ordering::SeqCst);
let peak = fetch.peak();
assert!((2..=FETCH_JOBS).contains(&peak), "peak {peak}");
for entry in &lock.packages {
for archive in &entry.archives {
Expand Down
10 changes: 7 additions & 3 deletions cli/lock/tests/fixture_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ fn resolve(index_path: &str) -> Result<String, String> {
Ok(json!({ "packages": listed }).to_string())
}

fn park_forever() -> ! {
loop {
std::thread::park();
}
}

fn main() -> ExitCode {
let index = std::env::args().nth(1).unwrap_or_default();
if index == "--hang" {
loop {
std::thread::sleep(std::time::Duration::from_secs(60));
}
park_forever();
}
match resolve(&index) {
Ok(resolution) => {
Expand Down
12 changes: 6 additions & 6 deletions cli/os_sys/src/process_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ fn pid_zero_and_unknown_signals_are_rejected() {

#[test]
fn a_child_can_be_signalled_and_reaped() {
let mut child = std::process::Command::new("/bin/sleep")
.arg("30")
let mut child = std::process::Command::new("/bin/cat")
.stdin(std::process::Stdio::piped())
.process_group(0)
.spawn()
.expect("sleep");
.expect("cat");
let pid = i32::try_from(child.id()).expect("pid");
assert_eq!(group_of(pid).expect("getpgid"), pid);
assert_eq!(stopped_by(pid).expect("waitid"), None);
Expand All @@ -56,10 +56,10 @@ fn a_child_can_be_signalled_and_reaped() {
#[cfg(target_os = "linux")]
#[test]
fn an_unattributed_signal_reaches_the_child() {
let mut child = std::process::Command::new("/bin/sleep")
.arg("30")
let mut child = std::process::Command::new("/bin/cat")
.stdin(std::process::Stdio::piped())
.spawn()
.expect("sleep");
.expect("cat");
let pid = i32::try_from(child.id()).expect("pid");
queue_unattributed(pid, SIGTERM).expect("rt_sigqueueinfo");
let status = child.wait().expect("wait");
Expand Down
18 changes: 14 additions & 4 deletions cli/os_sys/src/signals_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,30 @@ fn unlink_on_stop_child() {
unlink_on_stop(Path::new(&path)).expect("watch");
let pid = i32::try_from(std::process::id()).expect("pid fits pid_t");
crate::process::signal(pid, libc::SIGTERM).expect("signal self");
std::thread::sleep(std::time::Duration::from_secs(30));
park_forever();
}

fn park_forever() -> ! {
loop {
std::thread::park();
}
}

#[test]
fn sigterm_removes_the_watched_path_and_exits_143() {
let dir = std::env::var_os("TEST_TMPDIR").map_or_else(std::env::temp_dir, Into::into);
let part = dir.join(format!("unlink-on-stop-{}.part", std::process::id()));
std::fs::write(&part, b"partial").expect("write");
let status = std::process::Command::new(std::env::current_exe().expect("exe"))
let child = std::process::Command::new(std::env::current_exe().expect("exe"))
.args(["--exact", "signals::signals_tests::unlink_on_stop_child"])
.env(UNLINK_CHILD, &part)
.status()
.spawn()
.expect("run the child");
assert_eq!(status.code(), Some(143));
let exit = dx_test_scratch::wait::exit_within(child, std::time::Duration::from_secs(30))
.expect("wait for the child");
let exited_143 =
matches!(&exit, dx_test_scratch::wait::Exit::Exited(status) if status.code() == Some(143));
assert!(exited_143, "{exit:?}");
assert!(!part.exists());
}

Expand Down
6 changes: 3 additions & 3 deletions cli/os_sys/src/spawn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::tty::{foreground_group, open_pty};
#[test]
fn a_child_in_a_terminal_leads_its_foreground_group() {
let (master, slave) = open_pty().expect("pty");
let mut command = Command::new("/bin/sleep");
command.arg("30");
let mut command = Command::new("/bin/cat");
command.stdin(std::process::Stdio::piped());
in_terminal(&mut command, slave);
let mut child = command.spawn().expect("sleep");
let mut child = command.spawn().expect("cat");
let pid = i32::try_from(child.id()).expect("pid");
let held = foreground_group(&master);
let group = group_of(pid);
Expand Down
6 changes: 2 additions & 4 deletions cli/process/src/groups_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,10 @@ fn a_group_that_ignores_the_first_signal_is_killed() {
let dir = scratch.path().join("groups");
std::fs::create_dir_all(&dir).expect("dir");
let armed = scratch.path().join("armed");
let script = format!(
"trap '' TERM; : > '{}'; while :; do sleep 1; done",
armed.display()
);
let script = format!("trap '' TERM; : > '{}'; read -r line", armed.display());
let mut child = Command::new("/bin/sh")
.args(["-c", &script])
.stdin(std::process::Stdio::piped())
.process_group(0)
.spawn()
.expect("shell starts");
Expand Down
Loading
Loading