From 865feaf446fb78d50cf116e87e6cf1971316d791 Mon Sep 17 00:00:00 2001 From: Travis Long Date: Thu, 8 Aug 2019 13:07:12 -0500 Subject: [PATCH] Bug 1548535 - Cancel any pending workers when ping upload disabled --- .../mozilla/components/service/glean/Glean.kt | 12 +++++++++ .../components/service/glean/GleanTest.kt | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt b/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt index a9b2453751e..580230b3643 100644 --- a/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt +++ b/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt @@ -10,6 +10,7 @@ import android.content.pm.PackageManager import android.os.Build import androidx.annotation.VisibleForTesting import androidx.lifecycle.ProcessLifecycleOwner +import androidx.work.WorkManager import kotlinx.coroutines.Job import kotlinx.coroutines.joinAll import mozilla.components.service.glean.GleanMetrics.GleanBaseline @@ -23,6 +24,7 @@ import mozilla.components.service.glean.ping.PingMaker import mozilla.components.service.glean.private.PingType import mozilla.components.service.glean.scheduler.GleanLifecycleObserver import mozilla.components.service.glean.scheduler.MetricsPingScheduler +import mozilla.components.service.glean.scheduler.MetricsPingWorker import mozilla.components.service.glean.scheduler.PingUploadWorker import mozilla.components.service.glean.storages.StorageEngineManager import mozilla.components.service.glean.storages.PingStorageEngine @@ -205,10 +207,20 @@ open class GleanInternalAPI internal constructor () { if (enabled) { initializeCoreMetrics(applicationContext!!) } else { + cancelPingWorkers() clearMetrics() } } + /** + * Cancel any pending [PingUploadWorker] objects that have been enqueued. + */ + private fun cancelPingWorkers() { + val workManager = WorkManager.getInstance() + workManager.cancelUniqueWork(PingUploadWorker.PING_WORKER_TAG) + workManager.cancelUniqueWork(MetricsPingWorker.TAG) + } + /** * Clear any pending metrics when telemetry is disabled. */ diff --git a/components/service/glean/src/test/java/mozilla/components/service/glean/GleanTest.kt b/components/service/glean/src/test/java/mozilla/components/service/glean/GleanTest.kt index 58ac557022b..dec7bdaeaa4 100644 --- a/components/service/glean/src/test/java/mozilla/components/service/glean/GleanTest.kt +++ b/components/service/glean/src/test/java/mozilla/components/service/glean/GleanTest.kt @@ -24,6 +24,7 @@ import mozilla.components.service.glean.private.PingType import mozilla.components.service.glean.private.StringMetricType import mozilla.components.service.glean.private.UuidMetricType import mozilla.components.service.glean.scheduler.GleanLifecycleObserver +import mozilla.components.service.glean.scheduler.MetricsPingWorker import mozilla.components.service.glean.scheduler.PingUploadWorker import mozilla.components.service.glean.storages.StorageEngineManager import mozilla.components.service.glean.storages.StringsStorageEngine @@ -50,6 +51,7 @@ import java.io.BufferedReader import java.io.File import java.io.FileReader import java.time.Instant +import java.util.Calendar import java.util.Date import java.util.Locale import java.util.UUID @@ -575,6 +577,29 @@ class GleanTest { assertTrue(GleanInternalMetrics.os.testHasValue()) } + @Test + fun `Workers should be cancelled when disabling uploading`() { + // Force the MetricsPingScheduler to schedule the MetricsPingWorker + Glean.metricsPingScheduler.schedulePingCollection(Calendar.getInstance(), true) + // Enqueue a worker to send the baseline ping + Pings.baseline.send() + + // Verify that the workers are enqueued + assertTrue("PingUploadWorker is enqueued", + getWorkerStatus(PingUploadWorker.PING_WORKER_TAG).isEnqueued) + assertTrue("MetricsPingWorker is enqueued", + getWorkerStatus(MetricsPingWorker.TAG).isEnqueued) + + // Toggle upload enabled to false + Glean.setUploadEnabled(false) + + // Verify workers have been cancelled + assertFalse("PingUploadWorker is not enqueued", + getWorkerStatus(PingUploadWorker.PING_WORKER_TAG).isEnqueued) + assertFalse("MetricsPingWorker is not enqueued", + getWorkerStatus(MetricsPingWorker.TAG).isEnqueued) + } + @Test fun `firstRunDate is managed correctly when disabling and enabling metrics`() { val originalFirstRunDate = GleanInternalMetrics.firstRunDate.testGetValue()