Skip to content

Commit 5605083

Browse files
committed
tests: fix race in csync2_u test harness helper function
1 parent fbcd517 commit 5605083

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tests/include.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,35 @@ require()
283283
284284
csync2_u()
285285
{
286+
local tmp kid now client_exit nc_exit server_exit
286287
if csync2 -N $1 -M > /dev/null; then
287288
csync2 -N $2 -iii -vvv &
288289
kid=$!
289290
291+
# try to avoid the connect-before-listen race,
292+
# wait for the expected listening socket to show up,
293+
# with timeout using bash magic $SECONDS.
294+
now=$SECONDS
295+
while ! ss -tnl src $2:csync2 | grep -q ^LISTEN; do
296+
kill -0 $kid
297+
(( SECONDS - now < 2 ))
298+
sleep 0.1
299+
done
300+
290301
csync2 -N $1 -uvvv
291302
client_exit=$?
292303
293-
# server still alive?
294-
# then no connection was made...
295-
# attempt do one now.
296-
tmp=$(nc $2 $CSYNC2_PORT <<<"BYE" )
297-
nc_exit=$?
298-
304+
if ss -tnl src $2:csync2 | grep -q ^LISTEN; then
305+
# server still alive?
306+
# then no connection was made...
307+
# attempt to do one now.
308+
tmp=$(nc $2 $CSYNC2_PORT <<<"BYE" )
309+
nc_exit=$?
310+
311+
[[ $nc_exit = 0 ]] || kill $kid
312+
else
313+
nc_exit=1
314+
fi
299315
wait $kid
300316
server_exit=$?
301317
[[ $client_exit = 0 && $server_exit = 0 && $nc_exit != 0 ]]

0 commit comments

Comments
 (0)