Bug 1658246 [wpt PR 24933] - WPT: Service Worker: Add unexpected state check to wait_for_state(), a=testonly
authorBlink WPT Bot <blink-w3c-test-autoroller@chromium.org>
Wed, 26 Aug 2020 08:55:45 +0000
changeset 546506 510688acfbadbc3d5b0f088aa6b421f9e50f07fb
parent 546505 e79096f7efbf773f5d9efe109bd763d064dbd7ff
child 546507 e90a613d62fe6a255bf01ae9af5786d49f09f7dd
push id37735
push userabutkovits@mozilla.com
push dateThu, 27 Aug 2020 21:29:40 +0000
treeherdermozilla-central@109f3a4de567 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1658246, 24933, 1000544, 2344440, 799561
milestone82.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1658246 [wpt PR 24933] - WPT: Service Worker: Add unexpected state check to wait_for_state(), a=testonly Automatic update from web-platform-tests WPT: Service Worker: Add unexpected state check to wait_for_state() (#24933) This patch makes wait_for_state() in test-helpers.sub.js for a service worker WPT test check an unexpected state after adding the event handler. Before this patch, wait_for_state() only checks the equivalence of the state to the specific state. It might wait forever if the worker unexpectedly skips the state or it's unexpectedly evicted. Bug: 1000544 Change-Id: I7025fad3ba25648888a762e16ae0ded63f9c4f9f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2344440 Commit-Queue: Makoto Shimazu <shimazu@chromium.org> Reviewed-by: Matt Falkenhagen <falken@chromium.org> Reviewed-by: Makoto Shimazu <shimazu@chromium.org> Cr-Commit-Position: refs/heads/master@{#799561} Co-authored-by: Yuta Kasai <kasai.yuta0810@gmail.com> -- wpt-commits: a28628a7f1cec962767531f77172bb93d77e6b44 wpt-pr: 24933
testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
--- a/testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
@@ -87,66 +87,76 @@ function wait_for_update(test, registrat
       var handler = test.step_func(function() {
         registration.removeEventListener('updatefound', handler);
         resolve(registration.installing);
       });
       registration.addEventListener('updatefound', handler);
     }));
 }
 
-function wait_for_state(test, worker, state) {
-  if (!worker || worker.state == undefined) {
-    return Promise.reject(new Error(
-      'wait_for_state must be passed a ServiceWorker'));
-  }
-  if (worker.state === state)
-    return Promise.resolve(state);
-
-  if (state === 'installing') {
-    switch (worker.state) {
+// Return true if |state_a| is more advanced than |state_b|.
+function is_state_advanced(state_a, state_b) {
+  if (state_b === 'installing') {
+    switch (state_a) {
       case 'installed':
       case 'activating':
       case 'activated':
       case 'redundant':
-        return Promise.reject(new Error(
-          'worker is ' + worker.state + ' but waiting for ' + state));
+        return true;
     }
   }
 
-  if (state === 'installed') {
-    switch (worker.state) {
+  if (state_b === 'installed') {
+    switch (state_a) {
       case 'activating':
       case 'activated':
       case 'redundant':
-        return Promise.reject(new Error(
-          'worker is ' + worker.state + ' but waiting for ' + state));
+        return true;
+    }
+  }
+
+  if (state_b === 'activating') {
+    switch (state_a) {
+      case 'activated':
+      case 'redundant':
+        return true;
     }
   }
 
-  if (state === 'activating') {
-    switch (worker.state) {
-      case 'activated':
+  if (state_b === 'activated') {
+    switch (state_a) {
       case 'redundant':
-        return Promise.reject(new Error(
-          'worker is ' + worker.state + ' but waiting for ' + state));
+        return true;
     }
   }
+  return false;
+}
 
-  if (state === 'activated') {
-    switch (worker.state) {
-      case 'redundant':
-        return Promise.reject(new Error(
-          'worker is ' + worker.state + ' but waiting for ' + state));
-    }
+function wait_for_state(test, worker, state) {
+  if (!worker || worker.state == undefined) {
+    return Promise.reject(new Error(
+      'wait_for_state needs a ServiceWorker object to be passed.'));
   }
+  if (worker.state === state)
+    return Promise.resolve(state);
 
-  return new Promise(test.step_func(function(resolve) {
+  if (is_state_advanced(worker.state, state)) {
+    return Promise.reject(new Error(
+      `Waiting for ${state} but the worker is already ${worker.state}.`));
+  }
+  return new Promise(test.step_func(function(resolve, reject) {
       worker.addEventListener('statechange', test.step_func(function() {
           if (worker.state === state)
             resolve(state);
+
+          if (is_state_advanced(worker.state, state)) {
+            reject(new Error(
+              `The state of the worker becomes ${worker.state} while waiting` +
+                `for ${state}.`));
+          }
         }));
     }));
 }
 
 // Declare a test that runs entirely in the ServiceWorkerGlobalScope. The |url|
 // is the service worker script URL. This function:
 // - Instantiates a new test with the description specified in |description|.
 //   The test will succeed if the specified service worker can be successfully