1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

LibJS: Add a function to ensure calls are made within the same second

Before these tests could be flaky if they happened to be called around
the edge of a second. Now we try up to 5 times to execute the tests
while staying within the same second.
This commit is contained in:
davidot 2022-12-02 17:25:40 +01:00 committed by Linus Groh
parent 146d45a652
commit cf0d30add6
3 changed files with 41 additions and 12 deletions

View file

@ -1,6 +1,7 @@
var describe;
var test;
var expect;
var withinSameSecond;
// Stores the results of each test and suite. Has a terrible
// name to avoid name collision.
@ -611,4 +612,26 @@ class ExpectationError extends Error {
duration: 0,
};
};
withinSameSecond = callback => {
let callbackDuration;
for (let tries = 0; tries < 5; tries++) {
const start = Temporal.Now.instant();
const result = callback();
const end = Temporal.Now.instant();
if (start.epochSeconds !== end.epochSeconds) {
callbackDuration = start.until(end);
continue;
}
return result;
}
throw new ExpectationError(
`Tried to execute callback '${callback}' 5 times within the same second but ` +
`failed. Make sure the callback does as little work as possible (the last run ` +
`took ${callbackDuration.total(
"milliseconds"
)} ms) and the machine is not overloaded. If you see this ` +
`error appearing in the CI it is most likely a flaky failure!`
);
};
})();