Bug 1156712 - Part 1: Clean up test utils for waiting on ping submissions. r=dexter

This commit is contained in:
Georg Fritzsche 2015-07-07 16:49:09 +02:00
Родитель 52c94a8891
Коммит 308499e378
4 изменённых файлов: 165 добавлений и 224 удалений

Просмотреть файл

@ -5,6 +5,9 @@ const { classes: Cc, utils: Cu, interfaces: Ci, results: Cr } = Components;
Cu.import("resource://gre/modules/TelemetryController.jsm", this);
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://gre/modules/PromiseUtils.jsm", this);
Cu.import("resource://gre/modules/Task.jsm", this);
Cu.import("resource://testing-common/httpd.js", this);
const gIsWindows = ("@mozilla.org/windows-registry-key;1" in Cc);
const gIsMac = ("@mozilla.org/xpcom/mac-utils;1" in Cc);
@ -22,6 +25,78 @@ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12
let gOldAppInfo = null;
let gGlobalScope = this;
const PingServer = {
_httpServer: null,
_started: false,
_defers: [ PromiseUtils.defer() ],
_currentDeferred: 0,
get port() {
return this._httpServer.identity.primaryPort;
},
get started() {
return this._started;
},
registerPingHandler: function(handler) {
const wrapped = wrapWithExceptionHandler(handler);
this._httpServer.registerPrefixHandler("/submit/telemetry/", wrapped);
},
resetPingHandler: function() {
this.registerPingHandler((request, response) => {
let deferred = this._defers[this._defers.length - 1];
this._defers.push(PromiseUtils.defer());
deferred.resolve(request);
});
},
start: function() {
this._httpServer = new HttpServer();
this._httpServer.start(-1);
this._started = true;
this.clearRequests();
this.resetPingHandler();
},
stop: function() {
return new Promise(resolve => {
this._httpServer.stop(resolve);
this._started = false;
});
},
clearRequests: function() {
this._defers = [ PromiseUtils.defer() ];
this._currentDeferred = 0;
},
promiseNextRequest: function() {
const deferred = this._defers[this._currentDeferred++];
return deferred.promise;
},
promiseNextPing: function() {
return this.promiseNextRequest().then(request => decodeRequestPayload(request));
},
promiseNextRequests: Task.async(function*(count) {
let results = [];
for (let i=0; i<count; ++i) {
results.push(yield this.promiseNextRequest());
}
return results;
}),
promiseNextPings: function(count) {
return this.promiseNextRequests(count).then(requests => {
return [for (req of requests) decodeRequestPayload(req)];
});
},
};
/**
* Decode the payload of an HTTP request into a ping.
* @param {Object} request The data representing an HTTP request (nsIHttpRequest).
@ -63,6 +138,19 @@ function decodeRequestPayload(request) {
return payload;
}
function wrapWithExceptionHandler(f) {
function wrapper(...args) {
try {
f(...args);
} catch (ex if typeof(ex) == 'object') {
dump("Caught exception: " + ex.message + "\n");
dump(ex.stack);
do_test_finished();
}
}
return wrapper;
}
function loadAddonManager(id, name, version, platformVersion) {
let ns = {};
Cu.import("resource://gre/modules/Services.jsm", ns);

Просмотреть файл

@ -8,7 +8,6 @@
* checked in the second request.
*/
Cu.import("resource://testing-common/httpd.js", this);
Cu.import("resource://gre/modules/ClientID.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
@ -37,14 +36,11 @@ const PREF_UNIFIED = PREF_BRANCH + "unified";
const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry);
let gHttpServer = new HttpServer();
let gServerStarted = false;
let gRequestIterator = null;
let gClientID = null;
function sendPing(aSendClientId, aSendEnvironment) {
if (gServerStarted) {
TelemetrySend.setServer("http://localhost:" + gHttpServer.identity.primaryPort);
if (PingServer.started) {
TelemetrySend.setServer("http://localhost:" + PingServer.port);
} else {
TelemetrySend.setServer("http://doesnotexist");
}
@ -56,24 +52,6 @@ function sendPing(aSendClientId, aSendEnvironment) {
return TelemetryController.submitExternalPing(TEST_PING_TYPE, {}, options);
}
function wrapWithExceptionHandler(f) {
function wrapper(...args) {
try {
f(...args);
} catch (ex if typeof(ex) == 'object') {
dump("Caught exception: " + ex.message + "\n");
dump(ex.stack);
do_test_finished();
}
}
return wrapper;
}
function registerPingHandler(handler) {
gHttpServer.registerPrefixHandler("/submit/telemetry/",
wrapWithExceptionHandler(handler));
}
function checkPingFormat(aPing, aType, aHasClientId, aHasEnvironment) {
const MANDATORY_PING_FIELDS = [
"type", "id", "creationDate", "version", "application", "payload"
@ -114,15 +92,6 @@ function checkPingFormat(aPing, aType, aHasClientId, aHasEnvironment) {
Assert.equal("environment" in aPing, aHasEnvironment);
}
/**
* Start the webserver used in the tests.
*/
function startWebserver() {
gHttpServer.start(-1);
gServerStarted = true;
gRequestIterator = Iterator(new Request());
}
function run_test() {
do_test_pending();
@ -159,10 +128,10 @@ add_task(function* test_overwritePing() {
// Checks that a sent ping is correctly received by a dummy http server.
add_task(function* test_simplePing() {
startWebserver();
PingServer.start();
yield sendPing(false, false);
let request = yield gRequestIterator.next();
let request = yield PingServer.promiseNextRequest();
// Check that we have a version query parameter in the URL.
Assert.notEqual(request.queryString, "");
@ -186,8 +155,7 @@ add_task(function* test_deletionPing() {
// Disable FHR upload: this should trigger a deletion ping.
Preferences.set(PREF_FHR_UPLOAD_ENABLED, false);
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, DELETION_PING_TYPE, true, false);
// Restore FHR Upload.
@ -198,8 +166,7 @@ add_task(function* test_pingHasClientId() {
// Send a ping with a clientId.
yield sendPing(true, false);
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, TEST_PING_TYPE, true, false);
if (HAS_DATAREPORTINGSERVICE &&
@ -212,8 +179,7 @@ add_task(function* test_pingHasClientId() {
add_task(function* test_pingHasEnvironment() {
// Send a ping with the environment data.
yield sendPing(false, true);
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, TEST_PING_TYPE, false, true);
// Test a field in the environment build section.
@ -223,8 +189,7 @@ add_task(function* test_pingHasEnvironment() {
add_task(function* test_pingHasEnvironmentAndClientId() {
// Send a ping with the environment data and client id.
yield sendPing(true, true);
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, TEST_PING_TYPE, true, true);
// Test a field in the environment build section.
@ -254,13 +219,12 @@ add_task(function* test_archivePings() {
// If we're using unified telemetry, disabling ping upload will generate a "deletion"
// ping. Catch it.
if (isUnified) {
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, DELETION_PING_TYPE, true, false);
}
// Register a new Ping Handler that asserts if a ping is received, then send a ping.
registerPingHandler(() => Assert.ok(false, "Telemetry must not send pings if not allowed to."));
PingServer.registerPingHandler(() => Assert.ok(false, "Telemetry must not send pings if not allowed to."));
let pingId = yield sendPing(true, true);
// Check that the ping was archived, even with upload disabled.
@ -282,12 +246,12 @@ add_task(function* test_archivePings() {
now = new Date(2014, 06, 18, 22, 0, 0);
fakeNow(now);
// Restore the non asserting ping handler. This is done by the Request() constructor.
gRequestIterator = Iterator(new Request());
// Restore the non asserting ping handler.
PingServer.resetPingHandler();
pingId = yield sendPing(true, true);
// Check that we archive pings when successfully sending them.
yield gRequestIterator.next();
yield PingServer.promiseNextPing();
ping = yield TelemetryArchive.promiseArchivedPingById(pingId);
Assert.equal(ping.id, pingId,
"TelemetryController should still archive pings if ping upload is enabled.");
@ -308,13 +272,13 @@ add_task(function* test_midnightPingSendFuzzing() {
pingSendTimeout = timeout;
}, () => {});
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
yield TelemetryController.reset();
// A ping after midnight within the fuzzing delay should not get sent.
now = new Date(2030, 5, 2, 0, 40, 0);
fakeNow(now);
registerPingHandler((req, res) => {
PingServer.registerPingHandler((req, res) => {
Assert.ok(false, "No ping should be received yet.");
});
yield sendPing(true, true);
@ -328,34 +292,29 @@ add_task(function* test_midnightPingSendFuzzing() {
yield sendPing(true, true);
Assert.deepEqual(futureDate(now, pingSendTimeout), new Date(2030, 5, 2, 1, 0, 0));
// The Request constructor restores the previous ping handler.
gRequestIterator = Iterator(new Request());
// Restore the previous ping handler.
PingServer.resetPingHandler();
// Setting the clock to after the fuzzing delay, we should trigger the two ping sends
// with the timer callback.
now = futureDate(now, pingSendTimeout);
fakeNow(now);
yield pingSendTimerCallback();
let requests = [];
requests.push(yield gRequestIterator.next());
requests.push(yield gRequestIterator.next());
for (let req of requests) {
let ping = decodeRequestPayload(req);
const pings = yield PingServer.promiseNextPings(2);
for (let ping of pings) {
checkPingFormat(ping, TEST_PING_TYPE, true, true);
}
// Moving the clock further we should still send pings immediately.
now = futureDate(now, 5 * 60 * 1000);
yield sendPing(true, true);
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, TEST_PING_TYPE, true, true);
// Check that pings shortly before midnight are immediately sent.
now = fakeNow(2030, 5, 3, 23, 59, 0);
yield sendPing(true, true);
request = yield gRequestIterator.next();
ping = decodeRequestPayload(request);
ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, TEST_PING_TYPE, true, true);
// Clean-up.
@ -381,31 +340,6 @@ add_task(function* test_changePingAfterSubmission() {
});
add_task(function* stopServer(){
gHttpServer.stop(do_test_finished);
yield PingServer.stop();
do_test_finished();
});
// An iterable sequence of http requests
function Request() {
let defers = [];
let current = 0;
function RequestIterator() {}
// Returns a promise that resolves to the next http request
RequestIterator.prototype.next = function() {
let deferred = defers[current++];
return deferred.promise;
}
this.__iterator__ = function(){
return new RequestIterator();
}
registerPingHandler((request, response) => {
let deferred = defers[defers.length - 1];
defers.push(Promise.defer());
deferred.resolve(request);
});
defers.push(Promise.defer());
}

Просмотреть файл

@ -14,7 +14,6 @@
Cu.import("resource://gre/modules/osfile.jsm", this);
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://testing-common/httpd.js", this);
Cu.import("resource://gre/modules/Promise.jsm", this);
Cu.import("resource://gre/modules/TelemetryStorage.jsm", this);
Cu.import("resource://gre/modules/TelemetryController.jsm", this);
@ -45,7 +44,6 @@ const LRU_PINGS = TelemetrySend.MAX_LRU_PINGS;
const TOTAL_EXPECTED_PINGS = OVERDUE_PINGS + RECENT_PINGS + OLD_FORMAT_PINGS;
let gHttpServer = new HttpServer();
let gCreatedPings = 0;
let gSeenPings = 0;
@ -146,20 +144,6 @@ function pingHandler(aRequest) {
gSeenPings++;
}
/**
* Returns a Promise that resolves when gHttpServer has been
* successfully shut down.
*
* @returns Promise
*/
function stopHttpServer() {
let deferred = Promise.defer();
gHttpServer.stop(function() {
deferred.resolve();
})
return deferred.promise;
}
/**
* Clear out all pending pings.
*/
@ -179,8 +163,8 @@ function startTelemetry() {
}
function run_test() {
gHttpServer.registerPrefixHandler("/submit/telemetry/", pingHandler);
gHttpServer.start(-1);
PingServer.start();
PingServer.registerPingHandler(pingHandler);
do_get_profile();
loadAddonManager("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
@ -191,7 +175,7 @@ function run_test() {
Services.prefs.setBoolPref(TelemetryController.Constants.PREF_ENABLED, true);
Services.prefs.setCharPref(TelemetryController.Constants.PREF_SERVER,
"http://localhost:" + gHttpServer.identity.primaryPort);
"http://localhost:" + PingServer.port);
run_next_test();
}
@ -388,7 +372,7 @@ add_task(function* test_overdue_old_format() {
let receivedPings = 0;
// Register a new prefix handler to validate the URL.
gHttpServer.registerPrefixHandler("/submit/telemetry/", request => {
PingServer.registerPingHandler(request => {
// Check that we have a version query parameter in the URL.
Assert.notEqual(request.queryString, "");
@ -407,5 +391,5 @@ add_task(function* test_overdue_old_format() {
});
add_task(function* teardown() {
yield stopHttpServer();
yield PingServer.stop();
});

Просмотреть файл

@ -8,7 +8,6 @@
* checked in the second request.
*/
Cu.import("resource://testing-common/httpd.js", this);
Cu.import("resource://services-common/utils.js");
Cu.import("resource://gre/modules/ClientID.jsm");
Cu.import("resource://gre/modules/Services.jsm");
@ -77,9 +76,6 @@ XPCOMUtils.defineLazyGetter(this, "DATAREPORTING_PATH", function() {
return OS.Path.join(OS.Constants.Path.profileDir, DATAREPORTING_DIR);
});
let gHttpServer = new HttpServer();
let gServerStarted = false;
let gRequestIterator = null;
let gClientID = null;
function generateUUID() {
@ -97,8 +93,8 @@ function truncateDateToDays(date) {
function sendPing() {
TelemetrySession.gatherStartup();
if (gServerStarted) {
TelemetrySend.setServer("http://localhost:" + gHttpServer.identity.primaryPort);
if (PingServer.started) {
TelemetrySend.setServer("http://localhost:" + PingServer.port);
return TelemetrySession.testPing();
} else {
TelemetrySend.setServer("http://doesnotexist");
@ -113,19 +109,6 @@ let clearPendingPings = Task.async(function*() {
}
});
function wrapWithExceptionHandler(f) {
function wrapper(...args) {
try {
f(...args);
} catch (ex if typeof(ex) == 'object') {
dump("Caught exception: " + ex.message + "\n");
dump(ex.stack);
do_test_finished();
}
}
return wrapper;
}
function fakeGenerateUUID(sessionFunc, subsessionFunc) {
let session = Cu.import("resource://gre/modules/TelemetrySession.jsm");
session.Policy.generateSessionUUID = sessionFunc;
@ -137,11 +120,6 @@ function fakeIdleNotification(topic) {
return session.TelemetryScheduler.observe(null, topic, null);
}
function registerPingHandler(handler) {
gHttpServer.registerPrefixHandler("/submit/telemetry/",
wrapWithExceptionHandler(handler));
}
function setupTestData() {
Telemetry.newHistogram(IGNORE_HISTOGRAM, "never", Telemetry.HISTOGRAM_BOOLEAN);
Telemetry.histogramFrom(IGNORE_CLONED_HISTOGRAM, IGNORE_HISTOGRAM_TO_CLONE);
@ -533,10 +511,8 @@ add_task(function* test_noServerPing() {
// Checks that a sent ping is correctly received by a dummy http server.
add_task(function* test_simplePing() {
gHttpServer.start(-1);
gServerStarted = true;
gRequestIterator = Iterator(new Request());
Preferences.set(PREF_SERVER, "http://localhost:" + gHttpServer.identity.primaryPort);
PingServer.start();
Preferences.set(PREF_SERVER, "http://localhost:" + PingServer.port);
let now = new Date(2020, 1, 1, 12, 0, 0);
let expectedDate = new Date(2020, 1, 1, 0, 0, 0);
@ -553,8 +529,7 @@ add_task(function* test_simplePing() {
fakeNow(new Date(2020, 1, 1, 12, SESSION_DURATION_IN_MINUTES, 0));
yield sendPing();
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, PING_TYPE_MAIN, true, true);
@ -579,7 +554,7 @@ add_task(function* test_saveLoadPing() {
// Let's start out with a defined state.
yield clearPendingPings();
yield TelemetryController.reset();
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
// Setup test data and trigger pings.
setupTestData();
@ -587,10 +562,7 @@ add_task(function* test_saveLoadPing() {
yield sendPing();
// Get requests received by dummy server.
let requests = [
yield gRequestIterator.next(),
yield gRequestIterator.next(),
];
const requests = yield PingServer.promiseNextRequests(2);
for (let req of requests) {
Assert.equal(req.getHeader("content-type"), "application/json; charset=UTF-8",
@ -858,7 +830,7 @@ add_task(function* test_dailyCollection() {
let nowDay = new Date(2030, 1, 1, 0, 0, 0);
let schedulerTickCallback = null;
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
fakeNow(now);
@ -867,7 +839,7 @@ add_task(function* test_dailyCollection() {
// Init and check timer.
yield TelemetrySession.setup();
TelemetrySend.setServer("http://localhost:" + gHttpServer.identity.primaryPort);
TelemetrySend.setServer("http://localhost:" + PingServer.port);
// Set histograms to expected state.
const COUNT_ID = "TELEMETRY_TEST_COUNT";
@ -892,9 +864,8 @@ add_task(function* test_dailyCollection() {
yield schedulerTickCallback();
// Collect the daily ping.
let request = yield gRequestIterator.next();
Assert.ok(!!request);
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.payload.info.reason, REASON_DAILY);
@ -913,9 +884,8 @@ add_task(function* test_dailyCollection() {
// Run a scheduler tick. Trigger and collect another ping. The histograms should be reset.
yield schedulerTickCallback();
request = yield gRequestIterator.next();
Assert.ok(!!request);
ping = decodeRequestPayload(request);
ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.payload.info.reason, REASON_DAILY);
@ -936,9 +906,8 @@ add_task(function* test_dailyCollection() {
fakeNow(now);
yield schedulerTickCallback();
request = yield gRequestIterator.next();
Assert.ok(!!request);
ping = decodeRequestPayload(request);
ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.payload.info.reason, REASON_DAILY);
@ -959,7 +928,7 @@ add_task(function* test_dailyDuplication() {
return;
}
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
let schedulerTickCallback = null;
let now = new Date(2030, 1, 1, 0, 0, 0);
@ -979,15 +948,14 @@ add_task(function* test_dailyDuplication() {
yield schedulerTickCallback();
// Get the first daily ping.
let request = yield gRequestIterator.next();
Assert.ok(!!request);
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.payload.info.reason, REASON_DAILY);
// We don't expect to receive any other daily ping in this test, so assert if we do.
registerPingHandler((req, res) => {
PingServer.registerPingHandler((req, res) => {
Assert.ok(false, "No more daily pings should be sent/received in this test.");
});
@ -1003,6 +971,7 @@ add_task(function* test_dailyDuplication() {
// Shutdown to cleanup the aborted-session if it gets created.
yield TelemetrySession.shutdown();
PingServer.resetPingHandler();
});
add_task(function* test_dailyOverdue() {
@ -1023,7 +992,7 @@ add_task(function* test_dailyOverdue() {
fakeNow(now);
// Assert if we receive something!
registerPingHandler((req, res) => {
PingServer.registerPingHandler((req, res) => {
Assert.ok(false, "No daily ping should be received if not overdue!.");
});
@ -1031,8 +1000,9 @@ add_task(function* test_dailyOverdue() {
Assert.ok(!!schedulerTickCallback);
yield schedulerTickCallback();
// Restore the non asserting ping handler. This is done by the Request() constructor.
gRequestIterator = Iterator(new Request());
// Restore the non asserting ping handler.
PingServer.resetPingHandler();
PingServer.clearRequests();
// Simulate an overdue ping: we're not close to midnight, but the last daily ping
// time is too long ago.
@ -1044,9 +1014,8 @@ add_task(function* test_dailyOverdue() {
yield schedulerTickCallback();
// Get the first daily ping.
let request = yield gRequestIterator.next();
Assert.ok(!!request);
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.payload.info.reason, REASON_DAILY);
@ -1065,7 +1034,7 @@ add_task(function* test_environmentChange() {
let timerCallback = null;
let timerDelay = null;
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
fakeNow(now);
@ -1078,7 +1047,7 @@ add_task(function* test_environmentChange() {
// Setup.
yield TelemetrySession.setup();
TelemetrySend.setServer("http://localhost:" + gHttpServer.identity.primaryPort);
TelemetrySend.setServer("http://localhost:" + PingServer.port);
TelemetryEnvironment._watchPreferences(PREFS_TO_WATCH);
// Set histograms to expected state.
@ -1099,9 +1068,8 @@ add_task(function* test_environmentChange() {
fakeNow(now);
Preferences.set(PREF_TEST, 1);
let request = yield gRequestIterator.next();
Assert.ok(!!request);
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.environment.settings.userPrefs[PREF_TEST], undefined);
@ -1118,9 +1086,8 @@ add_task(function* test_environmentChange() {
fakeNow(now);
Preferences.set(PREF_TEST, 2);
request = yield gRequestIterator.next();
Assert.ok(!!request);
ping = decodeRequestPayload(request);
ping = yield PingServer.promiseNextPing();
Assert.ok(!!ping);
Assert.equal(ping.type, PING_TYPE_MAIN);
Assert.equal(ping.environment.settings.userPrefs[PREF_TEST], 1);
@ -1366,7 +1333,7 @@ add_task(function* test_abortedSession() {
yield TelemetryStorage.savePingToFile(abortedSessionPing, ABORTED_FILE, false);
yield clearPendingPings();
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
yield TelemetrySession.reset();
yield TelemetryController.reset();
@ -1384,11 +1351,7 @@ add_task(function* test_abortedSession() {
yield sendPing();
// We should receive two pings, one of them an aborted-session ping.
let requests = [
yield gRequestIterator.next(),
yield gRequestIterator.next(),
];
let pings = [for (req of requests) decodeRequestPayload(req)].filter((p) => {
const pings = (yield PingServer.promiseNextPings(2)).filter((p) => {
return p.type == PING_TYPE_MAIN && p.payload.info.reason == REASON_ABORTED_SESSION;
});
@ -1444,7 +1407,7 @@ add_task(function* test_abortedDailyCoalescing() {
yield OS.File.removeDir(DATAREPORTING_PATH, { ignoreAbsent: true });
let schedulerTickCallback = null;
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
let nowDate = new Date(2009, 10, 18, 0, 0, 0);
fakeNow(nowDate);
@ -1466,8 +1429,7 @@ add_task(function* test_abortedDailyCoalescing() {
yield schedulerTickCallback();
// Wait for the daily ping.
let request = yield gRequestIterator.next();
let dailyPing = decodeRequestPayload(request);
let dailyPing = yield PingServer.promiseNextPing();
Assert.equal(dailyPing.payload.info.reason, REASON_DAILY);
// Check that an aborted session ping was also written to disk.
@ -1492,7 +1454,7 @@ add_task(function* test_schedulerComputerSleep() {
const ABORTED_FILE = OS.Path.join(DATAREPORTING_PATH, ABORTED_PING_FILE_NAME);
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
// Remove any aborted-session ping from the previous tests.
yield OS.File.removeDir(DATAREPORTING_PATH, { ignoreAbsent: true });
@ -1511,8 +1473,7 @@ add_task(function* test_schedulerComputerSleep() {
// Execute one scheduler tick.
yield schedulerTickCallback();
let request = yield gRequestIterator.next();
let dailyPing = decodeRequestPayload(request);
let dailyPing = yield PingServer.promiseNextPing();
Assert.equal(dailyPing.payload.info.reason, REASON_DAILY);
Assert.ok((yield OS.File.exists(ABORTED_FILE)),
@ -1535,7 +1496,7 @@ add_task(function* test_schedulerEnvironmentReschedules() {
]);
yield clearPendingPings();
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
// Set a fake current date and start Telemetry.
let nowDate = new Date(2060, 10, 18, 0, 0, 0);
@ -1553,10 +1514,10 @@ add_task(function* test_schedulerEnvironmentReschedules() {
Preferences.set(PREF_TEST, 1);
// Wait for the environment-changed ping.
yield gRequestIterator.next();
yield PingServer.promiseNextPing();
// We don't expect to receive any daily ping in this test, so assert if we do.
registerPingHandler((req, res) => {
PingServer.registerPingHandler((req, res) => {
Assert.ok(false, "No ping should be sent/received in this test.");
});
@ -1580,7 +1541,7 @@ add_task(function* test_schedulerNothingDue() {
yield clearPendingPings();
// We don't expect to receive any ping in this test, so assert if we do.
registerPingHandler((req, res) => {
PingServer.registerPingHandler((req, res) => {
Assert.ok(false, "No ping should be sent/received in this test.");
});
@ -1603,6 +1564,7 @@ add_task(function* test_schedulerNothingDue() {
Assert.ok(!(yield OS.File.exists(ABORTED_FILE)));
yield TelemetrySession.shutdown();
PingServer.resetPingHandler();
});
add_task(function* test_pingExtendedStats() {
@ -1615,12 +1577,11 @@ add_task(function* test_pingExtendedStats() {
Telemetry.canRecordExtended = false;
yield clearPendingPings();
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
yield TelemetrySession.reset();
yield sendPing();
let request = yield gRequestIterator.next();
let ping = decodeRequestPayload(request);
let ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, PING_TYPE_MAIN, true, true);
// Check that the payload does not contain extended statistics fields.
@ -1645,8 +1606,7 @@ add_task(function* test_pingExtendedStats() {
// Send a new ping that should contain the extended data.
yield TelemetrySession.reset();
yield sendPing();
request = yield gRequestIterator.next();
ping = decodeRequestPayload(request);
ping = yield PingServer.promiseNextPing();
checkPingFormat(ping, PING_TYPE_MAIN, true, true);
// Check that the payload now contains extended statistics fields.
@ -1679,7 +1639,7 @@ add_task(function* test_schedulerUserIdle() {
}, () => {});
yield TelemetrySession.reset();
yield clearPendingPings();
gRequestIterator = Iterator(new Request());
PingServer.clearRequests();
// When not idle, the scheduler should have a 5 minutes tick interval.
Assert.equal(schedulerTimeout, SCHEDULER_TICK_INTERVAL_MS);
@ -1707,31 +1667,6 @@ add_task(function* test_schedulerUserIdle() {
});
add_task(function* stopServer(){
gHttpServer.stop(do_test_finished);
yield PingServer.stop();
do_test_finished();
});
// An iterable sequence of http requests
function Request() {
let defers = [];
let current = 0;
function RequestIterator() {}
// Returns a promise that resolves to the next http request
RequestIterator.prototype.next = function() {
let deferred = defers[current++];
return deferred.promise;
}
this.__iterator__ = function(){
return new RequestIterator();
}
registerPingHandler((request, response) => {
let deferred = defers[defers.length - 1];
defers.push(Promise.defer());
deferred.resolve(request);
});
defers.push(Promise.defer());
}