Bug 1538276 - Test 'prio' ping. r=janerik

Differential Revision: https://phabricator.services.mozilla.com/D24782

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chris H-C 2019-03-27 19:29:56 +00:00
Родитель a773e93e99
Коммит bfeaa78b80
2 изменённых файлов: 108 добавлений и 0 удалений

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

@ -0,0 +1,107 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
ChromeUtils.import("resource://gre/modules/TelemetryController.jsm", this);
ChromeUtils.defineModuleGetter(this, "TelemetryPrioPing",
"resource://gre/modules/PrioPing.jsm");
function checkPingStructure(type, payload, options) {
Assert.equal(type, TelemetryPrioPing.PRIO_PING_TYPE, "Should be a prio ping.");
// Check the payload for required fields.
Assert.ok("version" in payload, "Payload must have version.");
Assert.ok("reason" in payload, "Payload must have reason.");
Assert.ok(Object.values(TelemetryPrioPing.Reason).some(reason => payload.reason === reason),
"Should be a known reason.");
Assert.ok(Array.isArray(payload.prioData), "Payload prioData must be present and an array.");
payload.prioData.forEach(prioData => {
Assert.ok("encoding" in prioData, "All prioData must have encodings.");
Assert.ok("prio" in prioData, "All prioData must have prio blocks.");
});
// Ensure we forbid client id and environment
Assert.equal(options.addClientId, false, "Must forbid client Id.");
Assert.equal(options.addEnvironment, false, "Must forbid Environment.");
}
function fakePolicy(set, clear, send, snapshot) {
let mod = ChromeUtils.import("resource://gre/modules/PrioPing.jsm", null);
mod.Policy.setTimeout = set;
mod.Policy.clearTimeout = clear;
mod.Policy.sendPing = send;
mod.Policy.getEncodedOriginSnapshot = snapshot;
}
function pass() { /* intentionally empty */ }
function fail() {
Assert.ok(false, "Not allowed");
}
function fakeSnapshot() {
return [
{
encoding: "telemetry.test-1-1",
prio: {},
},
{
encoding: "telemetry.test-1-1",
prio: {},
},
];
}
add_task(async function setup() {
// Trigger a proper telemetry init.
do_get_profile(true);
// Make sure we don't generate unexpected pings due to pref changes.
await setEmptyPrefWatchlist();
await TelemetryController.testSetup();
TelemetryPrioPing.testReset();
});
// Similarly to test_EventPing tests in this file often follow the form:
// 1: Fake out timeout, ping submission, and snapshotting
// 2: Trigger a "prio" ping to happen
// 3: Inside the fake ping submission, ensure the ping is correctly formed.
// In sinon this would be replaced with spies and .wasCalledWith().
add_task(async function test_limit_reached() {
// Ensure that on being notified of the limit we immediately trigger a ping
// with reason "max"
fakePolicy(pass, pass, (type, payload, options) => {
checkPingStructure(type, payload, options);
Assert.equal(payload.reason, TelemetryPrioPing.Reason.MAX, "Sent using max reason.");
}, fakeSnapshot);
Services.obs.notifyObservers(null, "origin-telemetry-storage-limit-reached");
});
add_task(async function test_periodic() {
// First fake a "max"-reason ping to allow us to control when the timer fires.
let fireTimerNow;
fakePolicy(callback => fireTimerNow = callback, pass, (type, payload, options) => {
checkPingStructure(type, payload, options);
// And now, set up the actual test:
fakePolicy(pass, pass, (type2, payload2, options2) => {
checkPingStructure(type2, payload2, options2);
Assert.equal(payload2.reason, TelemetryPrioPing.Reason.PERIODIC, "Sent with periodic reason.");
}, fakeSnapshot);
// Now fire the timer. This is re-entrant, which is weird, but then
// so is this test.
fireTimerNow();
}, fakeSnapshot);
Services.obs.notifyObservers(null, "origin-telemetry-storage-limit-reached");
});
add_task(async function test_shutdown() {
fakePolicy(fail, pass, (type, payload, options) => {
checkPingStructure(type, payload, options);
Assert.equal(payload.reason, TelemetryPrioPing.Reason.SHUTDOWN, "Sent with shutdown reason.");
}, fakeSnapshot);
await TelemetryPrioPing.shutdown();
});

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

@ -101,3 +101,4 @@ skip-if = (os == "linux" && bits == 32) # lack of support on test runners (see b
[test_EventPing_disabled.js]
tags = coverage
[test_CoveragePing.js]
[test_PrioPing.js]