Closed marascio closed 6 years ago
After some debugging this seems to be related to https://github.com/sstephenson/bats/issues/80 from the old bats repository. The problem doesn't seem to be related to whether the output matches or doesn't, but simply that the last test case using background-process
hangs.
Applying this patch fixes the issue:
diff --git a/lib/bats/background-process b/lib/bats/background-process
index e39241c..ab3bd0c 100644
--- a/lib/bats/background-process
+++ b/lib/bats/background-process
@@ -64,7 +64,7 @@ run_in_background() {
export BATS_BACKGROUND_RUN_OUTPUT
BATS_BACKGROUND_RUN_OUTPUT="$BATS_TEST_ROOTDIR/background-run-output.txt"
printf '' >"$BATS_BACKGROUND_RUN_OUTPUT"
- "$@" >"$BATS_BACKGROUND_RUN_OUTPUT" 2>&1 &
+ "$@" >"$BATS_BACKGROUND_RUN_OUTPUT" 2>&1 3>&- &
export BATS_BACKGROUND_RUN_PID="$!"
restore_bats_shell_options
}
A revised test case run using go test
is:
#! /usr/bin/env bats
load environment
load "$_GO_CORE_DIR/lib/bats/background-process"
setup() {
mkdir "$BATS_TEST_ROOTDIR"
}
teardown() {
remove_bats_test_dirs
}
@test "$SUITE: found" {
skip_if_missing_background_utilities
run_in_background bash -c 'echo start; sleep 60;'
wait_for_background_output 'start'
stop_background_run
}
@test "$SUITE: not found" {
skip_if_missing_background_utilities
run_in_background bash -c 'echo start; sleep 60;'
wait_for_background_output 'fail'
stop_background_run
}
@test "$SUITE: found 2" {
skip_if_missing_background_utilities
run_in_background bash -c 'echo start; sleep 60;'
wait_for_background_output 'start'
stop_background_run
}
Without the above patch the last test case always hangs and is terminated after 60 seconds when the bash process exits. With the patch the last test is properly terminated after wait_for_background_output
matches. Were the last test case one that does not match it would terminate after the timeout in wait_for_background_output
.
Sorry I didn't get around to responding over Thanksgiving; will try to carve out some time to respond in the next evening or two.
In the meanwhile, you've done quite a bit of work already—feel free to convert it into a pull request if you like.
Finally gave this a try locally and confirmed your fix works. Thanks for digging in and digging up sstephenson/bats#80 so I didn't have to!
As for the example case at the top of the file, I think I got in the rhythm of always wrapping things in run_test_script_in_background
and forgot to both add test cases for the bare run_in_background
and to add example documentation for run_test_script_in_background
.
I have to run right now, but will put together a PR to add your new test cases and fix, to update the file documentation—and to cover a new corner case I discovered where running run_test_script_in_background "echo hi; sleep 60"
fails when it shouldn't.
Framework, Bash, and operating system version information
Description
When trying to use
background-process
based on the basic usage documentation at the top of a file, a simple test case hangs indefinitely if there is no match in the output.Consider the following, where
support/testfx
also sourcesbackground-process
andhelpers
.The output of the test is then:
The only reason the
fails
test exits is because thesleep 60
finally completes. In reality, it should be terminated much sooner afterwait_for_background_output
times out after 1 second. Leaving off the manual timeout and using the default timeout forwait_for_background_output
provides the same result.Looking through the
bats-background-process
test cases I don't see any that use the recommended usage pattern. They all seem to use a different pattern and even invokewait_for_background_output
using thebats
run
function.Are the docs out of date? Is this a bug? It sure seems like a bug since testing a background process implies the process should not have to exit for the test to work.