Merge places with mozilla-central. a=blockers
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Thu, 10 Feb 2011 20:18:23 -0800
changeset 62339 8d2ed27ac04a23199e1226f6575a1c3e178f5e92
parent 62331 935706ff9e55464d340ec438d42a858a23078097 (current diff)
parent 62338 8930fec7d1c6828c7b7819736bd7b0ea63dc1b45 (diff)
child 62340 c76d7a190b12ce5130d1c1035b31b3d55ec5603d
push id1
push userroot
push dateTue, 10 Dec 2013 15:46:25 +0000
reviewersblockers
milestone2.0b12pre
Merge places with mozilla-central. a=blockers
--- a/services/sync/modules/engines/history.js
+++ b/services/sync/modules/engines/history.js
@@ -664,29 +664,35 @@ HistoryTracker.prototype = {
   _upScore: function BMT__upScore() {
     this.score += 1;
   },
 
   onVisit: function HT_onVisit(uri, vid, time, session, referrer, trans) {
     if (this.ignoreAll)
       return;
     this._log.trace("onVisit: " + uri.spec);
-    if (this.addChangedID(this._GUIDForUri(uri, true)))
-      this._upScore();
+    let self = this;
+    Utils.delay(function() {
+      if (self.addChangedID(self._GUIDForUri(uri, true))) {
+        self._upScore();
+      }
+    }, 0);
   },
   onDeleteVisits: function onDeleteVisits() {
   },
   onPageExpired: function HT_onPageExpired(uri, time, entry) {
   },
   onBeforeDeleteURI: function onBeforeDeleteURI(uri) {
     if (this.ignoreAll)
       return;
     this._log.trace("onBeforeDeleteURI: " + uri.spec);
-    if (this.addChangedID(this._GUIDForUri(uri, true)))
+    let self = this;
+    if (this.addChangedID(this._GUIDForUri(uri, true))) {
       this._upScore();
+    }
   },
   onDeleteURI: function HT_onDeleteURI(uri) {
   },
   onClearHistory: function HT_onClearHistory() {
     this._log.trace("onClearHistory");
     this.score += 500;
   }
 };
--- a/services/sync/tests/unit/test_history_tracker.js
+++ b/services/sync/tests/unit/test_history_tracker.js
@@ -1,49 +1,104 @@
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/history.js");
 Cu.import("resource://services-sync/util.js");
 
+function onScoreUpdated(callback) {
+  Svc.Obs.add("weave:engine:score:updated", function observer() {
+    Svc.Obs.remove("weave:engine:score:updated", observer);
+    try {
+      callback();
+    } catch (ex) {
+      do_throw(ex);
+    }
+  });
+}
+
 function run_test() {
   Engines.register(HistoryEngine);
-  let tracker = Engines.get("history")._tracker;
+  let engine = Engines.get("history");
+  let tracker = engine._tracker;
 
   _("Verify we've got an empty tracker to work with.");
   do_check_eq([id for (id in tracker.changedIDs)].length, 0);
 
   let _counter = 0;
   function addVisit() {
     Svc.History.addVisit(Utils.makeURI("http://getfirefox.com/" + _counter),
                          Date.now() * 1000, null, 1, false, 0);
     _counter += 1;
   }
 
-  try {
+  do_test_pending();
+  asyncChainTests(function (next) {
+
     _("Create history item. Won't show because we haven't started tracking yet");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
 
     _("Tell the tracker to start tracking changes.");
+    onScoreUpdated(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 1);
+      next();
+    });
+    Svc.Obs.notify("weave:engine:start-tracking");
+    addVisit();
+
+  }, function (next) {
+
+    _("Notifying twice won't do any harm.");
+    onScoreUpdated(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 2);
+      next();
+    });
     Svc.Obs.notify("weave:engine:start-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 1);
+
+  }, function (next) {
+
+    _("Deletions are tracked.");
+    let uri = Utils.makeURI("http://getfirefox.com/0");
+    let guid = engine._store.GUIDForUri(uri);
+    do_check_false(guid in tracker.changedIDs);
 
-    _("Notifying twice won't do any harm.");
-    Svc.Obs.notify("weave:engine:start-tracking");
-    addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 2);
+    onScoreUpdated(function() {
+      do_check_true(guid in tracker.changedIDs);
+      do_check_eq([id for (id in tracker.changedIDs)].length, 3);
+      next();
+    });
+    Svc.History.removePage(uri);
+
+  }, function (next) {
 
     _("Let's stop tracking again.");
     tracker.clearChangedIDs();
     Svc.Obs.notify("weave:engine:stop-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
 
     _("Notifying twice won't do any harm.");
     Svc.Obs.notify("weave:engine:stop-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
-  } finally {
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
+
     _("Clean up.");
     Svc.History.removeAllPages();
-  }
+    do_test_finished();
+
+  })();
 }
