diff --git a/services/sync/modules/engines.js b/services/sync/modules/engines.js index f0ca10f7e332..f63f43586652 100644 --- a/services/sync/modules/engines.js +++ b/services/sync/modules/engines.js @@ -77,9 +77,19 @@ Tracker.prototype = { this._score = 0; }, - saveChangedIDs: function T_saveChangedIDs() { + persistChangedIDs: true, + + /** + * Persist changedIDs to disk at a later date. + * Optionally pass a callback to be invoked when the write has occurred. + */ + saveChangedIDs: function (cb) { + if (!this.persistChangedIDs) { + this._log.debug("Not saving changedIDs."); + return; + } Utils.namedTimer(function() { - Utils.jsonSave("changes/" + this.file, this, this.changedIDs); + Utils.jsonSave("changes/" + this.file, this, this.changedIDs, cb); }, 1000, this, "_lazySave"); }, @@ -122,7 +132,7 @@ Tracker.prototype = { if ((this.changedIDs[id] || -Infinity) < when) { this._log.trace("Adding changed ID: " + [id, when]); this.changedIDs[id] = when; - this.saveChangedIDs(); + this.saveChangedIDs(this.onSavedChangedIDs); } return true; }, diff --git a/services/sync/tests/unit/test_addons_tracker.js b/services/sync/tests/unit/test_addons_tracker.js index b0a4a894bbbf..65950e3553eb 100644 --- a/services/sync/tests/unit/test_addons_tracker.js +++ b/services/sync/tests/unit/test_addons_tracker.js @@ -20,6 +20,9 @@ let reconciler = engine._reconciler; let store = engine._store; let tracker = engine._tracker; +// Don't write out by default. +tracker.persistChangedIDs = false; + const addon1ID = "addon1@tests.mozilla.org"; function cleanup_and_advance() { @@ -28,7 +31,6 @@ function cleanup_and_advance() { tracker.resetScore(); tracker.clearChangedIDs(); - tracker._lazySave.clear(); reconciler._addons = {}; reconciler._changes = []; diff --git a/services/sync/tests/unit/test_bookmark_store.js b/services/sync/tests/unit/test_bookmark_store.js index dac10bc6960d..578eb142a0d0 100644 --- a/services/sync/tests/unit/test_bookmark_store.js +++ b/services/sync/tests/unit/test_bookmark_store.js @@ -9,8 +9,14 @@ Cu.import("resource://services-sync/util.js"); const PARENT_ANNO = "sync/parent"; Service.engineManager.register(BookmarksEngine); + let engine = Service.engineManager.get("bookmarks"); let store = engine._store; +let tracker = engine._tracker; + +// Don't write some persistence files asynchronously. +tracker.persistChangedIDs = false; + let fxuri = Utils.makeURI("http://getfirefox.com/"); let tburi = Utils.makeURI("http://getthunderbird.com/"); @@ -324,9 +330,9 @@ add_test(function test_move_order() { toolbar.children = [bmk2_guid, bmk1_guid]; store.applyIncoming(toolbar); // Bookmarks engine does this at the end of _processIncoming - engine._tracker.ignoreAll = true; + tracker.ignoreAll = true; store._orderChildren(); - engine._tracker.ignoreAll = false; + tracker.ignoreAll = false; delete store._childrenToOrder; _("Verify new order."); @@ -399,11 +405,6 @@ add_test(function test_reparentOrphans() { } finally { _("Clean up."); store.wipe(); - - if (engine._tracker._lazySave) { - engine._tracker._lazySave.clear(); - } - run_next_test(); } }); diff --git a/services/sync/tests/unit/test_bookmark_tracker.js b/services/sync/tests/unit/test_bookmark_tracker.js index c517f89335cb..1ab43a47f322 100644 --- a/services/sync/tests/unit/test_bookmark_tracker.js +++ b/services/sync/tests/unit/test_bookmark_tracker.js @@ -11,7 +11,10 @@ Cu.import("resource://services-sync/util.js"); Service.engineManager.register(BookmarksEngine); let engine = Service.engineManager.get("bookmarks"); let store = engine._store; +let tracker = engine._tracker; + store.wipe(); +tracker.persistChangedIDs = false; function test_tracking() { _("Verify we've got an empty tracker to work with."); @@ -66,7 +69,6 @@ function test_tracking() { store.wipe(); tracker.clearChangedIDs(); tracker.resetScore(); - tracker._lazySave.clear(); Svc.Obs.notify("weave:engine:stop-tracking"); } } @@ -105,7 +107,6 @@ function test_onItemChanged() { store.wipe(); tracker.clearChangedIDs(); tracker.resetScore(); - tracker._lazySave.clear(); Svc.Obs.notify("weave:engine:stop-tracking"); } } @@ -158,7 +159,6 @@ function test_onItemMoved() { store.wipe(); tracker.clearChangedIDs(); tracker.resetScore(); - tracker._lazySave.clear(); Svc.Obs.notify("weave:engine:stop-tracking"); } diff --git a/services/sync/tests/unit/test_forms_tracker.js b/services/sync/tests/unit/test_forms_tracker.js index dfb1081c269b..bc943e013856 100644 --- a/services/sync/tests/unit/test_forms_tracker.js +++ b/services/sync/tests/unit/test_forms_tracker.js @@ -9,6 +9,9 @@ Cu.import("resource://services-sync/util.js"); function run_test() { _("Verify we've got an empty tracker to work with."); let tracker = new FormEngine(Service)._tracker; + // Don't do asynchronous writes. + tracker.persistChangedIDs = false; + do_check_empty(tracker.changedIDs); Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender()); @@ -46,8 +49,5 @@ function run_test() { } finally { _("Clean up."); Svc.Form.removeAllEntries(); - if (tracker._lazySave) { - tracker._lazySave.clear(); - } } } diff --git a/services/sync/tests/unit/test_history_tracker.js b/services/sync/tests/unit/test_history_tracker.js index 8c493ea3e4db..d6f6fc9baa1a 100644 --- a/services/sync/tests/unit/test_history_tracker.js +++ b/services/sync/tests/unit/test_history_tracker.js @@ -24,6 +24,9 @@ Service.engineManager.register(HistoryEngine); let engine = Service.engineManager.get("history"); let tracker = engine._tracker; +// Don't write out by default. +tracker.persistChangedIDs = false; + let _counter = 0; function addVisit() { let uri = Utils.makeURI("http://getfirefox.com/" + _counter); @@ -55,12 +58,23 @@ add_test(function test_not_tracking(next) { }); add_test(function test_start_tracking() { + _("Add hook for save completion."); + tracker.persistChangedIDs = true; + tracker.onSavedChangedIDs = function () { + _("changedIDs written to disk. Proceeding."); + // Turn this back off. + tracker.persistChangedIDs = false; + delete tracker.onSavedChangedIDs; + run_next_test(); + }; + _("Tell the tracker to start tracking changes."); onScoreUpdated(function() { do_check_attribute_count(tracker.changedIDs, 1); do_check_eq(tracker.score, SCORE_INCREMENT_SMALL); run_next_test(); }); + Svc.Obs.notify("weave:engine:start-tracking"); addVisit(); }); @@ -148,8 +162,5 @@ add_test(function test_stop_tracking_twice() { add_test(function cleanup() { _("Clean up."); PlacesUtils.history.removeAllPages(); - if (tracker._lazySave) { - tracker._lazySave.clear(); - } run_next_test(); }); diff --git a/services/sync/tests/unit/test_password_tracker.js b/services/sync/tests/unit/test_password_tracker.js index 567621ea0d6d..ddfc524ab044 100644 --- a/services/sync/tests/unit/test_password_tracker.js +++ b/services/sync/tests/unit/test_password_tracker.js @@ -10,12 +10,20 @@ Cu.import("resource://services-sync/util.js"); Service.engineManager.register(PasswordEngine); let engine = Service.engineManager.get("passwords"); let store = engine._store; +let tracker = engine._tracker; -function test_tracking() { +// Don't do asynchronous writes. +tracker.persistChangedIDs = false; + +function run_test() { + initTestLogging("Trace"); + run_next_test(); +} + +add_test(function test_tracking() { let recordNum = 0; _("Verify we've got an empty tracker to work with."); - let tracker = engine._tracker; do_check_empty(tracker.changedIDs); function createPassword() { @@ -69,14 +77,13 @@ function test_tracking() { store.wipe(); tracker.clearChangedIDs(); tracker.resetScore(); - tracker._lazySave.clear(); Svc.Obs.notify("weave:engine:stop-tracking"); + run_next_test(); } -} +}); -function test_onWipe() { +add_test(function test_onWipe() { _("Verify we've got an empty tracker to work with."); - let tracker = engine._tracker; do_check_empty(tracker.changedIDs); do_check_eq(tracker.score, 0); @@ -89,16 +96,6 @@ function test_onWipe() { } finally { tracker.resetScore(); Svc.Obs.notify("weave:engine:stop-tracking"); + run_next_test(); } -} - -function run_test() { - initTestLogging("Trace"); - - Log4Moz.repository.getLogger("Sync.Engine.Passwords").level = Log4Moz.Level.Trace; - Log4Moz.repository.getLogger("Sync.Store.Passwords").level = Log4Moz.Level.Trace; - Log4Moz.repository.getLogger("Sync.Tracker.Passwords").level = Log4Moz.Level.Trace; - - test_tracking(); - test_onWipe(); -} +}); diff --git a/services/sync/tests/unit/test_prefs_tracker.js b/services/sync/tests/unit/test_prefs_tracker.js index 9f91ca0fa76f..dd45f239852f 100644 --- a/services/sync/tests/unit/test_prefs_tracker.js +++ b/services/sync/tests/unit/test_prefs_tracker.js @@ -11,6 +11,10 @@ Cu.import("resource://services-sync/util.js"); function run_test() { let engine = Service.engineManager.get("prefs"); let tracker = engine._tracker; + + // Don't write out by default. + tracker.persistChangedIDs = false; + let prefs = new Preferences(); try { @@ -80,8 +84,5 @@ function run_test() { } finally { Svc.Obs.notify("weave:engine:stop-tracking"); prefs.resetBranch(""); - if (tracker._lazySave) { - tracker._lazySave.clear(); - } } } diff --git a/services/sync/tests/unit/test_tab_tracker.js b/services/sync/tests/unit/test_tab_tracker.js index 3d9d54a62ab1..230dc68c14bc 100644 --- a/services/sync/tests/unit/test_tab_tracker.js +++ b/services/sync/tests/unit/test_tab_tracker.js @@ -51,6 +51,8 @@ function run_test() { _("We assume that tabs have changed at startup."); let tracker = engine._tracker; + tracker.persistChangedIDs = false; + do_check_true(tracker.modified); do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()), [clientsEngine.localID])); @@ -113,7 +115,4 @@ function run_test() { do_check_true(Utils.deepEquals(Object.keys(engine.getChangedIDs()), [clientsEngine.localID])); do_check_eq(logs.length, idx); // test that setTabValue isn't called - if (tracker._lazySave) { - tracker._lazySave.clear(); - } } diff --git a/services/sync/tests/unit/test_tracker_addChanged.js b/services/sync/tests/unit/test_tracker_addChanged.js index 7f8e3fa039eb..e73bd1162f54 100644 --- a/services/sync/tests/unit/test_tracker_addChanged.js +++ b/services/sync/tests/unit/test_tracker_addChanged.js @@ -3,9 +3,16 @@ Cu.import("resource://services-sync/engines.js"); Cu.import("resource://services-sync/service.js"); +Cu.import("resource://services-sync/util.js"); function run_test() { + run_next_test(); +} + +add_test(function test_tracker_basics() { let tracker = new Tracker("Tracker", Service); + tracker.persistChangedIDs = false; + let id = "the_id!"; _("Make sure nothing exists yet.."); @@ -26,5 +33,27 @@ function run_test() { _("Adding without time defaults to current time"); tracker.addChangedID(id); do_check_true(tracker.changedIDs[id] > 10); - tracker._lazySave.clear() -} + + run_next_test(); +}); + +add_test(function test_tracker_persistence() { + let tracker = new Tracker("Tracker", Service); + let id = "abcdef"; + + tracker.persistChangedIDs = true; + tracker.onSavedChangedIDs = function () { + _("IDs saved."); + do_check_eq(5, tracker.changedIDs[id]); + + // Verify the write by reading the file back. + Utils.jsonLoad("changes/tracker", this, function (json) { + do_check_eq(5, json[id]); + tracker.persistChangedIDs = false; + delete tracker.onSavedChangedIDs; + run_next_test(); + }); + }; + + tracker.addChangedID(id, 5); +});