feat: add PID tracking test

This commit is contained in:
gmega 2025-06-17 17:51:12 -03:00
parent 93187c6a91
commit 0c6f2b3b34
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
4 changed files with 59 additions and 27 deletions

7
src/clh Normal file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
LIB_SRC=${LIB_SRC:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}
source "${LIB_SRC}/config.bash"
source "${LIB_SRC}/utils.bash"
source "${LIB_SRC}/process_monitor.bash"

View File

@ -33,13 +33,11 @@ clh_start_process_monitor() {
( (
shutdown=false shutdown=false
while ! $shutdown; do while ! $shutdown; do
for pid_file in "${_procmon_output}"/*.pid; do clh_get_tracked_pids
[[ -f "${pid_file}" ]] || continue # null glob for pid in "${result[@]}"; do
base_name=$(basename "${pid_file}")
pid=${base_name%.pid}
if ! kill -0 "${pid}"; then if ! kill -0 "${pid}"; then
echoerr "[procmon] ${pid} is dead" echoerr "[procmon] ${pid} is dead"
rm "${pid_file}" rm "${_procmon_output}/${pid}.pid"
fi fi
sleep 1 sleep 1
done done
@ -60,6 +58,7 @@ clh_track_last_background_job() {
clh_get_tracked_pids() { clh_get_tracked_pids() {
result=() result=()
for pid_file in "${_procmon_output}"/*.pid; do for pid_file in "${_procmon_output}"/*.pid; do
[[ -f "${pid_file}" ]] || continue # null glob
base_name=$(basename "${pid_file}") base_name=$(basename "${pid_file}")
pid=${base_name%.pid} pid=${base_name%.pid}
result+=("${pid}") result+=("${pid}")
@ -80,7 +79,7 @@ clh_stop_process_monitor() {
if [ "$1" = "monitor_only" ]; then if [ "$1" = "monitor_only" ]; then
echoerr "[procmon] stop monitor only. Children will be left behind." echoerr "[procmon] stop monitor only. Children will be left behind."
kill -s TERM "$_procmon_pid" kill -s TERM "$_procmon_pid"
wait "$_procmon_pid" await "$_procmon_pid"
return 0 return 0
else else
echoerr "[procmon] stop process group. This will halt the script." echoerr "[procmon] stop process group. This will halt the script."

View File

@ -11,4 +11,11 @@ clh_output_folder() {
echoerr() { echoerr() {
echo "$@" >&2 echo "$@" >&2
}
await() {
local pid=$1
while kill -0 "$pid"; do
sleep 0.1
done
} }

View File

@ -18,29 +18,48 @@ setup() {
refute clh_stop_process_monitor refute clh_stop_process_monitor
} }
# @test "should keep track of process PIDs" { @test "should keep track of process IDs" {
# clh_get_tracked_pids echo "hi"
# assert [ ${#result[@]} -eq 0 ] assert clh_start_process_monitor
# ( clh_get_tracked_pids
# while [ ! -f "${OUTPUT_FOLDER}/stop" ]; do assert [ ${#result[@]} -eq 0 ]
# sleep 0.1
# done
# ) &
# clh_track_last_background_job
# ( (
# while [ ! -f "${OUTPUT_FOLDER}/stop" ]; do while true; do
# sleep 0.1 sleep 0.1
# done done
# ) & ) &
# clh_track_last_background_job clh_track_last_background_job
p1=$!
# clh_get_tracked_pids (
# assert [ ${#result[@]} -eq 2 ] while true; do
sleep 0.1
done
) &
clh_track_last_background_job
p2=$!
# touch "${OUTPUT_FOLDER}/stop" clh_get_tracked_pids
assert [ ${#result[@]} -eq 2 ]
# clh_get_tracked_pids kill -s TERM "$p1"
# assert [ ${#result[@]} -eq 0 ] kill -s TERM "$p2"
# }
echo "Kill issued" > killissued
# This will hang the bats runner for some reason.
await "$p1"
await "$p2"
# This should be more than enough for the process monitor to
# catch the exits. The alternative would be implementing temporal
# predicates.
sleep 3
clh_get_tracked_pids
assert [ ${#result[@]} -eq 0 ]
clh_stop_process_monitor "monitor_only"
}