0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-25 17:04:12 -05:00

test(js): false negative in sleep test

It is entirely possible that the difference between the specified
sleep time and the actual sleep time is greater than 15 seconds.

https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#Notes

> Note that in either case, the actual delay may be longer than
> intended; see Reasons for delays longer than specified below.

It is however an error for the delay to be shorter.

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  web_src/js/utils.test.js > sleep
AssertionError: expected false to be truthy
 ❯ testSleep web_src/js/utils.test.js:192:48
    190|   const endTime = Date.now();    // Record the end time
    191|   const actualSleepTime = endTime - startTime;
    192|   expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy();
       |                                                ^
    193| }
    194|
 ❯ web_src/js/utils.test.js:184:3

(cherry picked from commit a0d9d8d576)
This commit is contained in:
Earl Warren 2024-07-02 19:29:04 +02:00 committed by GitHub
parent 8fec0822e4
commit 9230d787ff

View file

@ -177,10 +177,6 @@ test('serializeXml', () => {
}); });
test('sleep', async () => { test('sleep', async () => {
// Test 500 ms sleep
await testSleep(500);
// Test 2000 ms sleep
await testSleep(2000); await testSleep(2000);
}); });
@ -189,5 +185,5 @@ async function testSleep(ms) {
await sleep(ms); await sleep(ms);
const endTime = Date.now(); // Record the end time const endTime = Date.now(); // Record the end time
const actualSleepTime = endTime - startTime; const actualSleepTime = endTime - startTime;
expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy(); expect(actualSleepTime >= ms).toBeTruthy();
} }