Bug 683396 follow-up: Fix intermittent test failures
authorMarina Samuel <msamuel@mozilla.com>
Tue, 06 Sep 2011 19:15:28 -0700
changeset 76747 607edee2e848fd500712187bac05241a8cc34880
parent 76746 df706d14c53d65aa37aabb70533f2b3f95a2c9b1
child 76748 474cf6d733df8b733efe6a4a25f12e544525c5e4
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
bugs683396
milestone9.0a1
Bug 683396 follow-up: Fix intermittent test failures
services/sync/modules/policies.js
services/sync/tests/unit/test_errorhandler.js
--- a/services/sync/modules/policies.js
+++ b/services/sync/modules/policies.js
@@ -490,59 +490,63 @@ let ErrorHandler = {
 
         Status.engines = [engine_name, exception.failureCode || ENGINE_UNKNOWN_FAIL];
         this._log.debug(engine_name + " failed: " + Utils.exceptionStr(exception));
         break;
       case "weave:service:login:error":
         if (this.shouldReportError()) {
           this.resetFileLog(Svc.Prefs.get("log.appender.file.logOnError"),
                             LOG_PREFIX_ERROR);
-          Svc.Obs.notify("weave:ui:login:error");
+          this.notifyOnNextTick("weave:ui:login:error");
         } else {
-          Svc.Obs.notify("weave:ui:clear-error");
+          this.notifyOnNextTick("weave:ui:clear-error");
         }
 
         this.dontIgnoreErrors = false;
         break;
       case "weave:service:sync:error":
         if (Status.sync == CREDENTIALS_CHANGED) {
           Weave.Service.logout();
         }
 
         if (this.shouldReportError()) {
           this.resetFileLog(Svc.Prefs.get("log.appender.file.logOnError"),
                             LOG_PREFIX_ERROR);
-          Svc.Obs.notify("weave:ui:sync:error");
+          this.notifyOnNextTick("weave:ui:sync:error");
         } else {
-          Svc.Obs.notify("weave:ui:sync:finish");
+          this.notifyOnNextTick("weave:ui:sync:finish");
         }
 
         this.dontIgnoreErrors = false;
         break;
       case "weave:service:sync:finish":
         if (Status.service == SYNC_FAILED_PARTIAL) {
           this._log.debug("Some engines did not sync correctly.");
           this.resetFileLog(Svc.Prefs.get("log.appender.file.logOnError"),
                             LOG_PREFIX_ERROR);
 
           if (this.shouldReportError()) {
             this.dontIgnoreErrors = false;
-            Svc.Obs.notify("weave:ui:sync:error");
+            this.notifyOnNextTick("weave:ui:sync:error");
             break;
           }
         } else {
           this.resetFileLog(Svc.Prefs.get("log.appender.file.logOnSuccess"),
                             LOG_PREFIX_SUCCESS);
         }
         this.dontIgnoreErrors = false;
-        Svc.Obs.notify("weave:ui:sync:finish");
+        this.notifyOnNextTick("weave:ui:sync:finish");
         break;
     }
   },
 
