Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js. r=bgrins, a=test-only
authorSami Jaktholm <sjakthol@outlook.com>
Fri, 25 Mar 2016 08:11:06 +0200
changeset 323764 bf4bb009194db162e9368863a4533bb9788cfd94
parent 323763 85ba0f8d7f904b8c29bfaf3b4fe81778ca8bdc28
child 323765 69aa935f881de82592f98d9e425d63e0daa4bd58
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins, test-only
bugs1239920
milestone47.0a2
Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js. r=bgrins, a=test-only 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