My library has test cases based on real time, and I noticed that tests will randomly fail with 1 millisecond error:
expect(received).toBeGreaterThanOrEqual(expected) Expected: >= 1000 Received: 999
This seems to be due to setTimeout calling the function prematurely.
So I wrote a separate test script:
let last = Date.now() setTimeout(next, 1000) function next() { if (Date.now() - last < 1000) process.exit(1) last = Date.now() setTimeout(next, 1000) }
On Node.js v12.19.0, v14.15.3, v15.4.0, it will fail randomly: sometimes the script can continue to run, sometimes the script will exit soon. This is not only happening on my local computer, but also on Github's CI server.
My questions: Is this a bug? Or some kind of expected behavior of setTimeout? Or Date.now() - time
always needs to add 1 millisecond?
UPDATE: See also https://github.com/nodejs/node/issues/26578
https://stackoverflow.com/questions/65367086/settimeout-calling-the-function-prematurely December 19, 2020 at 01:48PM
没有评论:
发表评论