Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile] Recheck active runs before canceling background schedules

laurenspriem494256744671
diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
index 4d31232d68..6522e1e450 100644
--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
@@ -23,6 +23,7 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterCallbackInformation
import java.util.UUID
import java.util.concurrent.Executors
+import java.util.concurrent.FutureTask
import java.util.concurrent.TimeUnit
import org.json.JSONArray
import org.json.JSONObject
@@ -158,7 +159,6 @@ internal object BackgroundRuntime {
return
}
if (!enabled) requestStop()
- val activeIdentifier = active?.configuration?.identifier
scheduler.execute {
try {
val preferences = app.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
@@ -189,6 +189,12 @@ internal object BackgroundRuntime {
previousTasks.getJSONObject(index).getString("identifier")
)
}
+ val activeTask = FutureTask {
+ if (!enabled) requestStop()
+ active?.configuration?.identifier
+ }
+ main.post(activeTask)
+ val activeIdentifier = activeTask.get()
for (identifier in existingIdentifiers) {
if (identifier !in selected && identifier != activeIdentifier) {
manager.cancelUniqueWork(identifier).result.get()
diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt
index 73509c344b..6cac31a78c 100644
--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt
+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt
@@ -9,8 +9,10 @@ import androidx.work.Data
import androidx.work.ListenableWorker
import androidx.work.WorkInfo
import androidx.work.WorkManager
+import androidx.work.impl.WorkManagerImpl
import androidx.work.testing.TestListenableWorkerBuilder
import androidx.work.testing.WorkManagerTestInitHelper
+import com.google.common.util.concurrent.SettableFuture
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
@@ -22,6 +24,8 @@ import io.flutter.plugin.common.StandardMethodCodec
import io.flutter.view.FlutterCallbackInformation
import java.nio.ByteBuffer
import java.time.Duration
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
@@ -62,6 +66,10 @@ class BackgroundRuntimeTest {
)
private fun configure(policy: TaskConfiguration, enabled: Boolean = true) {
+ awaitReply(beginConfigure(policy, enabled))
+ }
+
+ private fun beginConfigure(policy: TaskConfiguration, enabled: Boolean = true): Reply {
val data = org.json.JSONObject(policy.encode())
val result = Reply()
BackgroundRuntime.configure(
@@ -77,6 +85,10 @@ class BackgroundRuntimeTest {
),
result,
)
+ return result
+ }
+
+ private fun awaitReply(result: Reply) {
val deadline = System.nanoTime() + 10_000_000_000L
while (!result.done && System.nanoTime() < deadline) {
main.idle()
@@ -100,6 +112,64 @@ class BackgroundRuntimeTest {
messenger.call("complete", mapOf("invocation" to ready["invocation"], "outcome" to outcome))
}
+ private fun reconfigureWhileStarting(policy: TaskConfiguration, enabled: Boolean) {
+ configure(policy)
+ val original = WorkManagerImpl.getInstance(app)
+ val scheduled = original.getWorkInfosForUniqueWork(policy.identifier).get()
+ val manager = spy(original)
+ val blocked = CountDownLatch(1)
+ val query = SettableFuture.create<List<WorkInfo>>()
+ doAnswer {
+ blocked.countDown()
+ query
+ }
+ .`when`(manager)
+ .getWorkInfosForUniqueWork(policy.identifier)
+ WorkManagerImpl.setDelegate(manager)
+ try {
+ val pendingQuery = Reply()
+ BackgroundRuntime.scheduledTasks(pendingQuery)
+ assertTrue("Scheduler did not reach the query", blocked.await(10, TimeUnit.SECONDS))
+ val changed = beginConfigure(policy.copy(identifier = "test.replacement"), enabled)
+ val running = worker(policy).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ query.set(scheduled)
+ awaitReply(pendingQuery)
+ awaitReply(changed)
+ verify(manager, never()).cancelUniqueWork(policy.identifier)
+ assertFalse(running.isDone)
+ assertFalse(
+ original
+ .getWorkInfosForUniqueWork(policy.identifier)
+ .get()
+ .single()
+ .state
+ .isFinished
+ )
+ if (enabled) {
+ assertTrue(channel.sent.isEmpty())
+ } else {
+ assertEquals("requested", (channel.sent.single().arguments as Map<*, *>)["reason"])
+ }
+ complete(channel, ready)
+ assertEquals(ListenableWorker.Result.success(), running.get())
+ val afterRetirement = Reply()
+ BackgroundRuntime.scheduledTasks(afterRetirement)
+ awaitReply(afterRetirement)
+ assertEquals(
+ WorkInfo.State.CANCELLED,
+ original.getWorkInfosForUniqueWork(policy.identifier).get().single().state,
+ )
+ val enginesBeforeSkip = engines.size
+ assertEquals(ListenableWorker.Result.success(), worker(policy).startWork().get())
+ assertEquals(enginesBeforeSkip, engines.size)
+ } finally {
+ query.set(scheduled)
+ WorkManagerImpl.setDelegate(original)
+ }
+ }
+
@Test
fun lifecycleThroughNativeWorkerAndDartChannel() {
WorkManagerTestInitHelper.initializeTestWorkManager(app, Configuration.Builder().build())
@@ -338,6 +408,10 @@ class BackgroundRuntimeTest {
)
configure(cooperative)
+ reconfigureWhileStarting(cooperative, enabled = true)
+ reconfigureWhileStarting(cooperative, enabled = false)
+ configure(cooperative)
+
val teardownWorker = worker(cooperative)
var teardownCompletions = 0
BackgroundRuntime.start(teardownWorker, SystemClock.elapsedRealtime()) { success ->