Bug 792546 - Part 1: Move rotary engine to a testing-only JS module; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 24 Sep 2012 13:42:31 -0700
changeset 111092 234c791284378113405cf1dbb0744fe19e1b7f75
parent 111091 c9c1d7126858879fca53be667b1404c2b342c80b
child 111093 da562c88998fd83cd62eebecbfdd900d159db25e
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs792546
milestone18.0a1
Bug 792546 - Part 1: Move rotary engine to a testing-only JS module; r=rnewman
services/sync/Makefile.in
services/sync/modules-testing/rotaryengine.js
services/sync/tests/unit/head_helpers.js
services/sync/tests/unit/test_engine_abort.js
services/sync/tests/unit/test_hmac_error.js
services/sync/tests/unit/test_load_modules.js
services/sync/tests/unit/test_node_reassignment.js
services/sync/tests/unit/test_score_triggers.js
services/sync/tests/unit/test_syncengine_sync.js
--- a/services/sync/Makefile.in
+++ b/services/sync/Makefile.in
@@ -57,16 +57,20 @@ sync_engine_modules := \
   tabs.js \
   $(NULL)
 
 sync_stage_modules := \
   cluster.js \
   enginesync.js \
   $(NULL)
 
+sync_testing_modules := \
+  rotaryengine.js \
+  $(NULL)
+
 DIRS += locales
 TEST_DIRS += tests
 
 EXTRA_COMPONENTS := \
   SyncComponents.manifest \
   Weave.js \
   $(NULL)
 
@@ -80,9 +84,12 @@ INSTALL_TARGETS += SYNC_MAIN
 SYNC_ENGINES_FILES := $(addprefix modules/engines/,$(sync_engine_modules))
 SYNC_ENGINES_DEST = $(FINAL_TARGET)/modules/services-sync/engines
 INSTALL_TARGETS += SYNC_ENGINES
 
 SYNC_STAGES_FILES := $(addprefix modules/stages/,$(sync_stage_modules))
 SYNC_STAGES_DEST = $(FINAL_TARGET)/modules/services-sync/stages
 INSTALL_TARGETS += SYNC_STAGES
 
+TESTING_JS_MODULES := $(addprefix modules-testing/,$(sync_testing_modules))
+TESTING_JS_MODULE_DIR := services/sync
+
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/services/sync/modules-testing/rotaryengine.js
@@ -0,0 +1,124 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const EXPORTED_SYMBOLS = [
+  "RotaryEngine",
+  "RotaryRecord",
+  "RotaryStore",
+  "RotaryTracker",
+];
+
+const {utils: Cu} = Components;
+
+Cu.import("resource://services-sync/engines.js");
+Cu.import("resource://services-sync/record.js");
+Cu.import("resource://services-sync/util.js");
+
+/*
+ * A fake engine implementation.
+ * This is used all over the place.
+ *
+ * Complete with record, store, and tracker implementations.
+ */
+
+function RotaryRecord(collection, id) {
+  CryptoWrapper.call(this, collection, id);
+}
+RotaryRecord.prototype = {
+  __proto__: CryptoWrapper.prototype
+};
+Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]);
+
+function RotaryStore(engine) {
+  Store.call(this, "Rotary", engine);
+  this.items = {};
+}
+RotaryStore.prototype = {
+  __proto__: Store.prototype,
+
+  create: function create(record) {
+    this.items[record.id] = record.denomination;
+  },
+
+  remove: function remove(record) {
+    delete this.items[record.id];
+  },
+
+  update: function update(record) {
+    this.items[record.id] = record.denomination;
+  },
+
+  itemExists: function itemExists(id) {
+    return (id in this.items);
+  },
+
+  createRecord: function createRecord(id, collection) {
+    let record = new RotaryRecord(collection, id);
+
+    if (!(id in this.items)) {
+      record.deleted = true;
+      return record;
+    }
+
+    record.denomination = this.items[id] || "Data for new record: " + id;
+    return record;
+  },
+
+  changeItemID: function changeItemID(oldID, newID) {
+    if (oldID in this.items) {
+      this.items[newID] = this.items[oldID];
+    }
+
+    delete this.items[oldID];
+  },
+
+  getAllIDs: function getAllIDs() {
+    let ids = {};
+    for (let id in this.items) {
+      ids[id] = true;
+    }
+    return ids;
+  },
+
+  wipe: function wipe() {
+    this.items = {};
+  }
+};
+
+function RotaryTracker(engine) {
+  Tracker.call(this, "Rotary", engine);
+}
+RotaryTracker.prototype = {
+  __proto__: Tracker.prototype
+};
+
+
+function RotaryEngine(service) {
+  SyncEngine.call(this, "Rotary", service);
+  // Ensure that the engine starts with a clean slate.
+  this.toFetch        = [];
+  this.previousFailed = [];
+}
+RotaryEngine.prototype = {
+  __proto__: SyncEngine.prototype,
+  _storeObj: RotaryStore,
+  _trackerObj: RotaryTracker,
+  _recordObj: RotaryRecord,
+
+  _findDupe: function _findDupe(item) {
+    // This is a semaphore used for testing proper reconciling on dupe
+    // detection.
+    if (item.id == "DUPE_INCOMING") {
+      return "DUPE_LOCAL";
+    }
+
+    for (let [id, value] in Iterator(this._store.items)) {
+      if (item.denomination == value) {
+        return id;
+      }
+    }
+  }
+};
--- a/services/sync/tests/unit/head_helpers.js
+++ b/services/sync/tests/unit/head_helpers.js
@@ -274,113 +274,8 @@ function encryptPayload(cleartext) {
 }
 
 function generateNewKeys(collectionKeys, collections=null) {
   let wbo = collectionKeys.generateNewKeysWBO(collections);
   let modified = new_timestamp();
   collectionKeys.setContents(wbo.cleartext, modified);
 }
 
