From b7a5a93bce8b065fb89a7fbeff5b6e9c6ed6a06f Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Fri, 30 Dec 2011 13:55:19 -0800 Subject: [PATCH] Bug 714202 - Fix implementation of AddonsReconciler.pruneChangesBeforeDate(); r=rnewman --- services/sync/modules/addonsreconciler.js | 10 ++--- .../sync/tests/unit/test_addons_reconciler.js | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/services/sync/modules/addonsreconciler.js b/services/sync/modules/addonsreconciler.js index 65e895e3ee8..ff45ed1413c 100644 --- a/services/sync/modules/addonsreconciler.js +++ b/services/sync/modules/addonsreconciler.js @@ -547,13 +547,9 @@ AddonsReconciler.prototype = { pruneChangesBeforeDate: function pruneChangesBeforeDate(date) { this._ensureStateLoaded(); - while (this._changes.length > 0) { - if (this._changes[0][0] >= date) { - return; - } - - delete this._changes[0]; - } + this._changes = this._changes.filter(function test_age(change) { + return change[0] >= date; + }); }, /** diff --git a/services/sync/tests/unit/test_addons_reconciler.js b/services/sync/tests/unit/test_addons_reconciler.js index 18beeb9b888..d561db9eff3 100644 --- a/services/sync/tests/unit/test_addons_reconciler.js +++ b/services/sync/tests/unit/test_addons_reconciler.js @@ -150,3 +150,44 @@ add_test(function test_load_state_future_version() { run_next_test(); }); }); + +add_test(function test_prune_changes_before_date() { + _("Ensure that old changes are pruned properly."); + + let reconciler = new AddonsReconciler(); + reconciler._ensureStateLoaded(); + reconciler._changes = []; + + let now = new Date(); + const HOUR_MS = 1000 * 60 * 60; + + _("Ensure pruning an empty changes array works."); + reconciler.pruneChangesBeforeDate(now); + do_check_eq(0, reconciler._changes.length); + + let old = new Date(now.getTime() - HOUR_MS); + let young = new Date(now.getTime() - 1000); + reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]); + reconciler._changes.push([young, CHANGE_INSTALLED, "bar"]); + do_check_eq(2, reconciler._changes.length); + + _("Ensure pruning with an old time won't delete anything."); + let threshold = new Date(old.getTime() - 1); + reconciler.pruneChangesBeforeDate(threshold); + do_check_eq(2, reconciler._changes.length); + + _("Ensure pruning a single item works."); + let threshold = new Date(young.getTime() - 1000); + reconciler.pruneChangesBeforeDate(threshold); + do_check_eq(1, reconciler._changes.length); + do_check_neq(undefined, reconciler._changes[0]); + do_check_eq(young, reconciler._changes[0][0]); + do_check_eq("bar", reconciler._changes[0][2]); + + _("Ensure pruning all changes works."); + reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]); + reconciler.pruneChangesBeforeDate(now); + do_check_eq(0, reconciler._changes.length); + + run_next_test(); +});