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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,17 @@ jobs:
run: |
cargo build
cargo test

netbsd:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test on NetBSD
uses: vmactions/netbsd-vm@v1
with:
usesh: true
prepare: |
pkg_add rust
run: |
cargo build
cargo test
9 changes: 9 additions & 0 deletions src/tests/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::{Duration, Instant};

use tempfile::TempDir;

use super::exec_signal_delay;
use crate::{Exec, Redirection};

// --- Single-command Job tests ---
Expand Down Expand Up @@ -237,6 +238,7 @@ fn broken_pipe_on_stdin() {
fn terminate() {
let start = Instant::now();
let handle = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
handle.terminate().unwrap();
handle.wait().unwrap();
assert!(
Expand All @@ -251,6 +253,7 @@ fn terminate_twice() {

let start = Instant::now();
let handle = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
handle.terminate().unwrap();
thread::sleep(Duration::from_millis(100));
handle.terminate().unwrap();
Expand Down Expand Up @@ -279,6 +282,7 @@ fn pid_while_running() {
job.processes[0].exit_status().is_none(),
"exit_status() should be None while running"
);
exec_signal_delay();
job.terminate().unwrap();
job.wait().unwrap();
// pid is still available after exit
Expand All @@ -292,6 +296,7 @@ fn pid_while_running() {
#[test]
fn poll_running_process() {
let job = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
assert!(
job.poll().is_none(),
"poll() should return None for running process"
Expand Down Expand Up @@ -346,6 +351,7 @@ fn wait_timeout_zero() {
"zero timeout took too long"
);
assert!(result.is_none());
exec_signal_delay();
job.terminate().unwrap();
job.wait().unwrap();
}
Expand Down Expand Up @@ -434,6 +440,7 @@ fn exec_wait_timeout_terminate() {
fn started_pid() {
let start = Instant::now();
let job = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
assert!(job.pid() > 0, "pid() should be nonzero");
job.terminate().unwrap();
job.wait().unwrap();
Expand All @@ -446,6 +453,7 @@ fn started_pid() {
#[test]
fn started_kill() {
let handle = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
handle.kill().unwrap();
let status = handle.wait().unwrap();
assert!(!status.success());
Expand All @@ -455,6 +463,7 @@ fn started_kill() {
fn started_poll() {
let start = Instant::now();
let job = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
assert!(job.poll().is_none(), "poll() should be None while running");
job.terminate().unwrap();
job.wait().unwrap();
Expand Down
9 changes: 9 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ mod win32;

use crate::{Capture, Communicator, Exec, ExitStatus, Job, Pipeline, Process, Redirection};

/// Work around a NetBSD kernel race where signals sent immediately after
/// exec can be silently dropped. A brief sleep gives the child time to
/// become fully ready for signal delivery.
fn exec_signal_delay() {
if cfg!(target_os = "netbsd") {
std::thread::sleep(std::time::Duration::from_millis(1));
}
}

fn assert_send_sync<T: Send + Sync>() {}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions src/tests/posix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::time::{Duration, Instant};

use super::exec_signal_delay;
use crate::unix::{JobExt, PipelineExt};
use crate::{Exec, ExecExt, ExitStatus, Redirection};

#[test]
fn err_terminate() {
let job = Exec::cmd("sleep").arg("5").start().unwrap();
exec_signal_delay();
assert!(job.poll().is_none());
job.terminate().unwrap();
assert!(job.wait().unwrap().is_killed_by(libc::SIGTERM));
Expand All @@ -29,6 +31,7 @@ fn waitpid_echild() {
#[test]
fn send_signal() {
let job = Exec::cmd("sleep").arg("5").start().unwrap();
exec_signal_delay();
job.send_signal(libc::SIGUSR1).unwrap();
assert_eq!(job.wait().unwrap().signal(), Some(libc::SIGUSR1));
}
Expand Down Expand Up @@ -69,6 +72,7 @@ fn exec_setpgid() {
.setpgid()
.start()
.unwrap();
exec_signal_delay();
job.send_signal_group(libc::SIGTERM).unwrap();
assert!(job.wait().unwrap().is_killed_by(libc::SIGTERM));
}
Expand All @@ -83,6 +87,7 @@ fn send_signal_group() {
.setpgid()
.start()
.unwrap();
exec_signal_delay();
job.send_signal_group(libc::SIGTERM).unwrap();
assert!(job.wait().unwrap().is_killed_by(libc::SIGTERM));
}
Expand All @@ -99,6 +104,7 @@ fn send_signal_group_after_finish() {
fn kill_process() {
// kill() sends SIGKILL which cannot be caught.
let job = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
job.kill().unwrap();
assert!(job.wait().unwrap().is_killed_by(libc::SIGKILL));
}
Expand All @@ -108,10 +114,12 @@ fn kill_vs_terminate() {
// Demonstrate that terminate (SIGTERM) and kill (SIGKILL) produce
// different exit statuses.
let j1 = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
j1.terminate().unwrap();
let status1 = j1.wait().unwrap();

let j2 = Exec::cmd("sleep").arg("10").start().unwrap();
exec_signal_delay();
j2.kill().unwrap();
let status2 = j2.wait().unwrap();

Expand Down Expand Up @@ -155,6 +163,7 @@ fn exit_status_display() {
#[test]
fn started_send_signal() {
let job = Exec::cmd("sleep").arg("5").start().unwrap();
exec_signal_delay();
job.send_signal(libc::SIGTERM).unwrap();
let status = job.wait().unwrap();
assert!(status.is_killed_by(libc::SIGTERM));
Expand All @@ -167,6 +176,7 @@ fn started_send_signal_group() {
.setpgid()
.start()
.unwrap();
exec_signal_delay();
job.send_signal_group(libc::SIGKILL).unwrap();
let status = job.wait().unwrap();
assert!(status.is_killed_by(libc::SIGKILL) || status.is_killed_by(libc::SIGTERM));
Expand All @@ -183,6 +193,7 @@ fn pipeline_setpgid() {
.start()
.unwrap();
assert_eq!(handle.processes.len(), 2);
exec_signal_delay();
handle.send_signal_group(libc::SIGTERM).unwrap();
for p in &handle.processes {
let status = p.wait().unwrap();
Expand Down
Loading