Bug 1258005 - Remove setTimeout to avoid intermittent issue. r=bkelly
authorMarcos Caceres <marcos@marcosc.com>
Tue, 12 Apr 2016 00:39:00 +0200
changeset 316564 a956dc9f9398e1b1a2f9b42aae624bca5ed407c5
parent 316563 2a143bf5387f494bf389bd686cda9ee52d3f63a8
child 316565 061ad34e7f0f8e87c114f7dd74fb287638a73851
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1258005
milestone48.0a1
Bug 1258005 - Remove setTimeout to avoid intermittent issue. r=bkelly
dom/security/test/csp/browser_test_web_manifest.js
--- a/dom/security/test/csp/browser_test_web_manifest.js
+++ b/dom/security/test/csp/browser_test_web_manifest.js
@@ -170,68 +170,55 @@ const tests = [
       url.searchParams.append("csp", "manifest-src https://example.com:443");
       return url.href;
     },
     run(manifest) {
       is(manifest.name, "loaded", this.expected);
     }
   },
 ];
+
 //jscs:disable
 add_task(function* () {
   //jscs:enable
   const testPromises = tests.map((test) => {
     const tabOptions = {
       gBrowser,
       url: test.tabURL,
       skipAnimation: true,
     };
     return BrowserTestUtils.withNewTab(tabOptions, (browser) => testObtainingManifest(browser, test));
   });
   yield Promise.all(testPromises);
 });
 
 function* testObtainingManifest(aBrowser, aTest) {
-  const expectsBlocked = aTest.expected.includes("block");
-  const observer = (expectsBlocked) ? createNetObserver(aTest) : null;
+  const waitForObserver = waitForNetObserver(aTest);
   // Expect an exception (from promise rejection) if there a content policy
   // that is violated.
   try {
     const manifest = yield ManifestObtainer.browserObtainManifest(aBrowser);
     aTest.run(manifest);
   } catch (e) {
     const wasBlocked = e.message.includes("NetworkError when attempting to fetch resource");
     ok(wasBlocked, `Expected promise rejection obtaining ${aTest.tabURL}: ${e.message}`);
-    if (observer) {
-      yield observer.untilFinished;
-    }
+  } finally {
+    yield waitForObserver;
   }
 }
 
-// Helper object used to observe policy violations. It waits 1 seconds
-// for a response, and then times out causing its associated test to fail.
-function createNetObserver(test) {
-  let finishedTest;
-  let success = false;
-  const finished = new Promise((resolver) => {
-    finishedTest = resolver;
-  });
-  const timeoutId = setTimeout(() => {
-    if (!success) {
-      test.run("This test timed out.");
-      finishedTest();
+// Helper object used to observe policy violations when blocking is expected.
+function waitForNetObserver(aTest) {
+  return new Promise((resolve) => {
+    // We don't need to wait for violation, so just resolve
+    if (!aTest.expected.includes("block")){
+      return resolve();
     }
-  }, 1000);
-  var observer = {
-    get untilFinished() {
-      return finished;
-    },
-    observe(subject, topic) {
-      SpecialPowers.removeObserver(observer, "csp-on-violate-policy");
-      test.run(topic);
-      finishedTest();
-      clearTimeout(timeoutId);
-      success = true;
-    },
-  };
-  SpecialPowers.addObserver(observer, "csp-on-violate-policy", false);
-  return observer;
-}
\ No newline at end of file
+    const observer = {
+      observe(subject, topic) {
+        SpecialPowers.removeObserver(observer, "csp-on-violate-policy");
+        aTest.run(topic);
+        resolve();
+      },
+    };
+    SpecialPowers.addObserver(observer, "csp-on-violate-policy", false);
+  });
+}