Skip to content

Commit c62bffe

Browse files
committed
Test to verify no trailing restarted events
We generate an array of events, in a somewhat unorthodox manner to ensure that our original array contains multiple trailing restarted events. We then put a few restarted events between non-restarted events to verify that we stop removing items once we hit a non-restarted event.
1 parent d8f62d7 commit c62bffe

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

glean/tests/unit/core/pings/maker.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as PingMaker from "../../../../src/core/pings/maker";
1010
import Glean from "../../../../src/core/glean";
1111
import CoreEvents from "../../../../src/core/events";
1212
import Plugin from "../../../../src/plugins";
13-
import type { JSONObject } from "../../../../src/core/utils";
13+
import type { JSONArray, JSONObject } from "../../../../src/core/utils";
1414
import { Context } from "../../../../src/core/context";
1515
import { stopGleanUploader } from "../../../utils";
1616
import EventMetricType from "../../../../src/core/metrics/types/event";
@@ -273,4 +273,47 @@ describe("PingMaker", function() {
273273
labeled_counter: { "glean.error.invalid_value": { "glean.restarted": 1 } }
274274
});
275275
});
276+
277+
it("should delete trailing restarted events", async function() {
278+
const ping = new PingType({
279+
name: "aPing",
280+
includeClientId: true,
281+
sendIfEmpty: true,
282+
});
283+
const event = new EventMetricType({
284+
category: "test",
285+
name: "aEvent",
286+
sendInPings: ["aPing"],
287+
lifetime: Lifetime.Ping,
288+
disabled: false
289+
});
290+
291+
// Record events
292+
for (let i = 0; i < 6; i++) {
293+
if (i === 0 || i === 4) {
294+
// Record a non-restarted event
295+
event.record();
296+
// Wait for recording action to complete.
297+
await event.testGetValue();
298+
} else {
299+
// Record a restarted event (easiest way)
300+
//
301+
// Un-initialize and re-initialize manually instead of using testResetGlean
302+
// in order to have control over the startTime at initialization.
303+
await testUninitializeGlean(false);
304+
// Move the clock backwards by one hour.
305+
//
306+
// This will generate incoherent timestamps in events at collection time
307+
// and record an `InvalidValue` error for the `glean.restarted` event.
308+
Context.startTime.setTime(Context.startTime.getTime() - 1000 * 60 * 60);
309+
await testInitializeGlean(testAppId, true);
310+
}
311+
}
312+
313+
await PingMaker.collectAndStorePing("ident", ping);
314+
const allPings = Object.fromEntries(await Context.pingsDatabase.getAllPings());
315+
const eventsArray = allPings["ident"]["payload"]["events"] as JSONArray;
316+
317+
assert.equal(5, eventsArray.length);
318+
});
276319
});

0 commit comments

Comments
 (0)