+  notifyOnNextTick: function notifyOnNextTick(topic) {
+    Utils.nextTick(function() Svc.Obs.notify(topic));
+  },
+
   /**
    * Trigger a sync and don't muffle any errors, particularly network errors.
    */
   syncAndReportErrors: function syncAndReportErrors() {
     this._log.debug("Beginning user-triggered sync.");
 
     this.dontIgnoreErrors = true;
     Utils.nextTick(Weave.Service.sync, Weave.Service);
--- a/services/sync/tests/unit/test_errorhandler.js
+++ b/services/sync/tests/unit/test_errorhandler.js
@@ -106,16 +106,22 @@ function setUp() {
   Service.clusterURL = "http://localhost:8080/";
 
   generateNewKeys();
   let serverKeys = CollectionKeys.asWBO("crypto", "keys");
   serverKeys.encrypt(Service.syncKeyBundle);
   return serverKeys.upload(Service.cryptoKeysURL).success;
 }
 
+function clean() {
+  Service.startOver();
+  Status.resetSync();
+  Status.resetBackoff();
+}
+
 add_test(function test_401_logout() {
   let server = sync_httpd_setup();
   setUp();
 
   // By calling sync, we ensure we're logged in.
   Service.sync();
   do_check_eq(Status.sync, SYNC_SUCCEEDED);
   do_check_true(Service.isLoggedIn);
@@ -359,32 +365,32 @@ add_test(function test_shouldReportError
   };
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
   do_check_false(ErrorHandler.shouldReportError());
 
   // Clean up.
   Service.verifyLogin = Service._verifyLogin;
-  Service.startOver();
+  clean();
   server.stop(run_next_test);
 });
 
 add_test(function test_login_syncAndReportErrors_non_network_error() {
   // Test non-network errors are reported
   // when calling syncAndReportErrors
   let server = sync_httpd_setup();
   setUp();
   Service.password = "";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.login, LOGIN_FAILED_NO_PASSWORD);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_sync_syncAndReportErrors_non_network_error() {
@@ -399,17 +405,17 @@ add_test(function test_sync_syncAndRepor
   do_check_true(Service.isLoggedIn);
 
   generateCredentialsChangedFailure();
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, CREDENTIALS_CHANGED);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_login_syncAndReportErrors_prolonged_non_network_error() {
@@ -418,17 +424,17 @@ add_test(function test_login_syncAndRepo
   let server = sync_httpd_setup();
   setUp();
   Service.password = "";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.login, LOGIN_FAILED_NO_PASSWORD);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_sync_syncAndReportErrors_prolonged_non_network_error() {
@@ -443,17 +449,17 @@ add_test(function test_sync_syncAndRepor
   do_check_true(Service.isLoggedIn);
 
   generateCredentialsChangedFailure();
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, CREDENTIALS_CHANGED);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_login_syncAndReportErrors_network_error() {
@@ -462,17 +468,17 @@ add_test(function test_login_syncAndRepo
   Service.password = "ilovejane";
   Service.passphrase = "abcdeabcdeabcdeabcdeabcdea";
   Service.clusterURL = "http://localhost:8080/";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
 
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 
@@ -480,17 +486,17 @@ add_test(function test_sync_syncAndRepor
   // Test network errors are reported when calling syncAndReportErrors.
   Services.io.offline = true;
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
 
     Services.io.offline = false;
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_login_syncAndReportErrors_prolonged_network_error() {
@@ -498,19 +504,19 @@ add_test(function test_login_syncAndRepo
   // when calling syncAndReportErrors.
   Service.username = "johndoe";
   Service.password = "ilovejane";
   Service.passphrase = "abcdeabcdeabcdeabcdeabcdea";
   Service.clusterURL = "http://localhost:8080/";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
-    do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
+    do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
 
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_sync_syncAndReportErrors_prolonged_network_error() {
@@ -518,17 +524,17 @@ add_test(function test_sync_syncAndRepor
   // when calling syncAndReportErrors.
   Services.io.offline = true;
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
 
     Services.io.offline = false;
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
 
 add_test(function test_login_prolonged_non_network_error() {
@@ -536,17 +542,17 @@ add_test(function test_login_prolonged_n
   let server = sync_httpd_setup();
   setUp();
   Service.password = "";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_sync_prolonged_non_network_error() {
@@ -560,17 +566,17 @@ add_test(function test_sync_prolonged_no
   do_check_true(Service.isLoggedIn);
 
   generateCredentialsChangedFailure();
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_login_prolonged_network_error() {
@@ -579,34 +585,34 @@ add_test(function test_login_prolonged_n
   Service.password = "ilovejane";
   Service.passphrase = "abcdeabcdeabcdeabcdeabcdea";
   Service.clusterURL = "http://localhost:8080/";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_sync_prolonged_network_error() {
   // Test prolonged, network errors are reported
   Services.io.offline = true;
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
     Services.io.offline = false;
-    Service.startOver();
+    clean();
     run_next_test();
   });
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_login_non_network_error() {
@@ -614,42 +620,41 @@ add_test(function test_login_non_network
   let server = sync_httpd_setup();
   setUp();
   Service.password = "";
 
   Svc.Obs.add("weave:ui:login:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:login:error", onSyncError);
     do_check_eq(Status.login, LOGIN_FAILED_NO_PASSWORD);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
-
 add_test(function test_sync_non_network_error() {
   // Test non-network errors are reported
   let server = sync_httpd_setup();
   setUp();
 
   // By calling sync, we ensure we're logged in.
   Service.sync();
   do_check_eq(Status.sync, SYNC_SUCCEEDED);
   do_check_true(Service.isLoggedIn);
 
   generateCredentialsChangedFailure();
 
   Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
     Svc.Obs.remove("weave:ui:sync:error", onSyncError);
     do_check_eq(Status.sync, CREDENTIALS_CHANGED);
 
-    Service.startOver();
+    clean();
     server.stop(run_next_test);
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_login_network_error() {
@@ -659,37 +664,35 @@ add_test(function test_login_network_err
   Service.clusterURL = "http://localhost:8080/";
 
   // Test network errors are not reported.
   Svc.Obs.add("weave:ui:clear-error", function onClearError() {
     Svc.Obs.remove("weave:ui:clear-error", onClearError);
 
     do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
 
-    Service.startOver();
-    Status.resetSync();
     Services.io.offline = false;
+    clean();
     run_next_test();
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_sync_network_error() {
   // Test network errors are not reported.
   Services.io.offline = true;
 
   Svc.Obs.add("weave:ui:sync:finish", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:sync:finish", onUIUpdate);
     do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
 
-    Service.startOver();
     Services.io.offline = false;
-    Status.resetSync();
+    clean();
     run_next_test();
   });
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
 });
 
 add_test(function test_sync_server_maintenance_error() {
@@ -698,34 +701,36 @@ add_test(function test_sync_server_maint
   setUp();
 
   const BACKOFF = 42;
   let engine = Engines.get("catapult");
   engine.enabled = true;
   engine.exception = {status: 503,
                       headers: {"retry-after": BACKOFF}};
 
-  function onUIUpdate() {
+  function onSyncError() {
     do_throw("Shouldn't get here!");
   }
-  Svc.Obs.add("weave:ui:sync:error", onUIUpdate);
+  Svc.Obs.add("weave:ui:sync:error", onSyncError);
 
   do_check_eq(Status.service, STATUS_OK);
 
+  Svc.Obs.add("weave:ui:sync:finish", function onSyncFinish() {
+    Svc.Obs.remove("weave:ui:sync:finish", onSyncFinish);
+
+    do_check_eq(Status.service, SYNC_FAILED_PARTIAL);
+    do_check_eq(Status.sync, SERVER_MAINTENANCE);
+
+    Svc.Obs.remove("weave:ui:sync:error", onSyncError);
+    clean();
+    server.stop(run_next_test);
+  });
+
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
-
-  do_check_eq(Status.service, SYNC_FAILED_PARTIAL);
-  do_check_eq(Status.sync, SERVER_MAINTENANCE);
-
-  Svc.Obs.remove("weave:ui:sync:error", onUIUpdate);
-  Service.startOver();
-  Status.resetSync();
-  Status.resetBackoff();
-  server.stop(run_next_test);
 });
 
 add_test(function test_info_collections_login_server_maintenance_error() {
   // Test info/collections server maintenance errors are not reported.
   let server = sync_httpd_setup();
   setUp();
 
   Service.clusterURL = "http://localhost:8080/maintenance/";
@@ -740,30 +745,31 @@ add_test(function test_info_collections_
   function onUIUpdate() {
     do_throw("Shouldn't get here!");
   }
   Svc.Obs.add("weave:ui:login:error", onUIUpdate);
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
+  Svc.Obs.add("weave:ui:clear-error", function onLoginFinish() {
+    Svc.Obs.remove("weave:ui:clear-error", onLoginFinish);
+
+    do_check_true(Status.enforceBackoff);
+    do_check_eq(backoffInterval, 42);
+    do_check_eq(Status.service, LOGIN_FAILED);
+    do_check_eq(Status.login, SERVER_MAINTENANCE);
+
+    Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
+    clean();
+    server.stop(run_next_test);
+  });
+
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
-
-  do_check_true(Status.enforceBackoff);
-  do_check_eq(backoffInterval, 42);
-  do_check_eq(Status.service, LOGIN_FAILED);
-  do_check_eq(Status.login, SERVER_MAINTENANCE);
-
-  Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
-  Service.startOver();
-  Status.resetSync();
-  Status.resetBackoff();
-
-  server.stop(run_next_test);
 });
 
 add_test(function test_meta_global_login_server_maintenance_error() {
   // Test meta/global server maintenance errors are not reported.
   let server = sync_httpd_setup();
   setUp();
 
   Service.clusterURL = "http://localhost:8080/maintenance/";
@@ -778,30 +784,31 @@ add_test(function test_meta_global_login
   function onUIUpdate() {
     do_throw("Shouldn't get here!");
   }
   Svc.Obs.add("weave:ui:login:error", onUIUpdate);
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
+  Svc.Obs.add("weave:ui:clear-error", function onLoginFinish() {
+    Svc.Obs.remove("weave:ui:clear-error", onLoginFinish);
+
+    do_check_true(Status.enforceBackoff);
+    do_check_eq(backoffInterval, 42);
+    do_check_eq(Status.service, LOGIN_FAILED);
+    do_check_eq(Status.login, SERVER_MAINTENANCE);
+
+    Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
+    clean();
+    server.stop(run_next_test);
+  });
+
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
-
-  do_check_true(Status.enforceBackoff);
-  do_check_eq(backoffInterval, 42);
-  do_check_eq(Status.service, LOGIN_FAILED);
-  do_check_eq(Status.login, SERVER_MAINTENANCE);
-
-  Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
-  Service.startOver();
-  Status.resetSync();
-  Status.resetBackoff();
-
-  server.stop(run_next_test);
 });
 
 add_test(function test_crypto_keys_login_server_maintenance_error() {
   // Test crypto/keys server maintenance errors are not reported.
   let server = sync_httpd_setup();
   setUp();
 
   Service.clusterURL = "http://localhost:8080/maintenance/";
@@ -816,30 +823,31 @@ add_test(function test_crypto_keys_login
   function onUIUpdate() {
     do_throw("Shouldn't get here!");
   }
   Svc.Obs.add("weave:ui:login:error", onUIUpdate);
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
+  Svc.Obs.add("weave:ui:clear-error", function onLoginFinish() {
+    Svc.Obs.remove("weave:ui:clear-error", onLoginFinish);
+
+    do_check_true(Status.enforceBackoff);
+    do_check_eq(backoffInterval, 42);
+    do_check_eq(Status.service, LOGIN_FAILED);
+    do_check_eq(Status.login, SERVER_MAINTENANCE);
+
+    Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
+    clean();
+    server.stop(run_next_test);
+  });
+
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   Service.sync();
-
-  do_check_true(Status.enforceBackoff);
-  do_check_eq(backoffInterval, 42);
-  do_check_eq(Status.service, LOGIN_FAILED);
-  do_check_eq(Status.login, SERVER_MAINTENANCE);
-
-  Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
-  Service.startOver();
-  Status.resetSync();
-  Status.resetBackoff();
-
-  server.stop(run_next_test);
 });
 
 add_test(function test_sync_prolonged_server_maintenance_error() {
   // Test prolonged server maintenance errors are reported.
   let server = sync_httpd_setup();
   setUp();
 
   const BACKOFF = 42;
@@ -848,19 +856,17 @@ add_test(function test_sync_prolonged_se
   engine.exception = {status: 503,
                       headers: {"retry-after": BACKOFF}};
 
   Svc.Obs.add("weave:ui:sync:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:sync:error", onUIUpdate);
     do_check_eq(Status.service, SYNC_FAILED);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
 });
@@ -881,19 +887,17 @@ add_test(function test_info_collections_
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, SYNC_FAILED);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
@@ -915,19 +919,17 @@ add_test(function test_meta_global_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, SYNC_FAILED);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
@@ -949,19 +951,17 @@ add_test(function test_crypto_keys_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, SYNC_FAILED);
     do_check_eq(Status.sync, PROLONGED_SYNC_FAILURE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   Service.sync();
@@ -979,19 +979,17 @@ add_test(function test_sync_syncAndRepor
   engine.exception = {status: 503,
                       headers: {"retry-after": BACKOFF}};
 
   Svc.Obs.add("weave:ui:sync:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:sync:error", onUIUpdate);
     do_check_eq(Status.service, SYNC_FAILED_PARTIAL);
     do_check_eq(Status.sync, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
@@ -1013,19 +1011,17 @@ add_test(function test_info_collections_
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1048,19 +1044,17 @@ add_test(function test_meta_global_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1083,19 +1077,17 @@ add_test(function test_crypto_keys_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(NON_PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1113,19 +1105,17 @@ add_test(function test_sync_syncAndRepor
   engine.exception = {status: 503,
                       headers: {"retry-after": BACKOFF}};
 
   Svc.Obs.add("weave:ui:sync:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:sync:error", onUIUpdate);
     do_check_eq(Status.service, SYNC_FAILED_PARTIAL);
     do_check_eq(Status.sync, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetBackoff();
-    Status.resetSync();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
 });
@@ -1147,19 +1137,17 @@ add_test(function test_info_collections_
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1182,19 +1170,17 @@ add_test(function test_meta_global_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1217,19 +1203,17 @@ add_test(function test_crypto_keys_login
 
   Svc.Obs.add("weave:ui:login:error", function onUIUpdate() {
     Svc.Obs.remove("weave:ui:login:error", onUIUpdate);
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, 42);
     do_check_eq(Status.service, LOGIN_FAILED);
     do_check_eq(Status.login, SERVER_MAINTENANCE);
 
-    Service.startOver();
-    Status.resetSync();
-    Status.resetBackoff();
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_false(Status.enforceBackoff);
   do_check_eq(Status.service, STATUS_OK);
 
   setLastSync(PROLONGED_ERROR_DURATION);
   ErrorHandler.syncAndReportErrors();
@@ -1258,26 +1242,21 @@ add_test(function test_sync_engine_gener
 
     // Test Error log was written on SYNC_FAILED_PARTIAL.
     let entries = logsdir.directoryEntries;
     do_check_true(entries.hasMoreElements());
     let logfile = entries.getNext().QueryInterface(Ci.nsILocalFile);
     do_check_eq(logfile.leafName.slice(0, LOG_PREFIX_ERROR.length),
                 LOG_PREFIX_ERROR);
 
-    Status.resetSync();
-    Service.startOver();
-
-    server.stop(run_next_test);
+    clean();
   });
 
   do_check_eq(Status.engines["catapult"], undefined);
-
   do_check_true(setUp());
-
   Service.sync();
 });
 
 // This test should be the last one since it monkeypatches the engine object
 // and we should only have one engine object throughout the file (bug 629664).
 add_test(function test_engine_applyFailed() {
   let server = sync_httpd_setup();
 
@@ -1299,20 +1278,16 @@ add_test(function test_engine_applyFaile
 
     // Test Error log was written on SYNC_FAILED_PARTIAL.
     let entries = logsdir.directoryEntries;
     do_check_true(entries.hasMoreElements());
     let logfile = entries.getNext().QueryInterface(Ci.nsILocalFile);
     do_check_eq(logfile.leafName.slice(0, LOG_PREFIX_ERROR.length),
                 LOG_PREFIX_ERROR);
 
-    Status.resetSync();
-    Service.startOver();
-
+    clean();
     server.stop(run_next_test);
   });
 
   do_check_eq(Status.engines["catapult"], undefined);
-
   do_check_true(setUp());
-
   Service.sync();
 });