-/*
- * A fake engine implementation.
- * This is used all over the place.
- *
- * Complete with record, store, and tracker implementations.
- */
-
-function RotaryRecord(collection, id) {
-  CryptoWrapper.call(this, collection, id);
-}
-RotaryRecord.prototype = {
-  __proto__: CryptoWrapper.prototype
-};
-Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]);
-
-function RotaryStore(engine) {
-  Store.call(this, "Rotary", engine);
-  this.items = {};
-}
-RotaryStore.prototype = {
-  __proto__: Store.prototype,
-
-  create: function Store_create(record) {
-    this.items[record.id] = record.denomination;
-  },
-
-  remove: function Store_remove(record) {
-    delete this.items[record.id];
-  },
-
-  update: function Store_update(record) {
-    this.items[record.id] = record.denomination;
-  },
-
-  itemExists: function Store_itemExists(id) {
-    return (id in this.items);
-  },
-
-  createRecord: function(id, collection) {
-    let record = new RotaryRecord(collection, id);
-
-    if (!(id in this.items)) {
-      record.deleted = true;
-      return record;
-    }
-
-    record.denomination = this.items[id] || "Data for new record: " + id;
-    return record;
-  },
-
-  changeItemID: function(oldID, newID) {
-    if (oldID in this.items) {
-      this.items[newID] = this.items[oldID];
-    }
-
-    delete this.items[oldID];
-  },
-
-  getAllIDs: function() {
-    let ids = {};
-    for (let id in this.items) {
-      ids[id] = true;
-    }
-    return ids;
-  },
-
-  wipe: function() {
-    this.items = {};
-  }
-};
-
-function RotaryTracker(engine) {
-  Tracker.call(this, "Rotary", engine);
-}
-RotaryTracker.prototype = {
-  __proto__: Tracker.prototype
-};
-
-
-function RotaryEngine(service) {
-  SyncEngine.call(this, "Rotary", service);
-  // Ensure that the engine starts with a clean slate.
-  this.toFetch        = [];
-  this.previousFailed = [];
-}
-RotaryEngine.prototype = {
-  __proto__: SyncEngine.prototype,
-  _storeObj: RotaryStore,
-  _trackerObj: RotaryTracker,
-  _recordObj: RotaryRecord,
-
-  _findDupe: function(item) {
-    // This is a semaphore used for testing proper reconciling on dupe
-    // detection.
-    if (item.id == "DUPE_INCOMING") {
-      return "DUPE_LOCAL";
-    }
-
-    for (let [id, value] in Iterator(this._store.items)) {
-      if (item.denomination == value) {
-        return id;
-      }
-    }
-  }
-};
--- a/services/sync/tests/unit/test_engine_abort.js
+++ b/services/sync/tests/unit/test_engine_abort.js
@@ -1,14 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
+Cu.import("resource://testing-common/services/sync/rotaryengine.js");
 
 add_test(function test_processIncoming_abort() {
   _("An abort exception, raised in applyIncoming, will abort _processIncoming.");
   new SyncTestingInfrastructure();
   generateNewKeys(Service.collectionKeys);
 
   let engine = new RotaryEngine(Service);
 
--- a/services/sync/tests/unit/test_hmac_error.js
+++ b/services/sync/tests/unit/test_hmac_error.js
@@ -1,15 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
 
+Cu.import("resource://testing-common/services/sync/rotaryengine.js");
+
 // Track HMAC error counts.
 let hmacErrorCount = 0;
 (function () {
   let hHE = Service.handleHMACEvent;
   Service.handleHMACEvent = function () {
     hmacErrorCount++;
     return hHE.call(Service);
   };
--- a/services/sync/tests/unit/test_load_modules.js
+++ b/services/sync/tests/unit/test_load_modules.js
@@ -26,15 +26,26 @@ const modules = [
   "service.js",
   "stages/cluster.js",
   "stages/enginesync.js",
   "status.js",
   "userapi.js",
   "util.js",
 ];
 
+const testingModules = [
+  "rotaryengine.js",
+];
+
 function run_test() {
-  for each (let m in modules) {
-    _("Attempting to load resource://services-sync/" + m);
-    Cu.import("resource://services-sync/" + m, {});
+  for (let m of modules) {
+    let res = "resource://services-sync/" + m;
+    _("Attempting to load " + res);
+    Cu.import(res, {});
+  }
+
+  for (let m of testingModules) {
+    let res = "resource://testing-common/services/sync/" + m;
+    _("Attempting to load " + res);
+    Cu.import(res, {});
   }
 }
 
--- a/services/sync/tests/unit/test_node_reassignment.js
+++ b/services/sync/tests/unit/test_node_reassignment.js
@@ -4,16 +4,17 @@
 _("Test that node reassignment responses are respected on all kinds of " +
   "requests.");
 
 Cu.import("resource://services-common/log4moz.js");
 Cu.import("resource://services-common/rest.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/status.js");
+Cu.import("resource://testing-common/services/sync/rotaryengine.js");
 
 Service.engineManager.clear();
 
 function run_test() {
   Log4Moz.repository.getLogger("Sync.AsyncResource").level = Log4Moz.Level.Trace;
   Log4Moz.repository.getLogger("Sync.ErrorHandler").level  = Log4Moz.Level.Trace;
   Log4Moz.repository.getLogger("Sync.Resource").level      = Log4Moz.Level.Trace;
   Log4Moz.repository.getLogger("Sync.RESTRequest").level   = Log4Moz.Level.Trace;
--- a/services/sync/tests/unit/test_score_triggers.js
+++ b/services/sync/tests/unit/test_score_triggers.js
@@ -1,16 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/clients.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/status.js");
+Cu.import("resource://testing-common/services/sync/rotaryengine.js");
 
 Service.engineManager.clear();
 Service.engineManager.register(RotaryEngine);
 let engine = Service.engineManager.get("rotary");
 let tracker = engine._tracker;
 engine.enabled = true;
 
 // Tracking info/collections.
--- a/services/sync/tests/unit/test_syncengine_sync.js
+++ b/services/sync/tests/unit/test_syncengine_sync.js
@@ -2,16 +2,17 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/policies.js");
 Cu.import("resource://services-sync/resource.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
+Cu.import("resource://testing-common/services/sync/rotaryengine.js");
 
 function makeRotaryEngine() {
   return new RotaryEngine(Service);
 }
 
 function cleanAndGo(server) {
   Svc.Prefs.resetBranch("");
   Svc.Prefs.set("log.logger.engine.rotary", "Trace");