Bug 957123 - Complete porting test_TelemetrySendOldPings.js to OS.File. r=rvitillo

This commit is contained in:
David Rajchenbach-Teller 2014-02-07 16:09:02 -05:00
parent 168808d0d9
commit 1b0ea13e7d

View File

@ -23,13 +23,14 @@ Cu.import("resource://gre/modules/Promise.jsm", this);
Cu.import("resource://gre/modules/TelemetryFile.jsm", this); Cu.import("resource://gre/modules/TelemetryFile.jsm", this);
Cu.import("resource://gre/modules/TelemetryPing.jsm", this); Cu.import("resource://gre/modules/TelemetryPing.jsm", this);
Cu.import("resource://gre/modules/Task.jsm", this); Cu.import("resource://gre/modules/Task.jsm", this);
Cu.import("resource://gre/modules/osfile.jsm", this); let {OS: {File, Path, Constants}} = Cu.import("resource://gre/modules/osfile.jsm", {});
// We increment TelemetryFile's MAX_PING_FILE_AGE and // We increment TelemetryFile's MAX_PING_FILE_AGE and
// OVERDUE_PING_FILE_AGE by 1ms so that our test pings exceed // OVERDUE_PING_FILE_AGE by 1 minute so that our test pings exceed
// those points in time. // those points in time, even taking into account file system imprecision.
const EXPIRED_PING_FILE_AGE = TelemetryFile.MAX_PING_FILE_AGE + 1; const ONE_MINUTE_MS = 60 * 1000;
const OVERDUE_PING_FILE_AGE = TelemetryFile.OVERDUE_PING_FILE_AGE + 1; const EXPIRED_PING_FILE_AGE = TelemetryFile.MAX_PING_FILE_AGE + ONE_MINUTE_MS;
const OVERDUE_PING_FILE_AGE = TelemetryFile.OVERDUE_PING_FILE_AGE + ONE_MINUTE_MS;
const PING_SAVE_FOLDER = "saved-telemetry-pings"; const PING_SAVE_FOLDER = "saved-telemetry-pings";
const PING_TIMEOUT_LENGTH = 5000; const PING_TIMEOUT_LENGTH = 5000;
@ -52,7 +53,8 @@ let gSeenPings = 0;
* @param aAge the age in milliseconds to offset from now. A value * @param aAge the age in milliseconds to offset from now. A value
* of 10 would make the ping 10ms older than now, for * of 10 would make the ping 10ms older than now, for
* example. * example.
* @returns an Array with the created pings. * @returns Promise
* @resolve an Array with the created pings.
*/ */
function createSavedPings(aNum, aAge) { function createSavedPings(aNum, aAge) {
return Task.spawn(function*(){ return Task.spawn(function*(){
@ -71,8 +73,8 @@ function createSavedPings(aNum, aAge) {
if (aAge) { if (aAge) {
// savePing writes to the file synchronously, so we're good to // savePing writes to the file synchronously, so we're good to
// modify the lastModifedTime now. // modify the lastModifedTime now.
let file = getSaveFileForPing(ping); let file = getSavePathForPing(ping);
file.lastModifiedTime = age; yield File.setDates(file, null, age);
} }
gCreatedPings++; gCreatedPings++;
pings.push(ping); pings.push(ping);
@ -86,31 +88,29 @@ function createSavedPings(aNum, aAge) {
* exist. * exist.
* *
* @param aPings an Array of pings to delete. * @param aPings an Array of pings to delete.
* @returns Promise
*/ */
function clearPings(aPings) { function clearPings(aPings) {
for (let ping of aPings) { return Task.spawn(function*() {
let file = getSaveFileForPing(ping); for (let ping of aPings) {
if (file.exists()) { let path = getSavePathForPing(ping);
file.remove(false); yield File.remove(path);
} }
} });
} }
/** /**
* Returns a handle for the file that aPing should be * Returns a handle for the file that aPing should be
* stored in locally. * stored in locally.
* *
* @returns nsILocalFile * @returns path
*/ */
function getSaveFileForPing(aPing) { function getSavePathForPing(aPing) {
let file = Services.dirsvc.get("ProfD", Ci.nsILocalFile).clone(); return Path.join(Constants.Path.profileDir, PING_SAVE_FOLDER, aPing.slug);
file.append(PING_SAVE_FOLDER);
file.append(aPing.slug);
return file;
} }
/** /**
* Check if the number of TelemetryPings received by the * Check if the number of TelemetryPings received by the
* HttpServer is not equal to aExpectedNum. * HttpServer is not equal to aExpectedNum.
* *
* @param aExpectedNum the number of pings we expect to receive. * @param aExpectedNum the number of pings we expect to receive.
@ -123,18 +123,21 @@ function assertReceivedPings(aExpectedNum) {
* Throws if any pings in aPings is saved locally. * Throws if any pings in aPings is saved locally.
* *
* @param aPings an Array of pings to check. * @param aPings an Array of pings to check.
* @returns Promise
*/ */
function assertNotSaved(aPings) { function assertNotSaved(aPings) {
let saved = 0; return Task.spawn(function*() {
for (let ping of aPings) { let saved = 0;
let file = getSaveFileForPing(ping); for (let ping of aPings) {
if (file.exists()) { let file = getSavePathForPing(ping);
saved++; if (yield File.exists()) {
saved++;
}
} }
} if (saved > 0) {
if (saved > 0) { do_throw("Found " + saved + " unexpected saved pings.");
do_throw("Found " + saved + " unexpected saved pings."); }
} });
} }
/** /**
@ -200,7 +203,7 @@ add_task(function test_expired_pings_are_deleted() {
let expiredPings = yield createSavedPings(EXPIRED_PINGS, EXPIRED_PING_FILE_AGE); let expiredPings = yield createSavedPings(EXPIRED_PINGS, EXPIRED_PING_FILE_AGE);
yield startTelemetry(); yield startTelemetry();
assertReceivedPings(0); assertReceivedPings(0);
assertNotSaved(expiredPings); yield assertNotSaved(expiredPings);
yield resetTelemetry(); yield resetTelemetry();
}); });
@ -212,7 +215,7 @@ add_task(function test_recent_pings_not_sent() {
yield startTelemetry(); yield startTelemetry();
assertReceivedPings(0); assertReceivedPings(0);
yield resetTelemetry(); yield resetTelemetry();
clearPings(recentPings); yield clearPings(recentPings);
}); });
/** /**
@ -228,9 +231,9 @@ add_task(function test_overdue_pings_trigger_send() {
yield startTelemetry(); yield startTelemetry();
assertReceivedPings(TOTAL_EXPECTED_PINGS); assertReceivedPings(TOTAL_EXPECTED_PINGS);
assertNotSaved(recentPings); yield assertNotSaved(recentPings);
assertNotSaved(expiredPings); yield assertNotSaved(expiredPings);
assertNotSaved(overduePings); yield assertNotSaved(overduePings);
yield resetTelemetry(); yield resetTelemetry();
}); });