Bug 1184458- TelemetryEnvironment needs to shut down properly r=gfritzsche
authorchaithanya <chaitanya7991@gmail.com>
Wed, 27 Jan 2016 09:57:00 +0100
changeset 326708 bae683bcf1560a24b869a71dcbdec5528bd155e4
parent 326707 0ecd7d72f232304da046b352c457e039e35ceed7
child 326709 1686b901daa4eb42e6bbae2e9ba23c9754c584da
push id10166
push users.kaspari@gmail.com
push dateThu, 28 Jan 2016 12:16:34 +0000
reviewersgfritzsche
bugs1184458
milestone47.0a1
Bug 1184458- TelemetryEnvironment needs to shut down properly r=gfritzsche
toolkit/components/telemetry/TelemetryController.jsm
toolkit/components/telemetry/TelemetryEnvironment.jsm
toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
--- a/toolkit/components/telemetry/TelemetryController.jsm
+++ b/toolkit/components/telemetry/TelemetryController.jsm
@@ -790,16 +790,17 @@ var Impl = {
 
     Preferences.ignore(PREF_BRANCH_LOG, configureLogging);
     this._detachObservers();
 
     // Now do an orderly shutdown.
     try {
       // Stop the datachoices infobar display.
       TelemetryReportingPolicy.shutdown();
+      TelemetryEnvironment.shutdown();
 
       // Stop any ping sending.
       yield TelemetrySend.shutdown();
 
       // First wait for clients processing shutdown.
       yield this._shutdownBarrier.wait();
 
       // ... and wait for any outstanding async ping activity.
--- a/toolkit/components/telemetry/TelemetryEnvironment.jsm
+++ b/toolkit/components/telemetry/TelemetryEnvironment.jsm
@@ -72,16 +72,20 @@ this.TelemetryEnvironment = {
   registerChangeListener: function(name, listener) {
     return getGlobal().registerChangeListener(name, listener);
   },
 
   unregisterChangeListener: function(name) {
     return getGlobal().unregisterChangeListener(name);
   },
 
+  shutdown: function() {
+    return getGlobal().shutdown();
+  },
+
   // Policy to use when saving preferences. Exported for using them in tests.
   RECORD_PREF_STATE: 1, // Don't record the preference value
   RECORD_PREF_VALUE: 2, // We only record user-set prefs.
 
   // Testing method
   _watchPreferences: function(prefMap) {
     return getGlobal()._watchPreferences(prefMap);
   },
@@ -794,16 +798,21 @@ EnvironmentCache.prototype = {
     this._log.trace("unregisterChangeListener for " + name);
     if (this._shutdown) {
       this._log.warn("registerChangeListener - already shutdown");
       return;
     }
     this._changeListeners.delete(name);
   },
 
+  shutdown: function() {
+    this._log.trace("shutdown");
+    this._shutdown = true;
+  },
+
   /**
    * Only used in tests, set the preferences to watch.
    * @param aPreferences A map of preferences names and their recording policy.
    */
   _watchPreferences: function (aPreferences) {
     this._stopWatchingPrefs();
     this._watchedPrefs = aPreferences;
     this._updateSettings();
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
@@ -1367,11 +1367,36 @@ add_task(function* test_defaultSearchEng
 
   // ... but that if a cohort identifier is set, we send it.
   Services.prefs.setCharPref("browser.search.cohort", "testcohort");
   Services.obs.notifyObservers(null, "browser-search-service", "init-complete");
   data = TelemetryEnvironment.currentEnvironment;
   Assert.equal(data.settings.searchCohort, "testcohort");
 });
 
+add_task(function* test_environmentShutdown() {
+  // Define and reset the test preference.
+  const PREF_TEST = "toolkit.telemetry.test.pref1";
+  const PREFS_TO_WATCH = new Map([
+    [PREF_TEST, {what: TelemetryEnvironment.RECORD_PREF_STATE}],
+  ]);
+  Preferences.reset(PREF_TEST);
+  gNow = futureDate(gNow, 10 * MILLISECONDS_PER_MINUTE);
+  fakeNow(gNow);
+
+  // Set up the preferences and listener, then the trigger shutdown
+  TelemetryEnvironment._watchPreferences(PREFS_TO_WATCH);
+  TelemetryEnvironment.registerChangeListener("test_environmentShutdownChange", () => {
+  // Register a new change listener that asserts if change is propogated
+    Assert.ok(false, "No change should be propagated after shutdown.");
+  });
+  TelemetryEnvironment.shutdown();
+
+  // Flipping  the test preference after shutdown should not trigger the listener
+  Preferences.set(PREF_TEST, 1);
+
+  // Unregister the listener.
+  TelemetryEnvironment.unregisterChangeListener("test_environmentShutdownChange");
+});
+
 add_task(function*() {
   do_test_finished();
 });