Bug 1678373 - Make the test more robust, and check constant/increasing accumulators a bit more. r=alwu

This was failing locally sometimes.  Resolving a Promise is not very
deterministic: it can take some time, or be almost instantaneous. It's simpler
to block the main thread for a time duration longer than the clock resolution of
the OS. Generally, the worst clock resolution is on Windows, it's by default
16ms, and resolving a Promise is often a lot faster than 16ms, so we could have
had the same value here, but the code was correct. The reason that it passed was
because of this code. This is currently changing a lot in Windows, but 30ms is a
good middle ground I thought. The clock resolution on macOS/Linux
desktop/Android/anything else such as BSDs is a lot better than 16ms (in the
microsecond/nanosecond range depending on the kernel), so we're good there.

Differential Revision: https://phabricator.services.mozilla.com/D125082
This commit is contained in:
Paul Adenot 2021-10-07 15:44:53 +00:00
Родитель 5b350b7fc5
Коммит 521f23c69b
1 изменённых файлов: 19 добавлений и 3 удалений

Просмотреть файл

@ -353,6 +353,18 @@ function returnTrueWhenAllValuesAreTrue(arr) {
return true;
}
// Block the main thread for a number of milliseconds
function blockMainThread(durationMS) {
const start = Date.now();
while (Date.now() - start < durationMS) { /* spin */ }
}
// Allows comparing two values from the system clocks that are not gathered
// atomically. Allow up to 1ms of fuzzing when lhs and rhs are seconds.
function timeFuzzyEquals(lhs, rhs, str) {
ok(Math.abs(lhs - rhs) < 1e-3, str);
}
function assertAttributeDefined(mediaChrome, checkType) {
ok(mediaChrome[checkType] != undefined, `${checkType} exists`);
}
@ -365,8 +377,9 @@ function assertValueEqualTo(mediaChrome, checkType, expectedValue) {
async function assertValueConstantlyIncreases(mediaChrome, checkType) {
assertAttributeDefined(mediaChrome, checkType);
const valueSnapshot = mediaChrome[checkType];
// Simply wait for a short while.
await new Promise(r => r());
// 30ms is long enough to have a low-resolution system clock tick, but short
// enough to not slow the test down.
blockMainThread(30);
const current = mediaChrome[checkType];
ok(current > valueSnapshot, `${checkType} keeps increasing (${current} > ${valueSnapshot})`);
}
@ -374,7 +387,10 @@ async function assertValueConstantlyIncreases(mediaChrome, checkType) {
function assertValueKeptUnchanged(mediaChrome, checkType) {
assertAttributeDefined(mediaChrome, checkType);
const valueSnapshot = mediaChrome[checkType];
is(mediaChrome[checkType], valueSnapshot, `${checkType} keeps unchanged (${valueSnapshot})`);
// 30ms is long enough to have a low-resolution system clock tick, but short
// enough to not slow the test down.
blockMainThread(30);
timeFuzzyEquals(mediaChrome[checkType], valueSnapshot, `${checkType} keeps unchanged (${valueSnapshot})`);
}
function assertAllProbeRelatedAttributesKeptUnchanged(video) {