summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2022-12-02 17:25:40 +0100
committerLinus Groh <mail@linusgroh.de>2022-12-03 23:04:08 +0000
commitcf0d30add65716fae8a386da24973caa05b8db5e (patch)
tree873fcc1e99c4f13838c49ab994b9e6567c4f217e /Userland
parent146d45a652fb6740431d79c3bb843a28eac95535 (diff)
downloadserenity-cf0d30add65716fae8a386da24973caa05b8db5e.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js18
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTimeISO.js12
-rw-r--r--Userland/Libraries/LibJS/Tests/test-common.js23
3 files changed, 41 insertions, 12 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js
index 65ba7fbf3a..5b1c26115f 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js
@@ -37,12 +37,15 @@ describe("correct behavior", () => {
},
};
- const plainDateTime = Temporal.Now.plainDateTime(calendar, "UTC");
- const plainDateTimeWithOffset = Temporal.Now.plainDateTime(calendar, timeZone);
+ const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
+ return [
+ Temporal.Now.plainDateTime(calendar, "UTC"),
+ Temporal.Now.plainDateTime(calendar, timeZone),
+ ];
+ });
if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
- // Let's hope the duration between the above two lines is less than a second :^)
const differenceSeconds =
plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
plainDateTimeToEpochSeconds(plainDateTime);
@@ -58,12 +61,15 @@ describe("correct behavior", () => {
},
};
- const plainDateTime = Temporal.Now.plainDateTime(calendar, "UTC");
- const plainDateTimeWithOffset = Temporal.Now.plainDateTime(calendar, timeZone);
+ const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
+ return [
+ Temporal.Now.plainDateTime(calendar, "UTC"),
+ Temporal.Now.plainDateTime(calendar, timeZone),
+ ];
+ });
if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
- // Let's hope the duration between the above two lines is less than a second :^)
const differenceSeconds =
plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
plainDateTimeToEpochSeconds(plainDateTime);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTimeISO.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTimeISO.js
index 587deb2f29..a9f49ff357 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTimeISO.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTimeISO.js
@@ -35,12 +35,12 @@ describe("correct behavior", () => {
},
};
- const plainDateTime = Temporal.Now.plainDateTimeISO("UTC");
- const plainDateTimeWithOffset = Temporal.Now.plainDateTimeISO(timeZone);
+ const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
+ return [Temporal.Now.plainDateTimeISO("UTC"), Temporal.Now.plainDateTimeISO(timeZone)];
+ });
if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
- // Let's hope the duration between the above two lines is less than a second :^)
const differenceSeconds =
plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
plainDateTimeToEpochSeconds(plainDateTime);
@@ -55,12 +55,12 @@ describe("correct behavior", () => {
},
};
- const plainDateTime = Temporal.Now.plainDateTimeISO("UTC");
- const plainDateTimeWithOffset = Temporal.Now.plainDateTimeISO(timeZone);
+ const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
+ return [Temporal.Now.plainDateTimeISO("UTC"), Temporal.Now.plainDateTimeISO(timeZone)];
+ });
if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
- // Let's hope the duration between the above two lines is less than a second :^)
const differenceSeconds =
plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
plainDateTimeToEpochSeconds(plainDateTime);
diff --git a/Userland/Libraries/LibJS/Tests/test-common.js b/Userland/Libraries/LibJS/Tests/test-common.js
index 8d1ea280a0..0c177e7280 100644
--- a/Userland/Libraries/LibJS/Tests/test-common.js
+++ b/Userland/Libraries/LibJS/Tests/test-common.js
@@ -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!`
+ );
+ };
})();