Bug 1335180 - Fix race in browser_ext_tabs_reload_bypass_cache. r=mixedpuppy, a=test-only
authorKris Maglione <maglione.k@gmail.com>
Tue, 14 Mar 2017 09:20:17 -0700
changeset 395394 0b865b771d0dedcbd9041b2957859a0a5104037f
parent 395393 073e0a5f1fd515d258064c3622de8f79187f15ba
child 395395 4b70731f4113cc16c00dd43530b2c3baa432c97a
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy, test-only
bugs1335180
milestone54.0a2
Bug 1335180 - Fix race in browser_ext_tabs_reload_bypass_cache. r=mixedpuppy, a=test-only MozReview-Commit-ID: FXqRHwZUll6
browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js
--- a/browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js
@@ -7,39 +7,49 @@ add_task(function* () {
     manifest: {
       "permissions": ["tabs", "<all_urls>"],
     },
 
     async background() {
       const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
       const URL = BASE + "file_bypass_cache.sjs";
 
-      function awaitLoad(tabId) {
-        return new Promise(resolve => {
-          browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
-            if (tabId == tabId_ && changed.status == "complete" && tab.url == URL) {
-              browser.tabs.onUpdated.removeListener(listener);
-              resolve();
-            }
-          });
+      let tabId = null;
+      let loadPromise, resolveLoad;
+      function resetLoad() {
+        loadPromise = new Promise(resolve => {
+          resolveLoad = resolve;
         });
       }
+      function awaitLoad() {
+        return loadPromise.then(() => {
+          resetLoad();
+        });
+      }
+      resetLoad();
+
+      browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
+        if (tabId == tabId_ && changed.status == "complete" && tab.url == URL) {
+          resolveLoad();
+        }
+      });
 
       try {
         let tab = await browser.tabs.create({url: URL});
-        await awaitLoad(tab.id);
+        tabId = tab.id;
+        await awaitLoad();
 
         await browser.tabs.reload(tab.id, {bypassCache: false});
-        await awaitLoad(tab.id);
+        await awaitLoad();
 
         let [textContent] = await browser.tabs.executeScript(tab.id, {code: "document.body.textContent"});
         browser.test.assertEq("", textContent, "`textContent` should be empty when bypassCache=false");
 
         await browser.tabs.reload(tab.id, {bypassCache: true});
-        await awaitLoad(tab.id);
+        await awaitLoad();
 
         [textContent] = await browser.tabs.executeScript(tab.id, {code: "document.body.textContent"});
 
         let [pragma, cacheControl] = textContent.split(":");
         browser.test.assertEq("no-cache", pragma, "`pragma` should be set to `no-cache` when bypassCache is true");
         browser.test.assertEq("no-cache", cacheControl, "`cacheControl` should be set to `no-cache` when bypassCache is true");
 
         await browser.tabs.remove(tab.id);