Bug 1120380 - Add test coverage for archived pings retention. r=gfritzsche a=sledru
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Tue, 19 May 2015 06:46:00 -0400
changeset 275119 741b414586deb0885550e10f65bd1fc6f7f82c7b
parent 275118 2bac618c592b14f058bcd5ebab0e5b10f71a28ef
child 275120 7ebfb7f13d158a7a52f459ad5db2172f6aaf3f0e
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche, sledru
bugs1120380
milestone40.0a2
Bug 1120380 - Add test coverage for archived pings retention. r=gfritzsche a=sledru
toolkit/components/telemetry/tests/unit/head.js
toolkit/components/telemetry/tests/unit/test_PingAPI.js
--- a/toolkit/components/telemetry/tests/unit/head.js
+++ b/toolkit/components/telemetry/tests/unit/head.js
@@ -143,16 +143,17 @@ function fakeSchedulerTimer(set, clear) 
  * @return Date The new faked date.
  */
 function fakeNow(...args) {
   const date = new Date(...args);
   const modules = [
     Cu.import("resource://gre/modules/TelemetrySession.jsm"),
     Cu.import("resource://gre/modules/TelemetryEnvironment.jsm"),
     Cu.import("resource://gre/modules/TelemetryController.jsm"),
+    Cu.import("resource://gre/modules/TelemetryStorage.jsm"),
   ];
 
   for (let m of modules) {
     m.Policy.now = () => date;
   }
 
   return new Date(date);
 }
--- a/toolkit/components/telemetry/tests/unit/test_PingAPI.js
+++ b/toolkit/components/telemetry/tests/unit/test_PingAPI.js
@@ -70,17 +70,17 @@ add_task(function* test_archivedPings() 
       Assert.equal(ping.creationDate, data.dateCreated.toISOString(),
                    "Archived ping should have matching creation date");
     }
   });
 
   yield checkLoadingPings();
 
   // Check that we find the archived pings again by scanning after a restart.
-  TelemetryArchive._testReset();
+  TelemetryController.reset();
 
   let pingList = yield TelemetryArchive.promiseArchivedPingList();
   Assert.deepEqual(expectedPingList, pingList,
                    "Should have submitted pings in archive list after restart");
   yield checkLoadingPings();
 
   // Write invalid pings into the archive with both valid and invalid names.
   let writeToArchivedDir = Task.async(function*(dirname, filename, content, compressed) {
@@ -124,31 +124,78 @@ add_task(function* test_archivedPings() 
   expectedPingList.push({
     id: FAKE_ID3,
     type: "foo",
     timestampCreated: 4,
   });
   expectedPingList.sort((a, b) => a.timestampCreated - b.timestampCreated);
 
   // Reset the TelemetryArchive so we scan the archived dir again.
-  yield TelemetryArchive._testReset();
+  yield TelemetryController.reset();
 
   // Check that we are still picking up the valid archived pings on disk,
   // plus the valid ones above.
   pingList = yield TelemetryArchive.promiseArchivedPingList();
   Assert.deepEqual(expectedPingList, pingList, "Should have picked up valid archived pings");
   yield checkLoadingPings();
 
   // Now check that we fail to load the two invalid pings from above.
   Assert.ok((yield promiseRejects(TelemetryArchive.promiseArchivedPingById(FAKE_ID1))),
             "Should have rejected invalid ping");
   Assert.ok((yield promiseRejects(TelemetryArchive.promiseArchivedPingById(FAKE_ID2))),
             "Should have rejected invalid ping");
 });
 
+add_task(function* test_archiveCleanup() {
+  const PING_TYPE = "foo";
+
+  // Create a ping which should be pruned because it is past the retention period.
+  fakeNow(2010, 1, 1, 1, 0, 0);
+  const PING_ID1 = yield TelemetryController.submitExternalPing(PING_TYPE, {}, {});
+  // Create a ping which should be kept because it is within the retention period.
+  fakeNow(2010, 2, 1, 1, 0, 0);
+  const PING_ID2 = yield TelemetryController.submitExternalPing(PING_TYPE, {}, {});
+  // Create a ping which should be kept because it is within the retention period.
+  fakeNow(2010, 3, 1, 1, 0, 0);
+  const PING_ID3 = yield TelemetryController.submitExternalPing(PING_TYPE, {}, {});
+
+  // Move the current date 180 days ahead of the PING_ID1 ping.
+  fakeNow(2010, 7, 1, 1, 0, 0);
+  // Reset TelemetryArchive and TelemetryController to start the startup cleanup.
+  yield TelemetryController.reset();
+  // Wait for the cleanup to finish.
+  yield TelemetryStorage.testCleanupTaskPromise();
+  // Then scan the archived dir.
+  let pingList = yield TelemetryArchive.promiseArchivedPingList();
+
+  // The PING_ID1 ping should be removed and the other 2 kept.
+  Assert.ok((yield promiseRejects(TelemetryArchive.promiseArchivedPingById(PING_ID1))),
+            "Old pings should be removed from the archive");
+  Assert.ok((yield TelemetryArchive.promiseArchivedPingById(PING_ID2)),
+            "Recent pings should be kept in the archive");
+  Assert.ok((yield TelemetryArchive.promiseArchivedPingById(PING_ID3)),
+            "Recent pings should be kept in the archive");
+
+  // Move the current date 180 days ahead of the PING_ID2 ping.
+  fakeNow(2010, 8, 1, 1, 0, 0);
+  // Reset TelemetryController but not the TelemetryArchive: the cleanup task will update
+  // the existing archive cache.
+  yield TelemetryController.reset();
+  // Wait for the cleanup to finish.
+  yield TelemetryStorage.testCleanupTaskPromise();
+  // Then scan the archived dir again.
+  pingList = yield TelemetryArchive.promiseArchivedPingList();
+
+  // The PING_ID2 ping should be removed and PING_ID3 kept.
+  Assert.ok((yield promiseRejects(TelemetryArchive.promiseArchivedPingById(PING_ID2))),
+            "Old pings should be removed from the archive");
+  Assert.ok((yield TelemetryArchive.promiseArchivedPingById(PING_ID3)),
+            "Recent pings should be kept in the archive");
+});
+
 add_task(function* test_clientId() {
   // Check that a ping submitted after the delayed telemetry initialization completed
   // should get a valid client id.
   yield TelemetryController.setup();
   const clientId = TelemetryController.clientID;
 
   let id = yield TelemetryController.submitExternalPing("test-type", {}, {addClientId: true});
   let ping = yield TelemetryArchive.promiseArchivedPingById(id);