From 4e1532195c13d11b3798eca353e5cea828343eb1 Mon Sep 17 00:00:00 2001 From: Andrew Sutherland Date: Wed, 23 Oct 2024 16:06:22 +0000 Subject: [PATCH] Bug 1926132 - condprof should check registered SWs at start of test. r=jmaher Differential Revision: https://phabricator.services.mozilla.com/D226589 --- .../mochitest/tests/SimpleTest/SimpleTest.js | 20 +++++++------------ testing/mochitest/tests/SimpleTest/setup.js | 19 +++++++++++++++++- .../content/SpecialPowersChild.sys.mjs | 4 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/testing/mochitest/tests/SimpleTest/SimpleTest.js b/testing/mochitest/tests/SimpleTest/SimpleTest.js index 16f5c3ae8a13..10af4b7d01d2 100644 --- a/testing/mochitest/tests/SimpleTest/SimpleTest.js +++ b/testing/mochitest/tests/SimpleTest/SimpleTest.js @@ -1494,16 +1494,14 @@ SimpleTest.finish = function () { ); } } else if (workers.length) { - let FULL_PROFILE_WORKERS_TO_IGNORE = []; + let FULL_PROFILE_WORKERS_TO_IGNORE = new Set(); if (parentRunner.conditionedProfile) { // Full profile has service workers in the profile, without clearing the - // profile service workers will be leftover. We perform a startsWith - // check below because some origins (s.0cf.io) use a cache-busting query - // parameter. - FULL_PROFILE_WORKERS_TO_IGNORE = [ - "https://www.youtube.com/sw.js", - "https://s.0cf.io/sw.js", - ]; + // profile service workers will be leftover. + for (const knownWorker of parentRunner.conditionedProfile + .knownServiceWorkers) { + FULL_PROFILE_WORKERS_TO_IGNORE.add(knownWorker.scriptSpec); + } } else { SimpleTest.ok( false, @@ -1512,11 +1510,7 @@ SimpleTest.finish = function () { } for (let worker of workers) { - if ( - FULL_PROFILE_WORKERS_TO_IGNORE.some(ignoreBase => - worker.scriptSpec.startsWith(ignoreBase) - ) - ) { + if (FULL_PROFILE_WORKERS_TO_IGNORE.has(worker.scriptSpec)) { continue; } SimpleTest.ok( diff --git a/testing/mochitest/tests/SimpleTest/setup.js b/testing/mochitest/tests/SimpleTest/setup.js index 2a8509bc0af2..1cad7ce4838c 100644 --- a/testing/mochitest/tests/SimpleTest/setup.js +++ b/testing/mochitest/tests/SimpleTest/setup.js @@ -228,7 +228,24 @@ if (params.timeoutAsPass) { } if (params.conditionedProfile) { - TestRunner.conditionedProfile = true; + TestRunner.conditionedProfile = { + knownServiceWorkers: null, + }; + // Asynchronously populate knownServiceWorkers above. Because we only check + // this list after awaiting a different call to registeredServiceWorkers() in + // SimpleTest.js's afterCleanup, we are guaranteed that the list will be + // populated before we check it. + // + // That said, the question is whether the list was sampled before the test + // could start and add a ServiceWorker. And the answer is mainly yes because + // the request will make it to the parent process main thread before any call + // to register() can get there with very high probability. (We are dealing + // with different top-level protocols so there are some theoretical + // opportunities for pathological scheduling but practically speaking it is + // very unlikely to happen.) + SpecialPowers.registeredServiceWorkers(/* aForce */ true).then(workers => { + TestRunner.conditionedProfile.knownServiceWorkers = workers; + }); } if (params.comparePrefs) { diff --git a/testing/specialpowers/content/SpecialPowersChild.sys.mjs b/testing/specialpowers/content/SpecialPowersChild.sys.mjs index 97ab52356204..2644eb5d0f10 100644 --- a/testing/specialpowers/content/SpecialPowersChild.sys.mjs +++ b/testing/specialpowers/content/SpecialPowersChild.sys.mjs @@ -475,10 +475,10 @@ export class SpecialPowersChild extends JSWindowActorChild { return this.sendQuery("Ping").then(aCallback); } - async registeredServiceWorkers() { + async registeredServiceWorkers(aForceCheck) { // Please see the comment in SpecialPowersParent.sys.mjs above // this._serviceWorkerListener's assignment for what this returns. - if (this._serviceWorkerRegistered) { + if (this._serviceWorkerRegistered || aForceCheck) { // This test registered at least one service worker. Send a synchronous // call to the parent to make sure that it called unregister on all of its // service workers.