Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js. r?linclark draft
authorSami Jaktholm <sjakthol@outlook.com>
Fri, 25 Mar 2016 08:11:06 +0200
changeset 344642 321033fae2683c2157b08cb1d13cf145d6fcc416
parent 344570 aadf5e15faf8ba88b48d16bb396f115816b8f673
child 344643 cb43bdde5667a97d6fe93de5ef1e574d89cb0dcb
push id13891
push usersjakthol@outlook.com
push dateFri, 25 Mar 2016 06:18:45 +0000
reviewerslinclark
bugs1239920
milestone48.0a1
Bug 1239920 - Ignore unexpected requests in browser_console_netlogging.js. r?linclark 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