From 7ea95b7df3405229a6683a4884aabc8b42c878bd Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 15 Nov 2021 19:07:19 +0100 Subject: [PATCH] tests: fix intermittent $RANDOM reseeding fails (re: af6a32d1) When testing whether subshell $RANDOM reseeding worked, checking for non-identical numbers is not sufficient. There is no check for randomly occurring duplicate numbers, nor can there be, because subshells cannot (or, in the case of virtual subshells, should not) influence each other or the parent shell. src/cmd/ksh93/tests/variables.sh: - Try up to three times, tolerating identical numbers twice. --- src/cmd/ksh93/tests/variables.sh | 65 ++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/src/cmd/ksh93/tests/variables.sh b/src/cmd/ksh93/tests/variables.sh index 45aa0895c..45a5defde 100755 --- a/src/cmd/ksh93/tests/variables.sh +++ b/src/cmd/ksh93/tests/variables.sh @@ -34,46 +34,60 @@ fi # When the $RANDOM variable is used in a forked subshell, it shouldn't # use the same pseudorandom seed as the main shell. # https://github.com/ksh93/ksh/issues/285 +# These tests sometimes fail as duplicate numbers can occur randomly, so try up to $N times. +integer N=3 i rand1 rand2 RANDOM=123 function rand_print { ulimit -t unlimited 2> /dev/null print $RANDOM } -integer rand1=$(rand_print) -integer rand2=$(rand_print) +for((i=0; i /dev/null - test $RANDOM - print $RANDOM -) -integer rand2=${ print $RANDOM ;} +for((i=0; i /dev/null + test $RANDOM + print $RANDOM + ) + rand2=${ print $RANDOM ;} + ((rand1 != rand2)) && break +done (( rand1 == rand2 )) && err_exit "Test 2: \$RANDOM seed in subshell doesn't change" \ "(both results are $rand1)" # $RANDOM should be reseeded when the final command is inside of a subshell -rand1=$($SHELL -c 'RANDOM=1; (echo $RANDOM)') -rand2=$($SHELL -c 'RANDOM=1; (echo $RANDOM)') +for((i=0; ir1 -( echo $RANDOM & ) >r2 -integer giveup=0 -trap '((giveup++))' USR1 -(sleep 2; kill -s USR1 $$) & -while [[ ! -s r1 || ! -s r2 ]] -do ((giveup)) && break +for((i=0; i|r1 + ( echo $RANDOM & ) >|r2 + integer giveup=0 + trap '((giveup++))' USR1 + (sleep 2; kill -s USR1 $$) & + while [[ ! -s r1 || ! -s r2 ]] + do ((giveup)) && break + done + if ((giveup)) + then err_exit "Test 4: ( echo $RANDOM & ) does not write output" + fi + kill $! 2>/dev/null + trap - USR1 + unset giveup + [[ $(/dev/null -trap - USR1 -unset giveup +[[ $(