Bug 1173709 - Fix test failures and add test coverage. r=gfritzsche
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Mon, 15 Jun 2015 07:58:00 +0200
changeset 248949 cc9acd02073ce768db4eb7d5124f5d7199ade689
parent 248948 a8a08a33b4bf153f0aa11931f2db0ef1903773a2
child 248950 fbf0fdd5facef147113166594959274f82f4a6c8
push id28913
push usercbook@mozilla.com
push dateTue, 16 Jun 2015 13:53:56 +0000
treeherdermozilla-central@95b628befd28 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche
bugs1173709
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1173709 - Fix test failures and add test coverage. r=gfritzsche
toolkit/components/telemetry/TelemetrySend.jsm
toolkit/components/telemetry/tests/unit/test_TelemetrySendOldPings.js
--- a/toolkit/components/telemetry/TelemetrySend.jsm
+++ b/toolkit/components/telemetry/TelemetrySend.jsm
@@ -203,27 +203,34 @@ this.TelemetrySend = {
   },
 
   /**
    * Only used in tests.
    */
   setServer: function(server) {
     return TelemetrySendImpl.setServer(server);
   },
+
+  /**
+   * Only used in tests to wait on outgoing pending pings.
+   */
+  testWaitOnOutgoingPings: function() {
+    return TelemetrySendImpl.promisePendingPingActivity();
+  },
 };
 
 let TelemetrySendImpl = {
   _sendingEnabled: false,
   _logger: null,
   // Timer for scheduled ping sends.
   _pingSendTimer: null,
   // This tracks all pending ping requests to the server.
   _pendingPingRequests: new Map(),
-  // This is a private barrier blocked by pending async ping activity (sending & saving).
-  _connectionsBarrier: new AsyncShutdown.Barrier("TelemetrySend: Waiting for pending ping activity"),
+  // This tracks all the pending async ping activity.
+  _pendingPingActivity: new Set(),
   // This is true when running in the test infrastructure.
   _testMode: false,
 
   // Count of pending pings we discarded for age on startup.
   _discardedPingsCount: 0,
   // Count of pending pings we evicted for being over the limit on startup.
   _evictedPingsCount: 0,
   // Count of pending pings that were overdue.
@@ -345,17 +352,17 @@ let TelemetrySendImpl = {
     // We can't send anymore now.
     this._sendingEnabled = false;
 
     // Clear scheduled ping sends.
     this._clearPingSendTimer();
     // Cancel any outgoing requests.
     yield this._cancelOutgoingRequests();
     // ... and wait for any outstanding async ping activity.
-    yield this._connectionsBarrier.wait();
+    yield this.promisePendingPingActivity();
   }),
 
   reset: function() {
     this._log.trace("reset");
 
     this._overduePingCount = 0;
     this._discardedPingsCount = 0;
     this._evictedPingsCount = 0;
@@ -697,11 +704,25 @@ let TelemetrySendImpl = {
     }
   },
 
   /**
    * Track any pending ping send and save tasks through the promise passed here.
    * This is needed to block shutdown on any outstanding ping activity.
    */
   _trackPendingPingTask: function (promise) {
-    this._connectionsBarrier.client.addBlocker("Waiting for ping task", promise);
+    let clear = () => this._pendingPingActivity.delete(promise);
+    promise.then(clear, clear);
+    this._pendingPingActivity.add(promise);
+  },
+
+  /**
+   * Return a promise that allows to wait on pending pings.
+   * @return {Object<Promise>} A promise resolved when all the pending pings promises
+   *         are resolved.
+   */
+  promisePendingPingActivity: function () {
+    this._log.trace("promisePendingPingActivity - Waiting for ping task");
+    return Promise.all([for (p of this._pendingPingActivity) p.catch(ex => {
+      this._log.error("promisePendingPingActivity - ping activity had an error", ex);
+    })]);
   },
 };
--- a/toolkit/components/telemetry/tests/unit/test_TelemetrySendOldPings.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetrySendOldPings.js
@@ -314,16 +314,17 @@ add_task(function* test_overdue_old_form
   yield TelemetryStorage.savePing(PING_NO_PAYLOAD, true);
   yield TelemetryStorage.savePingToFile(PING_NO_SLUG, PING_FILES_PATHS[3], true);
 
   for (let f in PING_FILES_PATHS) {
     yield File.setDates(PING_FILES_PATHS[f], null, Date.now() - OVERDUE_PING_FILE_AGE);
   }
 
   yield TelemetryController.reset();
+  yield TelemetrySend.testWaitOnOutgoingPings();
   assertReceivedPings(OLD_FORMAT_PINGS);
 
   // |TelemetryStorage.cleanup| doesn't know how to remove a ping with no slug or id,
   // so remove it manually so that the next test doesn't fail.
   yield OS.File.remove(PING_FILES_PATHS[3]);
 
   yield clearPendingPings();
 });
@@ -340,16 +341,17 @@ add_task(function* test_overdue_pings_tr
     { num: OVERDUE_PINGS, age: OVERDUE_PING_FILE_AGE },
   ];
   let pings = yield createSavedPings(pingTypes);
   let recentPings = pings.slice(0, RECENT_PINGS);
   let expiredPings = pings.slice(RECENT_PINGS, RECENT_PINGS + EXPIRED_PINGS);
   let overduePings = pings.slice(-OVERDUE_PINGS);
 
   yield TelemetryController.reset();
+  yield TelemetrySend.testWaitOnOutgoingPings();
   assertReceivedPings(TOTAL_EXPECTED_PINGS);
 
   yield assertNotSaved(recentPings);
   yield assertNotSaved(expiredPings);
   yield assertNotSaved(overduePings);
 
   yield clearPendingPings();
 });
@@ -393,16 +395,17 @@ add_task(function* test_overdue_old_form
     // Make sure the version in the query string matches the old ping format version.
     let params = request.queryString.split("&");
     Assert.ok(params.find(p => p == "v=1"));
 
     receivedPings++;
   });
 
   yield TelemetryController.reset();
+  yield TelemetrySend.testWaitOnOutgoingPings();
   Assert.equal(receivedPings, 1, "We must receive a ping in the old format.");
 
   yield clearPendingPings();
 });
 
 add_task(function* teardown() {
   yield stopHttpServer();
 });