diff --git a/src/codex.bash b/src/codex.bash index 43b14b1..c9e5e68 100644 --- a/src/codex.bash +++ b/src/codex.bash @@ -117,9 +117,14 @@ cdx_launch_node() { cdx_ensure_ready "$@" } +cdx_pid() { + local node_index="$1" + echo "${_cdx_pids[$node_index]}" +} + cdx_destroy_node() { local node_index="$1" wipe_data="${2:-false}" pid - pid="${_cdx_pids[$node_index]}" + pid="$(cdx_pid "$node_index")" if [ -z "$pid" ]; then echoerr "Error: no process ID for node $node_index" return 1 @@ -197,6 +202,15 @@ cdx_download_file() { -o "${_cdx_downloads}/codex-${node_index}/$cid" || return 1 } +cdx_download_file_async() { + ( + cdx_download_file "$@" + pm_job_exit $? + ) & + pm_track_last_job + echo $! +} + cdx_upload_sha1() { local node_index="$1" cid="$2" cat "${_cdx_uploads}/codex-${node_index}/${cid}.sha1" || return 1 diff --git a/src/procmon.bash b/src/procmon.bash index 7893f88..298473f 100644 --- a/src/procmon.bash +++ b/src/procmon.bash @@ -191,6 +191,11 @@ pm_kill_rec() { kill -s TERM "$descendant" 2> /dev/null || true done + # Tries to wait so processes are not left lingering. + for descendant in "${result[@]}"; do + await "$descendant" || echo "[procmon] failed to wait for process $descendant" + done + return 0 } diff --git a/src/utils.bash b/src/utils.bash index 94fd659..3405f02 100644 --- a/src/utils.bash +++ b/src/utils.bash @@ -18,6 +18,10 @@ clh_output_folder() { echo "${OUTPUTS}/$1" } +clh_clear_outputs() { + rm -rf "${OUTPUTS}" || true +} + echoerr() { echo "$@" >&2 } diff --git a/test/test_codex.bats b/test/test_codex.bats index 8aa5563..03bfb44 100755 --- a/test/test_codex.bats +++ b/test/test_codex.bats @@ -74,7 +74,7 @@ setup() { pm_stop } -@test "should upload and synchronously download file to Codex node" { +@test "should upload and synchronously download file from Codex node" { pm_start assert cdx_launch_node 0 @@ -83,8 +83,29 @@ setup() { filename=$(cdx_generate_file 10) cid=$(cdx_upload_file 0 "$filename") - cdx_download_file 0 "$cid" + assert cdx_download_file 0 "$cid" assert_equal $(sha1 "${filename}") $(cdx_download_sha1 0 "$cid") pm_stop +} + +@test "should upload and asynchronously download file from Codex node" { + pm_start + + assert cdx_launch_node 0 + assert cdx_ensure_ready 0 3 + + filename=$(cdx_generate_file 10) + cid=$(cdx_upload_file 0 "$filename") + + handle=$(cdx_download_file_async 0 "$cid") + await $handle 3 + + assert_equal $(sha1 "${filename}") $(cdx_download_sha1 0 "$cid") + + pm_stop +} + +teardown() { + clh_clear_outputs } \ No newline at end of file