--- a/services/sync/tests/unit/test_service_sync_checkServerError.js
+++ b/services/sync/tests/unit/test_service_sync_checkServerError.js
@@ -16,121 +16,134 @@ CatapultEngine.prototype = {
   __proto__: SyncEngine.prototype,
   exception: null, // tests fill this in
   sync: function sync() {
     throw this.exception;
   }
 };
 
 function sync_httpd_setup() {
-  let handlers = {};
-  handlers["/1.0/johndoe/info/collections"]
-      = (new ServerWBO("collections", {})).handler(),
-  handlers["/1.0/johndoe/storage/clients"]
-      = (new ServerCollection()).handler();
-  handlers["/1.0/johndoe/storage/crypto"]
-      = (new ServerCollection()).handler();
-  handlers["/1.0/johndoe/storage/crypto/keys"]
-      = (new ServerWBO("keys", {})).handler();
-  handlers["/1.0/johndoe/storage/crypto/clients"]
-      = (new ServerWBO("clients", {})).handler();
-  handlers["/1.0/johndoe/storage/meta/global"]
-      = (new ServerWBO("global", {})).handler();
+  let collectionsHelper = track_collections_helper();
+  let upd = collectionsHelper.with_updated_collection;
+  let collections = collectionsHelper.collections;
+
+  let catapultEngine = Engines.get("catapult");
+  let engines        = {catapult: {version: catapultEngine.version,
+                                   syncID:  catapultEngine.syncID}}
+
+  // Track these using the collections helper, which keeps modified times
+  // up-to-date.
+  let clientsColl = new ServerCollection({}, true);
+  let keysWBO     = new ServerWBO("keys");
+  let globalWBO   = new ServerWBO("global", {storageVersion: STORAGE_VERSION,
+                                             syncID: Utils.makeGUID(),
+                                             engines: engines});
+
+  let handlers = {
+    "/1.0/johndoe/info/collections":    collectionsHelper.handler,
+    "/1.0/johndoe/storage/meta/global": upd("meta",    globalWBO.handler()),
+    "/1.0/johndoe/storage/clients":     upd("clients", clientsColl.handler()),
+    "/1.0/johndoe/storage/crypto/keys": upd("crypto",  keysWBO.handler())
+  }
   return httpd_setup(handlers);
 }
 
 function setUp() {
   Service.username = "johndoe";
   Service.password = "ilovejane";
   Service.passphrase = "aabcdeabcdeabcdeabcdeabcde";
   Service.clusterURL = "http://localhost:8080/";
   new FakeCryptoService();
 }
 
