Skip to content

Commit 1bb3d33

Browse files
committed
Create more generic helper for faking glean restarts
- create testing util that restarts glean by creating a new instance of the `EventsDatabase` - update existing and new tests to use new restart helper
1 parent a8f8ed4 commit 1bb3d33

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

glean/src/core/testing/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import type { ConfigurationInterface } from "../config";
6-
import { testInitializeGlean, testUninitializeGlean } from "./utils.js";
6+
import { testInitializeGlean, testRestartGlean, testUninitializeGlean } from "./utils.js";
77

88
/**
99
* Test-only API
@@ -29,3 +29,5 @@ export async function testResetGlean(
2929
await testUninitializeGlean(clearStores);
3030
await testInitializeGlean(applicationId, uploadEnabled, config);
3131
}
32+
33+
export { testRestartGlean };

glean/src/core/testing/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { ConfigurationInterface } from "../config.js";
77
import { Context } from "../context.js";
88
import { testResetEvents } from "../events/utils.js";
99
import Glean from "../glean.js";
10+
import EventsDatabase from "../metrics/events_database/index.js";
1011

1112
/**
1213
* Test-only API
@@ -64,3 +65,25 @@ export async function testUninitializeGlean(clearStores = true): Promise<void> {
6465
Glean.preInitSourceTags = undefined;
6566
}
6667
}
68+
69+
/**
70+
* Test-only API
71+
*
72+
* Fakes a restart by creating a new instance of the events database which will
73+
* log a `glean.restarted` event.
74+
*
75+
* If you do not move the clock forward, an error will be logged.
76+
*
77+
* @param timeOffset How far ahead to move clock before restarting. Some events just need
78+
* a future time, others need a specific future time. The default time is 1 minute.
79+
* @returns New instance of `EventsDatabase` since we "restarted" it.
80+
*/
81+
export async function testRestartGlean(timeOffset: number = 1000 * 60): Promise<EventsDatabase> {
82+
// Move the clock to look like Glean was really restarted.
83+
Context.startTime.setTime(Context.startTime.getTime() + timeOffset);
84+
85+
// Fake a restart.
86+
const db = new EventsDatabase();
87+
await db.initialize();
88+
return db;
89+
}

glean/tests/unit/core/metrics/events_database.spec.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,29 @@ import type { SinonFakeTimers } from "sinon";
77
import sinon from "sinon";
88

99
import { Lifetime } from "../../../../src/core/metrics/lifetime";
10-
import EventsDatabase, { getGleanRestartedEventMetric } from "../../../../src/core/metrics/events_database";
11-
import { InternalEventMetricType as EventMetricType} from "../../../../src/core/metrics/types/event";
10+
import EventsDatabase, {
11+
getGleanRestartedEventMetric,
12+
} from "../../../../src/core/metrics/events_database";
13+
import { InternalEventMetricType as EventMetricType } from "../../../../src/core/metrics/types/event";
1214
import type { JSONObject } from "../../../../src/core/utils";
1315
import CounterMetricType from "../../../../src/core/metrics/types/counter";
1416
import { generateReservedMetricIdentifiers } from "../../../../src/core/metrics/database";
15-
import { InternalPingType as PingType} from "../../../../src/core/pings/ping_type";
17+
import { InternalPingType as PingType } from "../../../../src/core/pings/ping_type";
1618
import { Context } from "../../../../src/core/context";
1719
import { RecordedEvent } from "../../../../src/core/metrics/events_database/recorded_event";
18-
import { EVENTS_PING_NAME, GLEAN_EXECUTION_COUNTER_EXTRA_KEY } from "../../../../src/core/constants";
20+
import {
21+
EVENTS_PING_NAME,
22+
GLEAN_EXECUTION_COUNTER_EXTRA_KEY,
23+
} from "../../../../src/core/constants";
1924
import { collectPing } from "../../../../src/core/pings/maker";
2025
import { ErrorType } from "../../../../src/core/error/error_type";
2126
import { testResetGlean } from "../../../../src/core/testing";
2227
import type { Event } from "../../../../src/core/metrics/events_database/recorded_event";
23-
import { testInitializeGlean, testUninitializeGlean } from "../../../../src/core/testing/utils";
28+
import {
29+
testInitializeGlean,
30+
testRestartGlean,
31+
testUninitializeGlean,
32+
} from "../../../../src/core/testing/utils";
2433
import { WaitableUploader } from "../../../utils";
2534
import type { PingPayload } from "../../../../src/core/pings/ping_payload";
2635

