Bug 714202 - Fix implementation of AddonsReconciler.pruneChangesBeforeDate(); r=rnewman
authorGregory Szorc <gps@mozilla.com>
Fri, 30 Dec 2011 13:55:19 -0800
changeset 83592 9dda4ea46da1821c8259eadf279549b90354cdca
parent 83591 04ef7f5644504e96208a12e50d1a39471b7c4441
child 83593 f2610136fb54094038a312f6d92ba4f5a428fdb5
push idunknown
push userunknown
push dateunknown
reviewersrnewman
bugs714202
milestone12.0a1
Bug 714202 - Fix implementation of AddonsReconciler.pruneChangesBeforeDate(); r=rnewman
services/sync/modules/addonsreconciler.js
services/sync/tests/unit/test_addons_reconciler.js
--- a/services/sync/modules/addonsreconciler.js
+++ b/services/sync/modules/addonsreconciler.js
@@ -542,23 +542,19 @@ AddonsReconciler.prototype = {
    * Prunes all recorded changes from before the specified Date.
    *
    * @param date
    *        Entries older than this Date will be removed.
    */
   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;
+    });
   },
 
   /**
    * Obtains the set of all known Sync GUIDs for add-ons.
    *
    * @return Object with guids as keys and values of true.
    */
   getAllSyncGUIDs: function getAllSyncGUIDs() {
--- a/services/sync/tests/unit/test_addons_reconciler.js
+++ b/services/sync/tests/unit/test_addons_reconciler.js
@@ -145,8 +145,49 @@ add_test(function test_load_state_future
 
     do_check_eq("object", typeof(reconciler.addons));
     do_check_eq(1, Object.keys(reconciler.addons).length);
     do_check_eq(1, reconciler._changes.length);
 
     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();
+});