+function generateAndUploadKeys() {
+  CollectionKeys.generateNewKeys();
+  let serverKeys = CollectionKeys.asWBO("crypto", "keys");
+  serverKeys.encrypt(Weave.Service.syncKeyBundle);
+  return serverKeys.upload("http://localhost:8080/1.0/johndoe/storage/crypto/keys").success;
+}
+
 function test_backoff500(next) {
   _("Test: HTTP 500 sets backoff status.");
   let server = sync_httpd_setup();
   setUp();
 
-  Engines.register(CatapultEngine);
   let engine = Engines.get("catapult");
   engine.enabled = true;
   engine.exception = {status: 500};
 
   try {
     do_check_false(Status.enforceBackoff);
-    
+
     // Forcibly create and upload keys here -- otherwise we don't get to the 500!
-    CollectionKeys.generateNewKeys();
-    do_check_true(CollectionKeys.asWBO().upload("http://localhost:8080/1.0/johndoe/storage/crypto/keys").success);
-    
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
     do_check_true(Status.enforceBackoff);
   } finally {
-    Engines.unregister("catapult");
     Status.resetBackoff();
     Service.startOver();
   }
   server.stop(next);
 }
 
 function test_backoff503(next) {
   _("Test: HTTP 503 with Retry-After header leads to backoff notification and sets backoff status.");
   let server = sync_httpd_setup();
   setUp();
 
   const BACKOFF = 42;
-  Engines.register(CatapultEngine);
   let engine = Engines.get("catapult");
   engine.enabled = true;
   engine.exception = {status: 503,
                       headers: {"retry-after": BACKOFF}};
 
   let backoffInterval;
   Svc.Obs.add("weave:service:backoff:interval", function (subject) {
     backoffInterval = subject;
   });
 
   try {
     do_check_false(Status.enforceBackoff);
 
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
 
     do_check_true(Status.enforceBackoff);
     do_check_eq(backoffInterval, BACKOFF);
   } finally {
-    Engines.unregister("catapult");
     Status.resetBackoff();
     Service.startOver();
   }
   server.stop(next);
 }
 
 function test_overQuota(next) {
   _("Test: HTTP 400 with body error code 14 means over quota.");
   let server = sync_httpd_setup();
   setUp();
 
-  Engines.register(CatapultEngine);
   let engine = Engines.get("catapult");
   engine.enabled = true;
   engine.exception = {status: 400,
                       toString: function() "14"};
 
   try {
     do_check_eq(Status.sync, SYNC_SUCCEEDED);
 
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
 
     do_check_eq(Status.sync, OVER_QUOTA);
   } finally {
-    Engines.unregister("catapult");
     Status.resetSync();
     Service.startOver();
   }
   server.stop(next);
 }
 
 function test_service_networkError(next) {
   _("Test: Connection refused error from Service.sync() leads to the right status code.");
@@ -177,19 +190,25 @@ function test_service_offline(next) {
 }
 
 function test_service_reset_ignorableErrorCount(next) {
   _("Test: Successful sync resets the ignorable error count.");
   let server = sync_httpd_setup();
   setUp();
   Service._ignorableErrorCount = 10;
 
+  // Disable the engine so that sync completes.
+  let engine = Engines.get("catapult");
+  engine.enabled = false;
+
   try {
     do_check_eq(Status.sync, SYNC_SUCCEEDED);
 
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
 
     do_check_eq(Status.sync, SYNC_SUCCEEDED);
     do_check_eq(Service._ignorableErrorCount, 0);
   } finally {
     Status.resetSync();
     Service.startOver();
@@ -198,71 +217,75 @@ function test_service_reset_ignorableErr
 }
 
 function test_engine_networkError(next) {
   _("Test: Network related exceptions from engine.sync() lead to the right status code.");
   let server = sync_httpd_setup();
   setUp();
   Service._ignorableErrorCount = 0;
 
-  Engines.register(CatapultEngine);
   let engine = Engines.get("catapult");
   engine.enabled = true;
   engine.exception = Components.Exception("NS_ERROR_UNKNOWN_HOST",
                                           Cr.NS_ERROR_UNKNOWN_HOST);
 
   try {
     do_check_eq(Status.sync, SYNC_SUCCEEDED);
 
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
 
     do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
     do_check_eq(Service._ignorableErrorCount, 1);
   } finally {
-    Engines.unregister("catapult");
     Status.resetSync();
     Service.startOver();
   }
   server.stop(next);
 }
 
 // Slightly misplaced test as it doesn't actually test checkServerError,
 // but the observer for "weave:engine:sync:apply-failed".
 function test_engine_applyFailed(next) {
   let server = sync_httpd_setup();
   setUp();
 
-  Engines.register(CatapultEngine);
   let engine = Engines.get("catapult");
   engine.enabled = true;
+  delete engine.exception;
   engine.sync = function sync() {
     Svc.Obs.notify("weave:engine:sync:apply-failed", {}, "steam");
   };
 
   try {
     do_check_eq(Status.engines["steam"], undefined);
 
+    do_check_true(generateAndUploadKeys());
+
     Service.login();
     Service.sync();
 
     do_check_eq(Status.engines["steam"], ENGINE_APPLY_FAIL);
   } finally {
-    Engines.unregister("catapult");
     Status.resetSync();
     Service.startOver();
   }
   server.stop(next);
 }
 
 function run_test() {
   if (DISABLE_TESTS_BUG_604565)
     return;
 
   do_test_pending();
+
+  // Register engine once.
+  Engines.register(CatapultEngine);
   asyncChainTests(test_backoff500,
                   test_backoff503,
                   test_overQuota,
                   test_service_networkError,
                   test_service_offline,
                   test_service_reset_ignorableErrorCount,
                   test_engine_networkError,
                   test_engine_applyFailed,