1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
// This tests the public Telemetry API for submitting Health pings.
"use strict";
ChromeUtils.import("resource://gre/modules/TelemetryController.jsm", this);
ChromeUtils.import("resource://gre/modules/TelemetryStorage.jsm", this);
ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm", this);
ChromeUtils.import("resource://gre/modules/Preferences.jsm", this);
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
ChromeUtils.import(
"resource://testing-common/TelemetryArchiveTesting.jsm",
this
);
ChromeUtils.defineModuleGetter(
this,
"TelemetryHealthPing",
"resource://gre/modules/HealthPing.jsm"
);
function checkHealthPingStructure(ping, expectedFailuresDict) {
let payload = ping.payload;
Assert.equal(
ping.type,
TelemetryHealthPing.HEALTH_PING_TYPE,
"Should have recorded a health ping."
);
for (let [key, value] of Object.entries(expectedFailuresDict)) {
Assert.deepEqual(
payload[key],
value,
"Should have recorded correct entry with key: " + key
);
}
}
function fakeHealthSchedulerTimer(set, clear) {
let telemetryHealthPing = ChromeUtils.import(
"resource://gre/modules/HealthPing.jsm",
null
);
telemetryHealthPing.Policy.setSchedulerTickTimeout = set;
telemetryHealthPing.Policy.clearSchedulerTickTimeout = clear;
}
async function waitForConditionWithPromise(
promiseFn,
timeoutMsg,
tryCount = 30
) {
const SINGLE_TRY_TIMEOUT = 100;
let tries = 0;
do {
try {
return await promiseFn();
} catch (ex) {}
await new Promise(resolve => do_timeout(SINGLE_TRY_TIMEOUT, resolve));
} while (++tries <= tryCount);
throw new Error(timeoutMsg);
}
function fakeSendSubmissionTimeout(timeOut) {
let telemetryHealthPing = ChromeUtils.import(
"resource://gre/modules/TelemetrySend.jsm",
null
);
telemetryHealthPing.Policy.pingSubmissionTimeout = () => timeOut;
}
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();
Preferences.set(TelemetryUtils.Preferences.HealthPingEnabled, true);
await TelemetryController.testSetup();
PingServer.start();
TelemetrySend.setServer("http://localhost:" + PingServer.port);
Preferences.set(
TelemetryUtils.Preferences.Server,
"http://localhost:" + PingServer.port
);
});
add_task(async function test_sendImmediately() {
PingServer.clearRequests();
TelemetryHealthPing.testReset();
await TelemetryHealthPing.recordSendFailure("testProblem");
let ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.SEND_FAILURE]: {
testProblem: 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.IMMEDIATE,
});
});
add_task(async function test_sendOnDelay() {
PingServer.clearRequests();
TelemetryHealthPing.testReset();
// This first failure should immediately trigger a ping. After this, subsequent failures should be throttled.
await TelemetryHealthPing.recordSendFailure("testFailure");
let testPing = await PingServer.promiseNextPing();
Assert.equal(
testPing.type,
TelemetryHealthPing.HEALTH_PING_TYPE,
"Should have recorded a health ping."
);
// Retrieve delayed call back.
let pingSubmissionCallBack = null;
fakeHealthSchedulerTimer(
callBack => (pingSubmissionCallBack = callBack),
() => {}
);
// Record two failures, health ping must not be send now.
await TelemetryHealthPing.recordSendFailure("testFailure");
await TelemetryHealthPing.recordSendFailure("testFailure");
// Wait for sending delayed health ping.
await pingSubmissionCallBack();
let ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.SEND_FAILURE]: {
testFailure: 2,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.DELAYED,
});
});
add_task(async function test_sendOverSizedPing() {
TelemetryHealthPing.testReset();
PingServer.clearRequests();
let OVER_SIZED_PING_TYPE = "over-sized-ping";
let overSizedData = generateRandomString(2 * 1024 * 1024);
await TelemetryController.submitExternalPing(OVER_SIZED_PING_TYPE, {
data: overSizedData,
});
let ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.DISCARDED_FOR_SIZE]: {
[OVER_SIZED_PING_TYPE]: 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.IMMEDIATE,
});
});
add_task(async function test_healthPingOnTop() {
PingServer.clearRequests();
TelemetryHealthPing.testReset();
let PING_TYPE = "priority-ping";
// Fake now to be in throttled state.
let now = fakeNow(2050, 1, 2, 0, 0, 0);
fakeMidnightPingFuzzingDelay(60 * 1000);
for (let value of [PING_TYPE, PING_TYPE, "health", PING_TYPE]) {
TelemetryController.submitExternalPing(value, {});
}
// Now trigger sending pings again.
fakeNow(futureDate(now, 5 * 60 * 1000));
await TelemetrySend.notifyCanUpload();
let scheduler = ChromeUtils.import(
"resource://gre/modules/TelemetrySend.jsm",
null
);
scheduler.SendScheduler.triggerSendingPings(true);
let pings = await PingServer.promiseNextPings(4);
Assert.equal(
pings[0].type,
"health",
"Should have received the health ping first."
);
});
add_task(async function test_sendOnTimeout() {
TelemetryHealthPing.testReset();
await TelemetrySend.reset();
PingServer.clearRequests();
let PING_TYPE = "ping-on-timeout";
// Disable send retry to make this test more deterministic.
fakePingSendTimer(
() => {},
() => {}
);
// Set up small ping submission timeout to always have timeout error.
fakeSendSubmissionTimeout(2);
await TelemetryController.submitExternalPing(PING_TYPE, {});
let response;
PingServer.registerPingHandler((req, res) => {
PingServer.resetPingHandler();
// We don't finish the response yet to make sure to trigger a timeout.
res.processAsync();
response = res;
});
// Wait for health ping.
let ac = new TelemetryArchiveTesting.Checker();
await ac.promiseInit();
await waitForConditionWithPromise(() => {
ac.promiseFindPing("health", []);
}, "Failed to find health ping");
if (response) {
response.finish();
}
let telemetryHealthPing = ChromeUtils.import(
"resource://gre/modules/TelemetrySend.jsm",
null
);
fakeSendSubmissionTimeout(telemetryHealthPing.PING_SUBMIT_TIMEOUT_MS);
PingServer.resetPingHandler();
TelemetrySend.notifyCanUpload();
let pings = await PingServer.promiseNextPings(2);
let healthPing = pings.find(ping => ping.type === "health");
checkHealthPingStructure(healthPing, {
[TelemetryHealthPing.FailureType.SEND_FAILURE]: {
timeout: 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.IMMEDIATE,
});
await TelemetryStorage.testClearPendingPings();
});
add_task(async function test_sendOnlyTopTenDiscardedPings() {
TelemetryHealthPing.testReset();
await TelemetrySend.reset();
PingServer.clearRequests();
let PING_TYPE = "sort-discarded";
// This first failure should immediately trigger a ping. After this, subsequent failures should be throttled.
await TelemetryHealthPing.recordSendFailure("testFailure");
let testPing = await PingServer.promiseNextPing();
Assert.equal(
testPing.type,
TelemetryHealthPing.HEALTH_PING_TYPE,
"Should have recorded a health ping."
);
// Retrieve delayed call back.
let pingSubmissionCallBack = null;
fakeHealthSchedulerTimer(
callBack => (pingSubmissionCallBack = callBack),
() => {}
);
// Add failures
for (let i = 1; i < 12; i++) {
for (let j = 1; j < i; j++) {
TelemetryHealthPing.recordDiscardedPing(PING_TYPE + i);
}
}
await TelemetrySend.reset();
await pingSubmissionCallBack();
let ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.DELAYED,
[TelemetryHealthPing.FailureType.DISCARDED_FOR_SIZE]: {
[PING_TYPE + 11]: 10,
[PING_TYPE + 10]: 9,
[PING_TYPE + 9]: 8,
[PING_TYPE + 8]: 7,
[PING_TYPE + 7]: 6,
[PING_TYPE + 6]: 5,
[PING_TYPE + 5]: 4,
[PING_TYPE + 4]: 3,
[PING_TYPE + 3]: 2,
[PING_TYPE + 2]: 1,
},
});
});
add_task(async function test_discardedForSizePending() {
TelemetryHealthPing.testReset();
PingServer.clearRequests();
const PING_TYPE = "discarded-for-size-pending";
const OVERSIZED_PING_ID = "9b21ec8f-f762-4d28-a2c1-44e1c4694f24";
// Create a pending oversized ping.
let overSizedPayload = generateRandomString(2 * 1024 * 1024);
const OVERSIZED_PING = {
id: OVERSIZED_PING_ID,
type: PING_TYPE,
creationDate: new Date().toISOString(),
// Generate a 2MB string to use as the ping payload.
payload: overSizedPayload,
};
// Test loadPendingPing.
await TelemetryStorage.savePendingPing(OVERSIZED_PING);
// Try to manually load the oversized ping.
await Assert.rejects(
TelemetryStorage.loadPendingPing(OVERSIZED_PING_ID),
/loadPendingPing - exceeded the maximum ping size/,
"The oversized ping should have been pruned."
);
let ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.DISCARDED_FOR_SIZE]: {
"<unknown>": 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.IMMEDIATE,
});
// Test _scanPendingPings.
TelemetryHealthPing.testReset();
await TelemetryStorage.savePendingPing(OVERSIZED_PING);
await TelemetryStorage.loadPendingPingList();
ping = await PingServer.promiseNextPing();
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.DISCARDED_FOR_SIZE]: {
"<unknown>": 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.IMMEDIATE,
});
});
add_task(async function test_usePingSenderOnShutdown() {
if (
gIsAndroid ||
(AppConstants.platform == "linux" && OS.Constants.Sys.bits == 32)
) {
// We don't support the pingsender on Android, yet, see bug 1335917.
// We also don't support the pingsender testing on Treeherder for
// Linux 32 bit (due to missing libraries). So skip it there too.
// See bug 1310703 comment 78.
return;
}
TelemetryHealthPing.testReset();
await TelemetrySend.reset();
PingServer.clearRequests();
// This first failure should immediately trigger a ping.
// After this, subsequent failures should be throttled.
await TelemetryHealthPing.recordSendFailure("testFailure");
await PingServer.promiseNextPing();
TelemetryHealthPing.recordSendFailure("testFailure");
let nextRequest = PingServer.promiseNextRequest();
await TelemetryController.testReset();
await TelemetryController.testShutdown();
let request = await nextRequest;
let ping = decodeRequestPayload(request);
checkHealthPingStructure(ping, {
[TelemetryHealthPing.FailureType.SEND_FAILURE]: {
testFailure: 1,
},
os: TelemetryHealthPing.OsInfo,
reason: TelemetryHealthPing.Reason.SHUT_DOWN,
});
// Check that the health ping is sent at shutdown using the pingsender.
Assert.equal(
request.getHeader("User-Agent"),
"pingsender/1.0",
"Should have received the correct user agent string."
);
Assert.equal(
request.getHeader("X-PingSender-Version"),
"1.0",
"Should have received the correct PingSender version string."
);
});
add_task(async function cleanup() {
await PingServer.stop();
});
|