diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2014-01-16 10:51:30 -0500 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2014-01-16 10:51:30 -0500 |
commit | a0414ef0949eaf66c467abd5009790a6f339b164 (patch) | |
tree | ef24eaa56f23c9e0a7b51c97649fcbc1168af20a /test/strftime.awk | |
parent | 699b3a96bfcdd281b4f0db447fcf80ba7fbc6ec6 (diff) | |
download | egawk-a0414ef0949eaf66c467abd5009790a6f339b164.tar.gz egawk-a0414ef0949eaf66c467abd5009790a6f339b164.tar.bz2 egawk-a0414ef0949eaf66c467abd5009790a6f339b164.zip |
Fix strftime test race condition.
Diffstat (limited to 'test/strftime.awk')
-rw-r--r-- | test/strftime.awk | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/test/strftime.awk b/test/strftime.awk index 775cd4e5..a52957f0 100644 --- a/test/strftime.awk +++ b/test/strftime.awk @@ -2,18 +2,36 @@ # # input is the output of `date', see Makefile.in # -# The mucking about with $0 and $N is to avoid problems +# The mucking about with $0 and $NF is to avoid problems # on cygwin, where the timezone field is empty and there # are two consecutive blanks. -# Additional mucking about to lop off the seconds field; -# helps decrease chance of difference due to a second boundary +BEGIN { + maxtries = 10 + datecmd = "date" + fmt = "%a %b %d %H:%M:%S %Z %Y" -{ - $3 = sprintf("%02d", $3 + 0) - $4 = substr($4, 1, 5) - print > "strftime.ok" - $0 = strftime("%a %b %d %H:%M %Z %Y") + # loop until before equals after, thereby protecting + # against a race condition where the seconds field might have + # incremented between running date and strftime + i = 0 + while (1) { + if (++i > maxtries) { + printf "Warning: this system is so slow that after %d attempts, we could never get two sequential invocations of strftime to give the same result!\n", maxtries > "/dev/stderr" + break + } + before = strftime(fmt) + datecmd | getline sd + after = strftime(fmt) + close(datecmd) + if (before == after) { + if (i > 1) + printf "Notice: it took %d loops to get the before and after strftime values to match\n", i > "/dev/stderr" + break + } + } + print sd > "strftime.ok" + $0 = after $NF = $NF print > OUTPUT } |