π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[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.ktindex 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.MethodChannelimport io.flutter.view.FlutterCallbackInformationimport java.util.UUIDimport java.util.concurrent.Executors+import java.util.concurrent.FutureTaskimport java.util.concurrent.TimeUnitimport org.json.JSONArrayimport org.json.JSONObject@@ -158,7 +159,6 @@ internal object BackgroundRuntime {return}if (!enabled) requestStop()- val activeIdentifier = active?.configuration?.identifierscheduler.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.ktindex 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.Dataimport androidx.work.ListenableWorkerimport androidx.work.WorkInfoimport androidx.work.WorkManager+import androidx.work.impl.WorkManagerImplimport androidx.work.testing.TestListenableWorkerBuilderimport androidx.work.testing.WorkManagerTestInitHelper+import com.google.common.util.concurrent.SettableFutureimport io.flutter.FlutterInjectorimport io.flutter.embedding.engine.FlutterEngineimport io.flutter.embedding.engine.dart.DartExecutor@@ -22,6 +24,8 @@ import io.flutter.plugin.common.StandardMethodCodecimport io.flutter.view.FlutterCallbackInformationimport java.nio.ByteBufferimport java.time.Duration+import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnitimport org.junit.Assert.*import org.junit.Testimport 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_000Lwhile (!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)+ }+ }+@Testfun 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 = 0BackgroundRuntime.start(teardownWorker, SystemClock.elapsedRealtime()) { success ->