-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add mechanism to remove deprecated WorkManager jobs #805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b52da00
we want to make sure that old work tags are cancelled
malmstein adbedb0
this could be private
malmstein 3f26bca
better naming for this method
malmstein bed3a0a
we want to separate the responsibilities of clearing old jobs and sch…
malmstein 67f48ff
we want to separate the responsibilities of clearing old jobs and sch…
malmstein b0b257d
Merge branch 'develop' into feature/david/remove_old_jobs
malmstein 76e1568
ensure we are scheduling some jobs before removing them
malmstein e4eb51c
adding work managet test library
malmstein 89cddf9
properly testing that the jobs are scheduled or not
malmstein a8eda12
initialising workmanager as documented
malmstein d69cf7d
better testing strategy
malmstein b75848c
add filter back
malmstein f2f8db0
formatting and comment cleanup
malmstein 0e6d77c
clean up duplicate scheduler code
malmstein bbde961
added new test to ensure non deprecated work is still enqueued
malmstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
app/src/androidTest/java/com/duckduckgo/app/job/JobCleanerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) 2020 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.job | ||
|
|
||
| import android.util.Log | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import androidx.work.Configuration | ||
| import androidx.work.OneTimeWorkRequestBuilder | ||
| import androidx.work.WorkInfo | ||
| import androidx.work.WorkManager | ||
| import androidx.work.impl.utils.SynchronousExecutor | ||
| import androidx.work.testing.WorkManagerTestInitHelper | ||
| import com.duckduckgo.app.job.JobCleaner.Companion.allDeprecatedNotificationWorkTags | ||
| import com.duckduckgo.app.notification.NotificationScheduler | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| class JobCleanerTest { | ||
|
|
||
| private val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
| private lateinit var workManager: WorkManager | ||
| private lateinit var testee: JobCleaner | ||
|
|
||
| @Before | ||
| fun before() { | ||
| initializeWorkManager() | ||
| testee = AndroidJobCleaner(workManager) | ||
| } | ||
|
|
||
| // https://developer.android.com/topic/libraries/architecture/workmanager/how-to/integration-testing | ||
| private fun initializeWorkManager() { | ||
| val config = Configuration.Builder() | ||
| .setMinimumLoggingLevel(Log.DEBUG) | ||
| .setExecutor(SynchronousExecutor()) | ||
| .build() | ||
|
|
||
| WorkManagerTestInitHelper.initializeTestWorkManager(context, config) | ||
| workManager = WorkManager.getInstance(context) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenStartedThenAllDeprecatedWorkIsCancelled() { | ||
| enqueueDeprecatedWorkers() | ||
| assertDeprecatedWorkersAreEnqueued() | ||
| testee.cleanDeprecatedJobs() | ||
| assertDeprecatedWorkersAreNotEnqueued() | ||
| } | ||
|
|
||
| @Test | ||
| fun whenStartedAndNoDeprecatedJobsAreScheduledThenNothingIsRemoved() { | ||
CDRussell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| enqueueNonDeprecatedWorkers() | ||
| assertNonDeprecatedWorkersAreEnqueued() | ||
| testee.cleanDeprecatedJobs() | ||
| assertNonDeprecatedWorkersAreEnqueued() | ||
| } | ||
|
|
||
| private fun enqueueDeprecatedWorkers() { | ||
| allDeprecatedNotificationWorkTags().forEach { | ||
| val requestBuilder = OneTimeWorkRequestBuilder<TestWorker>() | ||
| val request = requestBuilder | ||
| .addTag(it) | ||
| .setInitialDelay(10, TimeUnit.SECONDS) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we set this delay because if we don't the |
||
| .build() | ||
| workManager.enqueue(request) | ||
| } | ||
| } | ||
|
|
||
| private fun enqueueNonDeprecatedWorkers() { | ||
| allDeprecatedNotificationWorkTags().forEach { | ||
| val requestBuilder = OneTimeWorkRequestBuilder<TestWorker>() | ||
| val request = requestBuilder | ||
| .addTag(NotificationScheduler.UNUSED_APP_WORK_REQUEST_TAG) | ||
| .setInitialDelay(10, TimeUnit.SECONDS) | ||
| .build() | ||
| workManager.enqueue(request) | ||
| } | ||
| } | ||
|
|
||
| private fun assertDeprecatedWorkersAreEnqueued() { | ||
| allDeprecatedNotificationWorkTags().forEach { | ||
| val scheduledWorkers = getScheduledWorkers(it) | ||
| assertFalse(scheduledWorkers.isEmpty()) | ||
| } | ||
| } | ||
|
|
||
| private fun assertDeprecatedWorkersAreNotEnqueued() { | ||
| allDeprecatedNotificationWorkTags().forEach { | ||
| val scheduledWorkers = getScheduledWorkers(it) | ||
| assertTrue(scheduledWorkers.isEmpty()) | ||
| } | ||
| } | ||
|
|
||
| private fun assertNonDeprecatedWorkersAreEnqueued() { | ||
| val scheduledWorkers = getScheduledWorkers(NotificationScheduler.UNUSED_APP_WORK_REQUEST_TAG) | ||
| assertFalse(scheduledWorkers.isEmpty()) | ||
| } | ||
|
|
||
| private fun assertNonDeprecatedWorkersAreNotEnqueued() { | ||
| val scheduledWorkers = getScheduledWorkers(NotificationScheduler.UNUSED_APP_WORK_REQUEST_TAG) | ||
| assertTrue(scheduledWorkers.isEmpty()) | ||
| } | ||
|
|
||
|
|
||
| private fun getScheduledWorkers(tag: String): List<WorkInfo> { | ||
| return workManager | ||
| .getWorkInfosByTag(tag) | ||
| .get() | ||
| .filter { it.state == WorkInfo.State.ENQUEUED } | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
app/src/androidTest/java/com/duckduckgo/app/job/TestWorker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) 2020 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.job | ||
|
|
||
| import android.content.Context | ||
| import androidx.work.Worker | ||
| import androidx.work.WorkerParameters | ||
|
|
||
| class TestWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) { | ||
| override fun doWork(): Result { | ||
| return Result.success() | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
app/src/androidTest/java/com/duckduckgo/app/job/WorkSchedulerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright (c) 2020 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.job | ||
|
|
||
| import com.duckduckgo.app.notification.AndroidNotificationScheduler | ||
| import com.nhaarman.mockitokotlin2.mock | ||
| import com.nhaarman.mockitokotlin2.verify | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class WorkSchedulerTest { | ||
|
|
||
| private val notificationScheduler: AndroidNotificationScheduler = mock() | ||
| private val jobCleaner: JobCleaner = mock() | ||
|
|
||
| private lateinit var testee: WorkScheduler | ||
|
|
||
| @Before | ||
| fun before() { | ||
| testee = AndroidWorkScheduler( | ||
| notificationScheduler, | ||
| jobCleaner | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun schedulesNextNotificationAndCleansDeprecatedJobs() = runBlocking<Unit> { | ||
| testee.scheduleWork() | ||
|
|
||
| verify(notificationScheduler).scheduleNextNotification() | ||
| verify(jobCleaner).cleanDeprecatedJobs() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.