Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js;r=bgrins
authorSami Jaktholm <sjakthol@outlook.com>
Fri, 25 Mar 2016 08:11:06 +0200
changeset 290519 abf28569fff7fad6c1807471b2e747cd19a81dd3
parent 290452 d4ccb3062261c79044eb88f7f63d3afd80740979
child 290520 e04ad44074ebb9e04010a361116a0b189f7dccac
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1239920
milestone48.0a1
Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js;r=bgrins The problem here is that the test expects to receive a request for the |test-network-request.html| but the waitForFinishedRequest() accepts any request that happens to occur at the same time. In this particular bug, a GMP update is triggered during the test and the waitForFinishedRequest() promise resolves with that particular request which obviously isn't the one the test was expecting. These changes add an optional predicate function parameter to waitForFinishedRequest() which will be called for each completed request and the returned promise only resolves when the predicate returns true for a request. Also, browser_console_netlogging.js begins to use the aforementioned functionality by passing a predicate that only accepts the request for |test-network-request.html| effectively fixing the intermittent failure. MozReview-Commit-ID: AWhMRjrqP8i
devtools/client/webconsole/test/browser_console_netlogging.js
devtools/client/webconsole/test/head.js
--- a/devtools/client/webconsole/test/browser_console_netlogging.js
+++ b/devtools/client/webconsole/test/browser_console_netlogging.js
@@ -7,17 +7,20 @@
 
 "use strict";
 
 const TEST_NETWORK_REQUEST_URI =
   "http://example.com/browser/devtools/client/webconsole/test/" +
   "test-network-request.html";
 
 add_task(function* () {
-  let finishedRequest = waitForFinishedRequest();
+  let finishedRequest = waitForFinishedRequest(({ request }) => {
+    return request.url === TEST_NETWORK_REQUEST_URI;
+  });
+
   const hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI,
                                       "browserConsole");
   let request = yield finishedRequest;
 
   ok(request, "Page load was logged");
 
   let client = hud.ui.webConsoleClient;
   let args = [request.actor];
--- a/devtools/client/webconsole/test/head.js
+++ b/devtools/client/webconsole/test/head.js
@@ -1627,26 +1627,40 @@ function checkOutputForInputs(hud, input
 
   return Task.spawn(runner);
 }
 
 
 /**
  * Finish the request and resolve with the request object.
  *
+ * @param {Function} predicate A predicate function that takes the request
+ * object as an argument and returns true if the request was the expected one,
+ * false otherwise. The returned promise is resolved ONLY if the predicate
+ * matches a request. Defaults to accepting any request.
  * @return promise
  * @resolves The request object.
  */
-function waitForFinishedRequest() {
+function waitForFinishedRequest(predicate = () => true) {
   registerCleanupFunction(function() {
     HUDService.lastFinishedRequest.callback = null;
   });
 
   return new Promise(resolve => {
-    HUDService.lastFinishedRequest.callback = request => { resolve(request) };
+    HUDService.lastFinishedRequest.callback = request => {
+      // Check if this is the expected request
+      if (predicate(request)) {
+        // Match found. Clear the listener.
+        HUDService.lastFinishedRequest.callback = null;
+
+        resolve(request);
+      } else {
+        info(`Ignoring unexpected request ${JSON.stringify(request, null, 2)}`);
+      }
+    }
   });
 }
 
 /**
  * Wait for eventName on target.
  * @param {Object} target An observable object that either supports on/off or
  * addEventListener/removeEventListener
  * @param {String} eventName