Subject: | Unreliable t/usleep.t and t/utime.t tests |
t/usleep.t and t/utime.t fail randomly when built on loaded machine. Example from a perl-5.26.0 build:
# Failed test at t/usleep.t line 35.
../dist/threads-shared/t/waithires.t ............................... ok
../dist/threads/t/basic.t .......................................... ok
../dist/Time-HiRes/t/usleep.t ......................................
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/6 subtests
(less 1 skipped subtest: 4 okay)
# Looks like you failed 1 test of 6.
# Failed test 'File 1 atime set correctly'
# at t/utime.t line 113.
# '18.0852150917053'
# <
# '0.1'
# Failed test 'File 1 mtime set correctly'
# at t/utime.t line 114.
# '18.0852150917053'
# <
# '0.1'
# Failed test 'File 2 atime set correctly'
# at t/utime.t line 118.
# '18.0852150917053'
# <
# '0.1'
# Failed test 'File 2 mtime set correctly'
# at t/utime.t line 119.
# '18.0852150917053'
# <
# '0.1'
# Looks like you failed 4 tests of 18.
../dist/Time-HiRes/t/utime.t .......................................
Dubious, test returned 4 (wstat 1024, 0x400)
Failed 4/18 subtests
The reason is the tests retrieve current time, then perform an operation (usleep or utime) depending on CPU time, then retrieve current (or file) time and test the times difference falls into some range.
This is inherently unreliable because a the perl process can be suspended any time by a CPU scheduler for an unpredictable long period and then the delay influences the measured time.
I recommend to check only for lower bound of the range. I.e. If I test usleep(500_000) than I would test that the difference is bigger or equaled to 0.5 second (or 0.4 as you have in the current code). But not that the value is smaller than some arbitrary constant (0.9 in the current code).