From 98792982f57e2fe74961f817b338b5dbae36f11a Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 03:48:54 +0530 Subject: [PATCH 1/2] Support configurable PushFailurePolicy on MutableStore (#678) --- .../store/store5/MutableStoreBuilder.kt | 2 + .../store/store5/PushFailurePolicy.kt | 21 ++++++++ .../store/store5/impl/RealMutableStore.kt | 44 +++++++++++++++-- .../store5/impl/RealMutableStoreBuilder.kt | 8 +++ .../store/store5/impl/extensions/store.kt | 3 ++ .../mutablestore/RealMutableStoreTest.kt | 49 +++++++++++++++++++ 6 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicy.kt diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/MutableStoreBuilder.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/MutableStoreBuilder.kt index 21ec51c89..b8589c9ea 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/MutableStoreBuilder.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/MutableStoreBuilder.kt @@ -32,6 +32,8 @@ interface MutableStoreBuilder): MutableStoreBuilder + fun pushFailurePolicy(policy: PushFailurePolicy): MutableStoreBuilder + companion object { /** * Creates a new [MutableStoreBuilder] from a [Fetcher] and a [SourceOfTruth]. diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicy.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicy.kt new file mode 100644 index 000000000..b1bd10920 --- /dev/null +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicy.kt @@ -0,0 +1,21 @@ +package org.mobilenativefoundation.store.store5 + +/** + * Defines the strategy to execute when an eager push to remote fails during a stream request. + */ +enum class PushFailurePolicy { + /** + * Stop the stream and emit a [StoreReadResponse.Error] immediately without fetching from network. + */ + FAIL_FAST, + + /** + * Suppress remote network fetch and serve only local data, preserving unpushed local changes. + */ + SKIP_FETCH_KEEP_LOCAL, + + /** + * Log the error and proceed with the read request, which may fetch remote data (legacy default). + */ + IGNORE_AND_FETCH, +} diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt index 1c396096b..7d3bf9a45 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt @@ -12,8 +12,10 @@ import org.mobilenativefoundation.store.store5.Bookkeeper import org.mobilenativefoundation.store.store5.Clear import org.mobilenativefoundation.store.store5.Logger import org.mobilenativefoundation.store.store5.MutableStore +import org.mobilenativefoundation.store.store5.PushFailurePolicy import org.mobilenativefoundation.store.store5.StoreReadRequest import org.mobilenativefoundation.store.store5.StoreReadResponse +import org.mobilenativefoundation.store.store5.StoreReadResponseOrigin import org.mobilenativefoundation.store.store5.StoreWriteRequest import org.mobilenativefoundation.store.store5.StoreWriteResponse import org.mobilenativefoundation.store.store5.Updater @@ -27,6 +29,7 @@ internal class RealMutableStore, private val bookkeeper: Bookkeeper?, private val logger: Logger = DefaultLogger(), + private val pushFailurePolicy: PushFailurePolicy = PushFailurePolicy.IGNORE_AND_FETCH, private val beforeAcknowledgementCommit: suspend () -> Unit = {}, ) : MutableStore, Clear.All { private val storeLock = Mutex() @@ -36,10 +39,45 @@ internal class RealMutableStore(request.key, state)) { - is EagerConflictResolutionResult.Error.Exception -> logger.error(result.error.toString()) - is EagerConflictResolutionResult.Error.Message -> logger.error(result.message) + is EagerConflictResolutionResult.Error.Exception -> { + logger.error(result.error.toString()) + when (pushFailurePolicy) { + PushFailurePolicy.FAIL_FAST -> { + emit( + StoreReadResponse.Error.Exception( + error = result.error, + origin = StoreReadResponseOrigin.SourceOfTruth, + ), + ) + return@flow + } + PushFailurePolicy.SKIP_FETCH_KEEP_LOCAL -> { + delegate.stream(StoreReadRequest.cached(request.key, refresh = false)).collect { emit(it) } + return@flow + } + PushFailurePolicy.IGNORE_AND_FETCH -> Unit + } + } + is EagerConflictResolutionResult.Error.Message -> { + logger.error(result.message) + when (pushFailurePolicy) { + PushFailurePolicy.FAIL_FAST -> { + emit( + StoreReadResponse.Error.Message( + message = result.message, + origin = StoreReadResponseOrigin.SourceOfTruth, + ), + ) + return@flow + } + PushFailurePolicy.SKIP_FETCH_KEEP_LOCAL -> { + delegate.stream(StoreReadRequest.cached(request.key, refresh = false)).collect { emit(it) } + return@flow + } + PushFailurePolicy.IGNORE_AND_FETCH -> Unit + } + } is EagerConflictResolutionResult.Success.ConflictsResolved -> logger.debug(result.value.toString()) EagerConflictResolutionResult.Success.NoConflicts -> logger.debug("No conflicts.") } diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStoreBuilder.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStoreBuilder.kt index ff9696134..fa1f3d3e8 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStoreBuilder.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStoreBuilder.kt @@ -10,6 +10,7 @@ import org.mobilenativefoundation.store.store5.Fetcher import org.mobilenativefoundation.store.store5.MemoryPolicy import org.mobilenativefoundation.store.store5.MutableStore import org.mobilenativefoundation.store.store5.MutableStoreBuilder +import org.mobilenativefoundation.store.store5.PushFailurePolicy import org.mobilenativefoundation.store.store5.SourceOfTruth import org.mobilenativefoundation.store.store5.Store import org.mobilenativefoundation.store.store5.StoreDefaults @@ -45,6 +46,7 @@ internal class RealMutableStoreBuilder? = StoreDefaults.memoryPolicy private var validator: Validator? = null + private var pushFailurePolicy: PushFailurePolicy = PushFailurePolicy.IGNORE_AND_FETCH override fun scope(scope: CoroutineScope): MutableStoreBuilder { this.scope = scope @@ -66,6 +68,11 @@ internal class RealMutableStoreBuilder { + this.pushFailurePolicy = policy + return this + } + fun build(): Store = RealStore( scope = scope ?: GlobalScope, @@ -105,5 +112,6 @@ internal class RealMutableStoreBuilder( updater = updater, bookkeeper = bookkeeper, + pushFailurePolicy = pushFailurePolicy, ) } diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt index cdf0910c7..0448529bc 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.first import org.mobilenativefoundation.store.core5.ExperimentalStoreApi import org.mobilenativefoundation.store.store5.Bookkeeper import org.mobilenativefoundation.store.store5.MutableStore +import org.mobilenativefoundation.store.store5.PushFailurePolicy import org.mobilenativefoundation.store.store5.Store import org.mobilenativefoundation.store.store5.StoreReadRequest import org.mobilenativefoundation.store.store5.StoreReadResponse @@ -88,6 +89,7 @@ suspend fun Store.fresh(key: Key) = fun Store.asMutableStore( updater: Updater, bookkeeper: Bookkeeper?, + pushFailurePolicy: PushFailurePolicy = PushFailurePolicy.IGNORE_AND_FETCH, ): MutableStore { val delegate = this as? RealStore @@ -97,6 +99,7 @@ fun Store< delegate = delegate, updater = updater, bookkeeper = bookkeeper, + pushFailurePolicy = pushFailurePolicy, ) } diff --git a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt index 734193b74..fa3cfac1a 100644 --- a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt +++ b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.toList import kotlinx.coroutines.test.runTest import org.mobilenativefoundation.store.core5.ExperimentalStoreApi import org.mobilenativefoundation.store.store5.FetcherResult +import org.mobilenativefoundation.store.store5.PushFailurePolicy import org.mobilenativefoundation.store.store5.SourceOfTruth import org.mobilenativefoundation.store.store5.StoreReadRequest import org.mobilenativefoundation.store.store5.StoreReadResponse @@ -155,6 +156,54 @@ class RealMutableStoreTest { assertNotNull(testBookkeeper.getLastFailedSync("key3")) } + @Test + fun stream_givenConflictResolutionFailsAndFailFast_whenReading_thenEmitsErrorAndAborts() = + runTest { + val failFastStore = + RealMutableStore( + delegate = delegateStore, + updater = testUpdater, + bookkeeper = testBookkeeper, + logger = testLogger, + pushFailurePolicy = PushFailurePolicy.FAIL_FAST, + ) + val request = StoreReadRequest.Companion.cached("keyFailFast", refresh = true) + val errorMessage = "Conflict resolution failure" + delegateStore.write("keyFailFast", Note("localId", "localContent")) + testBookkeeper.setLastFailedSync("keyFailFast") + testUpdater.errorMessage = errorMessage + + val results = failFastStore.stream(request).toList() + + assertEquals(1, results.size) + assertIs(results[0]) + assertEquals(errorMessage, (results[0] as StoreReadResponse.Error.Message).message) + } + + @Test + fun stream_givenConflictResolutionFailsAndSkipFetchKeepLocal_whenReading_thenEmitsLocalOnly() = + runTest { + val skipFetchStore = + RealMutableStore( + delegate = delegateStore, + updater = testUpdater, + bookkeeper = testBookkeeper, + logger = testLogger, + pushFailurePolicy = PushFailurePolicy.SKIP_FETCH_KEEP_LOCAL, + ) + val request = StoreReadRequest.Companion.cached("keySkipFetch", refresh = true) + val errorMessage = "Conflict resolution failure" + delegateStore.write("keySkipFetch", Note("localId", "localContent")) + testBookkeeper.setLastFailedSync("keySkipFetch") + testUpdater.errorMessage = errorMessage + + val results = skipFetchStore.stream(request).take(1).toList() + + assertEquals(1, results.size) + assertIs>(results[0]) + assertEquals("localContent", (results[0] as StoreReadResponse.Data).value.content) + } + @Test fun stream_givenWriteFlowAndNoConflicts_whenCollecting_thenLocalAndNetworkAreUpdated() = runTest { From efae18127c09900f99517d866c3a3b5957e9cc32 Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 04:22:05 +0530 Subject: [PATCH 2/2] Add connectedAndroidDeviceTest for PushFailurePolicy --- store/build.gradle.kts | 9 ++ .../store5/PushFailurePolicyDeviceTest.kt | 119 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicyDeviceTest.kt diff --git a/store/build.gradle.kts b/store/build.gradle.kts index 900c73d61..f444207d2 100644 --- a/store/build.gradle.kts +++ b/store/build.gradle.kts @@ -25,6 +25,15 @@ kotlin { implementation(libs.turbine) } } + + val androidDeviceTest by getting { + dependencies { + implementation(libs.junit) + implementation(libs.kotlinx.coroutines.test) + implementation(kotlin("test")) + implementation("androidx.test:runner:1.6.2") + } + } } } diff --git a/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicyDeviceTest.kt b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicyDeviceTest.kt new file mode 100644 index 000000000..01aef752d --- /dev/null +++ b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/PushFailurePolicyDeviceTest.kt @@ -0,0 +1,119 @@ +package org.mobilenativefoundation.store.store5 + +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.test.runTest +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi +import org.mobilenativefoundation.store.store5.impl.extensions.asMutableStore +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs + +@OptIn(ExperimentalStoreApi::class) +class PushFailurePolicyDeviceTest { + + @Test + fun pushFailurePolicyFailFastOnDevice() = runTest { + val storage = mutableMapOf() + val dataFlow = MutableStateFlow("local_val") + storage["key1"] = "local_val" + + val sot = SourceOfTruth.of( + reader = { dataFlow }, + writer = { k, v -> + storage[k] = v + dataFlow.value = v + } + ) + + val delegateStore = StoreBuilder.from( + fetcher = Fetcher.of { key -> "remote_$key" }, + sourceOfTruth = sot + ).disableCache() + .build() + + val updater = Updater.by( + post = { key, value -> + UpdaterResult.Error.Message("Update failed on device") + } + ) + + val failedKeys = mutableSetOf("key1") + val bookkeeper = object : Bookkeeper { + override suspend fun getLastFailedSync(key: String): Long? = if (failedKeys.contains(key)) 12345L else null + override suspend fun setLastFailedSync(key: String, timestamp: Long): Boolean = true + override suspend fun clear(key: String): Boolean = failedKeys.remove(key) + override suspend fun clearAll(): Boolean { + failedKeys.clear() + return true + } + } + + val failFastStore = delegateStore.asMutableStore( + updater = updater, + bookkeeper = bookkeeper, + pushFailurePolicy = PushFailurePolicy.FAIL_FAST + ) + + val responses = failFastStore.stream( + StoreReadRequest.cached("key1", refresh = true) + ).toList() + + assertEquals(1, responses.size) + assertIs(responses[0]) + assertEquals("Update failed on device", (responses[0] as StoreReadResponse.Error.Message).message) + } + + @Test + fun pushFailurePolicySkipFetchKeepLocalOnDevice() = runTest { + val storage = mutableMapOf() + val dataFlow = MutableStateFlow("local_val") + storage["key1"] = "local_val" + + val sot = SourceOfTruth.of( + reader = { dataFlow }, + writer = { k, v -> + storage[k] = v + dataFlow.value = v + } + ) + + val delegateStore = StoreBuilder.from( + fetcher = Fetcher.of { key -> "remote_$key" }, + sourceOfTruth = sot + ).disableCache() + .build() + + val updater = Updater.by( + post = { key, value -> + UpdaterResult.Error.Message("Update failed on device") + } + ) + + val failedKeys = mutableSetOf("key1") + val bookkeeper = object : Bookkeeper { + override suspend fun getLastFailedSync(key: String): Long? = if (failedKeys.contains(key)) 12345L else null + override suspend fun setLastFailedSync(key: String, timestamp: Long): Boolean = true + override suspend fun clear(key: String): Boolean = failedKeys.remove(key) + override suspend fun clearAll(): Boolean { + failedKeys.clear() + return true + } + } + + val skipFetchStore = delegateStore.asMutableStore( + updater = updater, + bookkeeper = bookkeeper, + pushFailurePolicy = PushFailurePolicy.SKIP_FETCH_KEEP_LOCAL + ) + + val responses = skipFetchStore.stream( + StoreReadRequest.cached("key1", refresh = true) + ).take(1).toList() + + assertEquals(1, responses.size) + assertIs>(responses[0]) + assertEquals("local_val", (responses[0] as StoreReadResponse.Data).value) + } +}