-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test hangs when trying basic usage of background-process when output does not match #226
Comments
After some debugging this seems to be related to sstephenson/bats#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 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
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 |
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 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 |
Closes #226. From the comment within `run_in_background`: Bats duplicates standard output as file descriptor 3 so that output from its framework functions isn't captured along with any output from the code under test. If the code under test contains a `sleep` or other blocking operation, this file descriptor will be held open until the process becomes unblocked, preventing Bats from exiting. Hence, we explicitly close file descriptor 3. Any other code running under Bats that opens a background process should close this file descriptor as well. See: sstephenson/bats#80 Much thanks to @marascio for discovering and researching the problem, and proposing the actual fix.
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.
The text was updated successfully, but these errors were encountered: