summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_EventPing.js
blob: 450e88a84626b06bef3a031d7cbe5e91eb3afcda (plain)
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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  TelemetryEventPing: "resource://gre/modules/EventPing.sys.mjs",
});

function checkPingStructure(type, payload, options) {
  Assert.equal(
    type,
    TelemetryEventPing.EVENT_PING_TYPE,
    "Should be an event ping."
  );
  // Check the payload for required fields.
  Assert.ok("reason" in payload, "Payload must have reason.");
  Assert.ok(
    "processStartTimestamp" in payload,
    "Payload must have processStartTimestamp."
  );
  Assert.ok("sessionId" in payload, "Payload must have sessionId.");
  Assert.ok("subsessionId" in payload, "Payload must have subsessionId.");
  Assert.ok("lostEventsCount" in payload, "Payload must have lostEventsCount.");
  Assert.ok("events" in payload, "Payload must have events.");
}

function fakePolicy(set, clear, send) {
  let { Policy } = ChromeUtils.importESModule(
    "resource://gre/modules/EventPing.sys.mjs"
  );
  Policy.setTimeout = set;
  Policy.clearTimeout = clear;
  Policy.sendPing = send;
}

function pass() {
  /* intentionally empty */
}
function fail() {
  Assert.ok(false, "Not allowed");
}

function recordEvents(howMany) {
  for (let i = 0; i < howMany; i++) {
    Telemetry.recordEvent("telemetry.test", "test1", "object1");
  }
}

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();
  TelemetryEventPing.testReset();
  Telemetry.setEventRecordingEnabled("telemetry.test", true);
});

// Tests often take the form of faking policy within faked policy.
// This is to allow us to record events in addition to any that were
// recorded to trigger the submit in the first place.
// This works because we start the timer at the top of _submitPing, giving us
// this opportunity.
// This results in things looking this way:
/*
fakePolicy((callback, delay) => {
  // Code that runs at the top of _submitPing
  fakePolicy(pass, pass, (type, payload, options) => {
    // Code that runs at the bottom of _submitPing
  });
}, pass, fail);
// Code that triggers _submitPing to run
*/

add_task(async function test_eventLimitReached() {
  Telemetry.clearEvents();
  TelemetryEventPing.testReset();

  let pingCount = 0;

  fakePolicy(pass, pass, fail);
  recordEvents(999);
  fakePolicy(
    (callback, delay) => {
      Telemetry.recordEvent("telemetry.test", "test2", "object1");
      fakePolicy(pass, pass, (type, payload, options) => {
        checkPingStructure(type, payload, options);
        Assert.ok(options.addClientId, "Adds the client id.");
        Assert.ok(options.addEnvironment, "Adds the environment.");
        Assert.ok(!options.usePingSender, "Doesn't require pingsender.");
        Assert.equal(
          payload.reason,
          TelemetryEventPing.Reason.MAX,
          "Sending because we hit max"
        );
        Assert.equal(
          payload.events.parent.length,
          1000,
          "Has one thousand events"
        );
        Assert.equal(payload.lostEventsCount, 0, "Lost no events");
        Assert.ok(
          !payload.events.parent.some(ev => ev[1] === "test2"),
          "Should not have included the final event (yet)."
        );
        pingCount++;
      });
    },
    pass,
    fail
  );
  // Now trigger the submit.
  Telemetry.recordEvent("telemetry.test", "test1", "object1");
  Assert.equal(pingCount, 1, "Should have sent a ping");

  // With a recent MAX ping sent, record another max amount of events (and then two extras).
  fakePolicy(fail, fail, fail);
  recordEvents(998);
  fakePolicy(
    (callback, delay) => {
      Telemetry.recordEvent("telemetry.test", "test2", "object2");
      Telemetry.recordEvent("telemetry.test", "test2", "object2");
      fakePolicy(pass, pass, (type, payload, options) => {
        checkPingStructure(type, payload, options);
        Assert.ok(options.addClientId, "Adds the client id.");
        Assert.ok(options.addEnvironment, "Adds the environment.");
        Assert.ok(!options.usePingSender, "Doesn't require pingsender.");
        Assert.equal(
          payload.reason,
          TelemetryEventPing.Reason.MAX,
          "Sending because we hit max"
        );
        Assert.equal(
          payload.events.parent.length,
          1000,
          "Has one thousand events"
        );
        Assert.equal(payload.lostEventsCount, 2, "Lost two events");
        Assert.equal(
          payload.events.parent[0][2],
          "test2",
          "The first event of the second bunch should be the leftover event of the first bunch."
        );
        Assert.ok(
          !payload.events.parent.some(ev => ev[3] === "object2"),
          "Should not have included any of the lost two events."
        );
        pingCount++;
      });
      callback(); // Trigger the send immediately.
    },
    pass,
    fail
  );
  recordEvents(1);
  Assert.equal(pingCount, 2, "Should have sent a second ping");

  // Ensure we send a subsequent MAX ping exactly on 1000 events, and without
  // the two events we lost.
  fakePolicy(fail, fail, fail);
  recordEvents(999);
  fakePolicy((callback, delay) => {
    fakePolicy(pass, pass, (type, payload, options) => {
      checkPingStructure(type, payload, options);
      Assert.ok(options.addClientId, "Adds the client id.");
      Assert.ok(options.addEnvironment, "Adds the environment.");
      Assert.ok(!options.usePingSender, "Doesn't require pingsender.");
      Assert.equal(
        payload.reason,
        TelemetryEventPing.Reason.MAX,
        "Sending because we hit max"
      );
      Assert.equal(
        payload.events.parent.length,
        1000,
        "Has one thousand events"
      );
      Assert.equal(payload.lostEventsCount, 0, "Lost no events");
      Assert.ok(
        !payload.events.parent.some(ev => ev[3] === "object2"),
        "Should not have included any of the lost two events from the previous bunch."
      );
      pingCount++;
    });
    callback(); // Trigger the send immediately
  });
  recordEvents(1);
  Assert.equal(pingCount, 3, "Should have sent a third ping");
});

add_task(async function test_timers() {
  Telemetry.clearEvents();
  TelemetryEventPing.testReset();

  // Immediately after submitting a MAX ping, we should set the timer for the
  // next interval.
  recordEvents(999);
  fakePolicy(
    (callback, delay) => {
      Assert.equal(
        delay,
        TelemetryEventPing.minFrequency,
        "Timer should be started with the min frequency"
      );
    },
    pass,
    pass
  );
  recordEvents(1);

  fakePolicy(
    (callback, delay) => {
      Assert.ok(
        delay <= TelemetryEventPing.maxFrequency,
        "Timer should be at most the max frequency for a subsequent MAX ping."
      );
    },
    pass,
    pass
  );
  recordEvents(1000);
});

add_task(async function test_periodic() {
  Telemetry.clearEvents();
  TelemetryEventPing.testReset();

  fakePolicy(
    (callback, delay) => {
      Assert.equal(
        delay,
        TelemetryEventPing.minFrequency,
        "Timer should default to the min frequency"
      );
      fakePolicy(pass, pass, (type, payload, options) => {
        checkPingStructure(type, payload, options);
        Assert.ok(options.addClientId, "Adds the client id.");
        Assert.ok(options.addEnvironment, "Adds the environment.");
        Assert.ok(!options.usePingSender, "Doesn't require pingsender.");
        Assert.equal(
          payload.reason,
          TelemetryEventPing.Reason.PERIODIC,
          "Sending because we hit a timer"
        );
        Assert.equal(payload.events.parent.length, 1, "Has one event");
        Assert.equal(payload.lostEventsCount, 0, "Lost no events");
      });
      callback();
    },
    pass,
    fail
  );

  recordEvents(1);
  TelemetryEventPing._startTimer();
});

// Ensure this is the final test in the suite, as it shuts things down.
add_task(async function test_shutdown() {
  Telemetry.clearEvents();
  TelemetryEventPing.testReset();

  recordEvents(999);
  fakePolicy(pass, pass, (type, payload, options) => {
    Assert.ok(options.addClientId, "Adds the client id.");
    Assert.ok(options.addEnvironment, "Adds the environment.");
    Assert.ok(options.usePingSender, "Asks for pingsender.");
    Assert.equal(
      payload.reason,
      TelemetryEventPing.Reason.SHUTDOWN,
      "Sending because we are shutting down"
    );
    Assert.equal(payload.events.parent.length, 999, "Has 999 events");
    Assert.equal(payload.lostEventsCount, 0, "No lost events");
  });
  TelemetryEventPing.shutdown();
});