Bug 1430182 - Remove racy code from test_file_upload.html r=dom-workers-and-storage-reviewers,sg
authorYaron Tausky <ytausky@mozilla.com>
Wed, 15 Jan 2020 09:23:52 +0000
changeset 510370 c35bb210b8ae793c844bd94c1848d246bf601293
parent 510369 826012956299e269c87e9372e0e73d8e468e92da
child 510371 259f059c93cb57edaa5b8367e9eac5325fbe70dc
push id37020
push userccoroiu@mozilla.com
push dateWed, 15 Jan 2020 21:36:21 +0000
treeherdermozilla-central@c35bb210b8ae [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, sg
bugs1430182
milestone74.0a1
first release with
nightly linux32
c35bb210b8ae / 74.0a1 / 20200115213621 / files
nightly linux64
c35bb210b8ae / 74.0a1 / 20200115213621 / files
nightly win32
c35bb210b8ae / 74.0a1 / 20200115213621 / files
nightly win64
c35bb210b8ae / 74.0a1 / 20200115213621 / files
nightly mac
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly win32
nightly win64
Bug 1430182 - Remove racy code from test_file_upload.html r=dom-workers-and-storage-reviewers,sg This test contains a few race conditions that could cause it to hang and time out. This commit removes them, hopefully solving the intermittent failures. Differential Revision: https://phabricator.services.mozilla.com/D59918
dom/serviceworkers/test/test_file_upload.html
dom/serviceworkers/test/utils.js
--- a/dom/serviceworkers/test/test_file_upload.html
+++ b/dom/serviceworkers/test/test_file_upload.html
@@ -14,29 +14,30 @@
 <input id="input" type="file">
 <script class="testbody" type="text/javascript">
 
 async function onOpened(message) {
   let input = document.getElementById("input");
   SpecialPowers.wrap(input).mozSetFileArray([message.file]);
   script.destroy();
 
-  let registration = await navigator.serviceWorker.register('sw_file_upload.js',
-                                                            {scope: "." });
-  await waitForState(registration.installing, 'activated');
+  let reg = await navigator.serviceWorker.register('sw_file_upload.js',
+                                                   {scope: "." });
+  let serviceWorker = reg.installing || reg.waiting || reg.active;
+  await waitForState(serviceWorker, 'activated');
 
   let res = await fetch('server_file_upload.sjs', {
     method: 'POST',
     body: input.files[0],
   });
 
   let data = await res.clone().text();
   ok(data.length > 0, "We have data!");
 
-  await registration.unregister();
+  await reg.unregister();
   SimpleTest.finish();
 }
 
 let url = SimpleTest.getTestFileURL("script_file_upload.js");
 let script = SpecialPowers.loadChromeScript(url);
 
 SimpleTest.waitForExplicitFinish();
 SpecialPowers.pushPrefEnv({"set": [
--- a/dom/serviceworkers/test/utils.js
+++ b/dom/serviceworkers/test/utils.js
@@ -1,20 +1,23 @@
 function waitForState(worker, state, context) {
   return new Promise(resolve => {
-    if (worker.state === state) {
-      resolve(context);
-      return;
-    }
-    worker.addEventListener("statechange", function onStateChange() {
+    function onStateChange() {
       if (worker.state === state) {
         worker.removeEventListener("statechange", onStateChange);
         resolve(context);
       }
-    });
+    }
+
+    // First add an event listener, so we won't miss any change that happens
+    // before we check the current state.
+    worker.addEventListener("statechange", onStateChange);
+
+    // Now check if the worker is already in the desired state.
+    onStateChange();
   });
 }
 
 /**
  * Helper for browser tests to issue register calls from the content global and
  * wait for the SW to progress to the active state, as most tests desire.
  * From the ContentTask.spawn, use via
  * `content.wrappedJSObject.registerAndWaitForActive`.