Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Bug 1548535 - Cancel any pending workers when ping upload disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
travis79 committed Aug 8, 2019
1 parent cc3cdea commit 865feaf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 865feaf

Please sign in to comment.