Skip to content

Commit a818beb

Browse files
committed
Guarantee Context.uploadEnabled is always set to something
We were not covering the case when upload was disabled in a previous run and Glean was also restarted with uploadEnabled=false.
1 parent b13eb45 commit a818beb

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

glean/src/core/glean.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class Glean {
271271
// Clear application lifetime metrics.
272272
await Context.metricsDatabase.clear(Lifetime.Application);
273273

274+
Context.uploadEnabled = uploadEnabled;
274275
// The upload enabled flag may have changed since the last run, for
275276
// example by the changing of a config file.
276277
if (uploadEnabled) {

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ describe("Glean", function() {
272272

273273
assert.strictEqual(postSpy.callCount, 1);
274274
assert.ok(postSpy.getCall(0).args[0].indexOf(DELETION_REQUEST_PING_NAME) !== -1);
275+
assert.strictEqual(Context.uploadEnabled, false);
275276

276277
postSpy.resetHistory();
277278
Glean.setUploadEnabled(true);
278279
await Context.dispatcher.testBlockOnQueue();
279280
assert.strictEqual(postSpy.callCount, 0);
281+
assert.strictEqual(Context.uploadEnabled, true);
280282
});
281283

282284
it("deletion request is sent when toggling upload from on to off and the pings queue is full", async function() {
@@ -299,6 +301,7 @@ describe("Glean", function() {
299301

300302
// This throws in case the ping is not sent.
301303
await waitForDeletionRequestPing;
304+
assert.strictEqual(Context.uploadEnabled, false);
302305
});
303306

304307
it("deletion request ping is sent when toggling upload status between runs", async function() {
@@ -317,35 +320,50 @@ describe("Glean", function() {
317320
}
318321
);
319322

323+
// If ping was not sent this promise will reject.
320324
await pingBody;
325+
assert.strictEqual(Context.uploadEnabled, false);
321326
});
322327

323328
it("deletion request ping is not sent if upload status does not change between runs", async function () {
324-
const postSpy = sandbox.spy(Glean.platform.uploader, "post");
329+
const mockUploader = new WaitableUploader();
330+
let pingBody = mockUploader.waitForPingSubmission("deletion-request");
331+
await Glean.testResetGlean(
332+
testAppId,
333+
true,
334+
{
335+
httpClient: mockUploader,
336+
}
337+
);
325338

326339
Glean.setUploadEnabled(false);
327-
await Context.dispatcher.testBlockOnQueue();
340+
// If ping was not sent this promise will reject.
341+
await pingBody;
342+
assert.strictEqual(Context.uploadEnabled, false);
328343

329-
// A deletion request is sent
330-
assert.strictEqual(postSpy.callCount, 1);
331-
assert.ok(postSpy.getCall(0).args[0].indexOf(DELETION_REQUEST_PING_NAME) !== -1);
344+
// Can't clear stores here,
345+
// otherwise Glean won't know upload has been disabled in a previous run.
346+
await Glean.testUninitialize(false);
332347

333-
// Can't use testResetGlean here because it clears all stores
334-
// and when there is no client_id at all stored, a deletion ping is also not set.
335-
await Glean.testUninitialize();
336-
await Glean.testInitialize(testAppId, false);
337-
await Context.dispatcher.testBlockOnQueue();
338-
// TODO: Make this nicer once we resolve Bug 1691033 is resolved.
339-
await Glean["pingUploader"].testBlockOnPingsQueue();
348+
pingBody = mockUploader.waitForPingSubmission("deletion-request");
349+
await Glean.testInitialize(
350+
testAppId,
351+
false,
352+
{
353+
httpClient: mockUploader,
354+
}
355+
);
340356

341-
postSpy.resetHistory();
342-
assert.strictEqual(postSpy.callCount, 0);
357+
// If ping was not sent this promise will reject.
358+
await assert.rejects(pingBody);
359+
assert.strictEqual(Context.uploadEnabled, false);
343360
});
344361

345362
it("deletion request ping is not sent when user starts Glean for the first time with upload disabled", async function () {
346363
const postSpy = sandbox.spy(Glean.platform.uploader, "post");
347364
await Glean.testResetGlean(testAppId, false);
348365
assert.strictEqual(postSpy.callCount, 0);
366+
assert.strictEqual(Context.uploadEnabled, false);
349367
});
350368

351369
it("setting log pings works before and after and on initialize", async function () {

0 commit comments

Comments
 (0)