Skip to content

Commit f857177

Browse files
author
Beatriz Rizental
authored
Merge pull request #1015 from brizental/upload-check-fix
No bug - Check if Glean is initialized before dispatching toggle upload task
2 parents eaf740a + 374463c commit f857177

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#984](https://github.com/mozilla/glean.js/pull/984): BUGFIX: Return correct upload result in case an error happens while building a ping request.
66
* [#988](https://github.com/mozilla/glean.js/pull/988): BUGFIX: Enforce rate limitation at upload time, not at ping submission time.
77
* Note: This change required a big refactoring of the internal uploading logic.
8+
* [#1015](https://github.com/mozilla/glean.js/pull/1015): BUGFIX: Make attempting to call the `setUploadEnabled` API before initializing Glean a no-op.
89

910
# v0.27.0 (2021-11-22)
1011

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)