Skip to content

Commit 314376b

Browse files
committed
BUGFIX: Check if Glean is initialized before dispatching toggle upload task
Checking inside the task had no meaning, because that would guarantee the call to be executed _after_ the initialize task. That is the case, because the initialize task is _always_ the first task to be executed by the dispatcher regardless of any other previously dispatched tasks.
1 parent eaf740a commit 314376b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

glean/src/core/glean.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ class Glean {
355355
* @param flag When true, enable metric collection.
356356
*/
357357
static setUploadEnabled(flag: boolean): void {
358+
if (!Context.initialized) {
359+
log(
360+
LOG_TAG,
361+
[
362+
"Changing upload enabled before Glean is initialized is not supported.\n",
363+
"Pass the correct state into `Glean.initialize`.\n",
364+
"See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk`"
365+
],
366+
LoggingLevel.Error
367+
);
368+
return;
369+
}
370+
358371
if (!isBoolean(flag)) {
359372
log(
360373
LOG_TAG,
@@ -365,19 +378,6 @@ class Glean {
365378
}
366379

367380
Context.dispatcher.launch(async () => {
368-
if (!Context.initialized) {
369-
log(
370-
LOG_TAG,
371-
[
372-
"Changing upload enabled before Glean is initialized is not supported.\n",
373-
"Pass the correct state into `Glean.initialize\n`.",
374-
"See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk`"
375-
],
376-
LoggingLevel.Error
377-
);
378-
return;
379-
}
380-
381381
if (Context.uploadEnabled !== flag) {
382382
if (flag) {
383383
await Glean.onUploadEnabled();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ describe("Glean", function() {
164164
assert.strictEqual(spy.callCount, 1);
165165
});
166166

167+
it("attempting to change upload status prior to initialize is a no-op", async function() {
168+
await Glean.testUninitialize();
169+
170+
const launchSpy = sandbox.spy(Context.dispatcher, "launch");
171+
Glean.setUploadEnabled(false);
172+
assert.strictEqual(launchSpy.callCount, 0);
173+
});
174+
167175
it("initialization throws if applicationId is an empty string", async function() {
168176
await Glean.testUninitialize();
169177
try {

0 commit comments

Comments
 (0)