Bash · 1796 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[kill-wait]"
3 . "$(cd "$(dirname "$0")/.." && pwd)/test_harness.sh"
4
5 section "1. kill signals"
6 check_exit "kill -l lists signals" 'kill -l' "0"
7 compare_exit "kill nonexistent PID fails" 'kill -0 999999 2>/dev/null'
8 compare_output "kill -l output has TERM" 'kill -l | grep -q TERM && echo yes'
9 compare_output "kill -0 checks process exists" 'sleep 60 & pid=$!; kill -0 $pid && echo alive; kill $pid; wait $pid 2>/dev/null'
10 compare_output "kill sends TERM by default" 'sleep 60 & pid=$!; kill $pid; wait $pid 2>/dev/null; echo done'
11 compare_output "kill -9 sends SIGKILL" 'sleep 60 & pid=$!; kill -9 $pid; wait $pid 2>/dev/null; echo done'
12 compare_output "kill -s TERM sends TERM" 'sleep 60 & pid=$!; kill -s TERM $pid; wait $pid 2>/dev/null; echo done'
13 compare_exit "kill with invalid signal" 'kill -INVALID 1 2>/dev/null'
14
15 section "2. wait basic"
16 compare_output "wait for background job" 'echo start; true & wait; echo done'
17 compare_exit "wait returns bg exit status 0" 'true & wait $!'
18 compare_exit "wait returns bg exit status 1" 'false & wait $!'
19 compare_output "wait with no bg jobs succeeds" 'wait; echo $?'
20 compare_exit "wait specific PID" 'sleep 0.1 & wait $!'
21
22 section "3. wait multiple"
23 compare_output "wait all background jobs" 'true & true & wait; echo done'
24 compare_output "wait specific among multiple" 'sleep 0.1 & p1=$!; sleep 0.1 & p2=$!; wait $p1; echo "p1=$?"; wait $p2; echo "p2=$?"'
25 compare_exit "wait for already-exited process" 'true & pid=$!; sleep 0.1; wait $pid'
26
27 section "4. wait exit status"
28 compare_output "wait captures exit 42" '(exit 42) & wait $!; echo $?'
29 compare_output "wait captures exit 0" '(exit 0) & wait $!; echo $?'
30 compare_output "dollar-bang is last bg PID" 'sleep 0.1 & echo $! | grep -qE "^[0-9]+$" && echo valid'
31
32 print_summary