feat: add no-timeout option to await; fix minor bugs

This commit is contained in:
gmega 2025-06-27 08:23:28 -03:00
parent 2fde22116c
commit 29a39369eb
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
4 changed files with 38 additions and 5 deletions

View File

@ -56,7 +56,7 @@ for i in $(seq 1 "${repetitions}"); do
handles+=("$result")
done
await_all "${handles[@]}"
await_all "${handles[@]}" "Inf"
cdx_log_timings_end
done

View File

@ -209,6 +209,8 @@ pm_list_descendants() {
}
pm_async() {
_pm_assert_state "running" || return 1
proc_type=""
command=()
while [[ "$#" -gt 0 ]]; do
@ -227,7 +229,7 @@ pm_async() {
done
(
set +e
set -e
_pm_job_started "${BASHPID}" "$proc_type" "$@"
trap '_pm_job_exited "${BASHPID}" "$proc_type" "killed" "$@"' TERM
trap '_pm_job_exited "${BASHPID}" "$proc_type" "$?" "$@"' EXIT
@ -239,7 +241,7 @@ pm_async() {
await() {
local pid=$1 timeout=${2:-30} start="${SECONDS}"
while kill -0 "$pid" 2> /dev/null; do
if ((SECONDS - start > timeout)); then
if [ "$timeout" != 'Inf' ] && ((SECONDS - start > timeout)); then
echoerr "Error: timeout waiting for process $pid to exit"
return 1
fi

View File

@ -23,7 +23,6 @@ setup() {
done
assert [ "$found" = true ]
}
@test "should launch Codex nodes with metrics enabled when there is an experiment in scope" {

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bats
# shellcheck disable=SC2128
# shellcheck disable=SC2128,SC2317
setup() {
load test_helper/common_setup
common_setup
@ -10,6 +10,34 @@ setup() {
pm_set_outputs "${TEST_OUTPUTS}/pm"
}
@test "should allow awaiting for a process" {
job() {
sleep 0.1
}
pm_start
pm_async job
pid=$result
assert await "$pid"
pm_stop
}
@test "should allow awaiting for a process up until a timeout" {
job() {
sleep 5000
}
pm_start
pm_async job
pid=$result
refute await "$pid" 1
pm_stop
}
@test "should kill processes recursively" {
# Note that this is fragile. We need to structure
# the process tree such that the parent does not exit
@ -230,6 +258,8 @@ callback() {
@test "should allow passing custom arguments to lifecycle callback" {
pm_register_callback "sleepy" "callback"
pm_start
pm_async sleep 0.1 -%- "sleepy" "arg1" "arg2"
pid=$result
@ -237,4 +267,6 @@ callback() {
assert_equal "$(cat "${_pm_output}/${pid}-sleepy-start-args")" "arg1 arg2"
assert_equal "$(cat "${_pm_output}/${pid}-sleepy-exit-args")" "arg1 arg2"
pm_stop
}