From eadf835794d7a4087d9bd366661364d5d8119afa Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Mon, 3 Jun 2013 01:29:17 +0100 Subject: tests: avoid a race in tail --retry testing Prompted by the continuous integration build failure at: http://hydra.nixos.org/build/5221053 * tests/tail-2/retry.sh: Ensure the 'out' file is truncated, as it's used to arbitrate the run order of commands. Relying on the truncation in the background tail command is racy because the truncation can occur after the fork of the background shell and thus wait4lines would not wait for output to occur in 'out', which would mean that the 'missing' file could be populated by the time tail(1) gets to process it. --- tests/tail-2/retry.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'tests/tail-2') diff --git a/tests/tail-2/retry.sh b/tests/tail-2/retry.sh index d56d4c169..7ba773475 100644 --- a/tests/tail-2/retry.sh +++ b/tests/tail-2/retry.sh @@ -26,7 +26,7 @@ wait4lines_ () { local delay=$1 local elc=$2 # Expected line count. - [ $( wc -l < out ) -ge $elc ] || { sleep $delay; return 1; } + [ "$( wc -l < out )" -ge "$elc" ] || { sleep $delay; return 1; } } # === Test: @@ -44,9 +44,11 @@ grep -F 'tail: warning: --retry ignored' out || fail=1 # === Test: # Ensure that "tail --retry --follow=name" waits for the file to appear. +# Clear 'out' so that we can check its contents without races +:>out || framework_failure_ timeout 10 tail -s.1 --follow=name --retry missing >out 2>&1 & pid=$! retry_delay_ wait4lines_ .1 6 1 || fail=1 # Wait for "cannot open" error. -echo "X" > missing || fail=1 # Write "X" into 'missing'. +echo "X" > missing || fail=1 retry_delay_ wait4lines_ .1 6 3 || fail=1 # Wait for the expected output. kill $pid wait $pid @@ -55,14 +57,14 @@ wait $pid grep -F 'cannot open' out || { fail=1; cat out; } grep -F 'has become accessible' out || { fail=1; cat out; } grep '^X$' out || { fail=1; cat out; } -rm -f missing out || fail=1 +rm -f missing out || framework_failure_ # === Test: # Ensure that "tail --retry --follow=descriptor" waits for the file to appear. # tail-8.21 failed at this (since the implementation of the inotify support). timeout 10 tail -s.1 --follow=descriptor --retry missing >out 2>&1 & pid=$! retry_delay_ wait4lines_ .1 6 2 || fail=1 # Wait for "cannot open" error. -echo "X" > missing || fail=1 # Write "X" into 'missing'. +echo "X" > missing || fail=1 retry_delay_ wait4lines_ .1 6 4 || fail=1 # Wait for the expected output. kill $pid wait $pid @@ -73,7 +75,7 @@ grep -F 'retry only effective for the initial open' out \ grep -F 'cannot open' out || { fail=1; cat out; } grep -F 'has appeared' out || { fail=1; cat out; } grep '^X$' out || { fail=1; cat out; } -rm -f missing out || fail=1 +rm -f missing out || framework_failure_ # === Test: # Ensure that tail --follow=descriptor --retry exits when the file appears @@ -91,7 +93,7 @@ grep -F 'cannot open' out || { fail=1; cat out; } grep -F 'replaced with an untailable file' out || { fail=1; cat out; } grep -F 'no files remaining' out || { fail=1; cat out; } [ $rc = 1 ] || { fail=1; cat out; } -rm -fd missing out || fail=1 +rm -fd missing out || framework_failure_ # === Test: # Ensure that --follow=descriptor (without --retry) does *not wait* for the -- cgit v1.2.3-54-g00ecf