Bug 1452445 - Fix update error observer in unit test. r=gcp
authorFrancois Marier <francois@mozilla.com>
Tue, 29 May 2018 18:06:49 -0700
changeset 478627 e3d40506f24d88e3442f1856e886ab382812d8ef
parent 478626 295900f1ae690684391457c12a8b32c20458442a
child 478628 fc197074cafbdaf86b4a17e3e2d182675ad22885
push id1757
push userffxbld-merge
push dateFri, 24 Aug 2018 17:02:43 +0000
treeherdermozilla-release@736023aebdb1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1452445
milestone62.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 1452445 - Fix update error observer in unit test. r=gcp We should only observe for update errors while we are expecting a successful update. MozReview-Commit-ID: 3grGhmxqhIX
toolkit/components/url-classifier/tests/unit/head_urlclassifier.js
toolkit/components/url-classifier/tests/unit/test_hashcompleter_v4.js
toolkit/components/url-classifier/tests/unit/test_listmanager.js
--- a/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js
+++ b/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js
@@ -431,23 +431,33 @@ function waitUntilMetaDataSaved(expected
     if (!didCallback) {
       do_timeout(1000, waitUntilMetaDataSaved.bind(null, expectedState,
                                                          expectedChecksum,
                                                          callback));
     }
   });
 }
 
+var gUpdateFinishedObserverEnabled = false;
+var gUpdateFinishedObserver = function(aSubject, aTopic, aData) {
+  info("[" + aTopic + "] " + aData);
+  if (aData != "success") {
+    updateError(aData);
+  }
+};
+
 function throwOnUpdateErrors() {
-  Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
-    info("[" + aTopic + "] " + aData);
-    if (aData != "success") {
-      Services.obs.removeObserver(observer, aTopic);
-      updateError(aData);
-    }
-  }, "safebrowsing-update-finished");
+  Services.obs.addObserver(gUpdateFinishedObserver, "safebrowsing-update-finished");
+  gUpdateFinishedObserverEnabled = true;
+}
+
+function stopThrowingOnUpdateErrors() {
+  if (gUpdateFinishedObserverEnabled) {
+    Services.obs.removeObserver(gUpdateFinishedObserver, "safebrowsing-update-finished");
+    gUpdateFinishedObserverEnabled = false;
+  }
 }
 
 cleanUp();
 
 registerCleanupFunction(function() {
   cleanUp();
 });
--- a/toolkit/components/url-classifier/tests/unit/test_hashcompleter_v4.js
+++ b/toolkit/components/url-classifier/tests/unit/test_hashcompleter_v4.js
@@ -234,8 +234,12 @@ function run_test() {
 
   gHttpServV4 = new HttpServer();
   gHttpServV4.registerDirectory("/", do_get_cwd());
 
   registerHandlerUpdateV4();
   gHttpServV4.start(5555);
   run_next_test();
 }
+
+registerCleanupFunction(function() {
+  stopThrowingOnUpdateErrors();
+});
--- a/toolkit/components/url-classifier/tests/unit/test_listmanager.js
+++ b/toolkit/components/url-classifier/tests/unit/test_listmanager.js
@@ -187,18 +187,16 @@ add_test(function test_getGethashUrl() {
     equal(gListManager.getGethashUrl(t.tableName), t.gethashUrl);
   });
   equal(gListManager.getGethashUrl(TEST_TABLE_DATA_V4.tableName),
         TEST_TABLE_DATA_V4.gethashUrl);
   run_next_test();
 });
 
 function run_test() {
-  throwOnUpdateErrors();
-
   // Setup primary testing server.
   gHttpServ = new HttpServer();
   gHttpServ.registerDirectory("/", do_get_cwd());
 
   gHttpServ.registerPathHandler("/safebrowsing/update", function(request, response) {
     let body = NetUtil.readInputStreamToString(request.bodyInputStream,
                                                request.bodyInputStream.available());
 
@@ -299,22 +297,24 @@ function run_test() {
   gHttpServV4.start(5555);
 
   run_next_test();
 }
 
 // A trick to force updating tables. However, before calling this, we have to
 // call disableAllUpdates() first to clean up the updateCheckers in listmanager.
 function forceTableUpdate() {
+  throwOnUpdateErrors();
   Services.prefs.setCharPref(PREF_NEXTUPDATETIME, "1");
   Services.prefs.setCharPref(PREF_NEXTUPDATETIME_V4, "1");
   gListManager.maybeToggleUpdateChecking();
 }
 
 function disableAllUpdates() {
+  stopThrowingOnUpdateErrors();
   TEST_TABLE_DATA_LIST.forEach(t => gListManager.disableUpdate(t.tableName));
   gListManager.disableUpdate(TEST_TABLE_DATA_V4.tableName);
 }
 
 // Since there's no public interface on listmanager to know the update success,
 // we could only rely on the refresh of "nextupdatetime".
 function waitForUpdateSuccess(callback) {
   let nextupdatetime = parseInt(Services.prefs.getCharPref(PREF_NEXTUPDATETIME));