@@ -334,10 +343,7 @@ describe("EventsDatabase", function() {
334343
}));
335344

336345
// Move the clock forward by one minute to look like Glean was really restarted.
337-
Context.startTime.setTime(Context.startTime.getTime() + 1000 * 60);
338-
// Fake a re-start.
339-
const db2 = new EventsDatabase();
340-
await db2.initialize();
346+
const db2 = await testRestartGlean();
341347

342348
for (const store of stores) {
343349
const snapshot = await db2.getPingEvents(store, true);
@@ -373,10 +379,7 @@ describe("EventsDatabase", function() {
373379
}));
374380

375381
// Move the clock forward by one minute.
376-
Context.startTime.setTime(Context.startTime.getTime() + 1000 * 60);
377-
// Fake a re-start.
378-
db = new EventsDatabase();
379-
await db.initialize();
382+
db = await testRestartGlean();
380383
}
381384

382385
const snapshot = await db.getPingEvents("store", true);
@@ -577,10 +580,7 @@ describe("EventsDatabase", function() {
577580

578581
// Move the clock forward by one hour.
579582
const restartedTimeOffset = 1000 * 60 * 60;
580-
Context.startTime.setTime(firstStartTime.getTime() + restartedTimeOffset);
581-
// Product is rebooted.
582-
db = new EventsDatabase();
583-
await db.initialize();
583+
db = await testRestartGlean(restartedTimeOffset);
584584

585585
// New events are recorded.
586586
await db.record(event, new RecordedEvent({
@@ -647,10 +647,7 @@ describe("EventsDatabase", function() {
647647

648648
// Move the clock forward by one hour.
649649
const restartedTimeOffset = 1000 * 60 * 60;
650-
Context.startTime.setTime(firstStartTime.getTime() + restartedTimeOffset);
651-
// Product is rebooted.
652-
db = new EventsDatabase();
653-
await db.initialize();
650+
db = await testRestartGlean(restartedTimeOffset);
654651

655652
// New set of events are recorded
656653
await db.record(event, new RecordedEvent({
@@ -665,10 +662,7 @@ describe("EventsDatabase", function() {
665662
}));
666663

667664
// Move the clock forward by one more hour.
668-
Context.startTime.setTime(firstStartTime.getTime() + restartedTimeOffset * 2);
669-
// Product is rebooted.
670-
db = new EventsDatabase();
671-
await db.initialize();
665+
db = await testRestartGlean(restartedTimeOffset);
672666

673667
// New set of events are recorded
674668
await db.record(event, new RecordedEvent({

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import assert from "assert";
66
import sinon from "sinon";
77

8-
import { InternalPingType as PingType} from "../../../../src/core/pings/ping_type";
8+
import { InternalPingType as PingType } from "../../../../src/core/pings/ping_type";
99
import * as PingMaker from "../../../../src/core/pings/maker";
1010
import Glean from "../../../../src/core/glean";
1111
import CoreEvents from "../../../../src/core/events";
@@ -15,7 +15,11 @@ import { Context } from "../../../../src/core/context";
1515
import { stopGleanUploader } from "../../../utils";
1616
import EventMetricType from "../../../../src/core/metrics/types/event";
1717
import { Lifetime } from "../../../../src/core/metrics/lifetime";
18-
import { testInitializeGlean, testUninitializeGlean } from "../../../../src/core/testing/utils";
18+
import {
19+
testInitializeGlean,
20+
testRestartGlean,
21+
testUninitializeGlean,
22+
} from "../../../../src/core/testing/utils";
1923
import { testResetGlean } from "../../../../src/core/testing";
2024
import type { ExtraMap } from "../../../../src/core/metrics/events_database/recorded_event";
2125

@@ -296,8 +300,7 @@ describe("PingMaker", function() {
296300
};
297301

298302
const triggerRestartedEvent = async () => {
299-
// Easiest way to simulate a restart and create a `glean.restarted` event
300-
await testResetGlean(testAppId, true, undefined, false);
303+
await testRestartGlean();
301304
};
302305

303306
// Record events

0 commit comments

Comments
 (0)