From b36eb59096b5e389cef8c71951ccc652ea3a781c Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 03:41:17 +0530 Subject: [PATCH 1/2] Notify SourceOfTruthWithBarrier on delete and deleteAll (#731) --- .../store/store5/impl/RefCountedResource.kt | 5 ++ .../store5/impl/SourceOfTruthWithBarrier.kt | 62 +++++++++++++++++-- .../store5/SourceOfTruthWithBarrierTests.kt | 45 ++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RefCountedResource.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RefCountedResource.kt index d98473292..3a2d70a48 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RefCountedResource.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RefCountedResource.kt @@ -52,6 +52,11 @@ internal class RefCountedResource( } } + suspend fun activeKeys(): List = + lock.withLock { + items.keys.toList() + } + // used in tests suspend fun size() = lock.withLock { diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/SourceOfTruthWithBarrier.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/SourceOfTruthWithBarrier.kt index f9926a25f..99b84adc0 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/SourceOfTruthWithBarrier.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/SourceOfTruthWithBarrier.kt @@ -85,7 +85,7 @@ internal class SourceOfTruthWithBarrier delegate.reader(key).mapIndexed { index, local: Output? -> - if (index == 0 && messageArrivedAfterMe) { + if (index == 0 && messageArrivedAfterMe && !barrierMessage.isDelete) { val firstMsgOrigin = if (writeError == null) { // restarted barrier without an error means write succeeded @@ -191,11 +191,61 @@ internal class SourceOfTruthWithBarrier + barrier.value = BarrierMsg.Blocked(versionCounter.incrementAndGet()) + } + try { + delegate.deleteAll() + } catch (cancellation: CancellationException) { + throw cancellation + } catch (throwable: Throwable) { + deleteException = throwable + } + } finally { + val nextVersion = versionCounter.incrementAndGet() + acquired.forEach { (_, barrier) -> + barrier.value = + BarrierMsg.Open( + version = nextVersion, + writeError = deleteException, + isDelete = true, + ) + } + withContext(NonCancellable) { + acquired.forEach { (key, barrier) -> + barriers.release(key, barrier) + } + } + } } private sealed class BarrierMsg( @@ -203,7 +253,11 @@ internal class SourceOfTruthWithBarrier() + lock.complete(Unit) + val reader = source.reader(1, lock) + val collected = mutableListOf>() + val collection = + launch { + reader.collect { + collected.add(it) + } + } + source.write(1, "initial") + advanceUntilIdle() + + assertEquals( + listOf>( + StoreReadResponse.Data( + origin = StoreReadResponseOrigin.SourceOfTruth, + value = "initial", + ), + ), + collected, + ) + + source.delete(1) + advanceUntilIdle() + + assertEquals( + listOf>( + StoreReadResponse.Data( + origin = StoreReadResponseOrigin.SourceOfTruth, + value = "initial", + ), + StoreReadResponse.Data( + origin = StoreReadResponseOrigin.SourceOfTruth, + value = null, + ), + ), + collected, + ) + collection.cancelAndJoin() + } } From c85d3eeddc02aed602cba6662fdbdeaa09c0942b Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 04:20:34 +0530 Subject: [PATCH 2/2] Add connectedAndroidDeviceTest for ClearBarrier --- store/build.gradle.kts | 9 ++ .../store/store5/ClearBarrierDeviceTest.kt | 87 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/ClearBarrierDeviceTest.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/ClearBarrierDeviceTest.kt b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/ClearBarrierDeviceTest.kt new file mode 100644 index 000000000..8fe2209aa --- /dev/null +++ b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/ClearBarrierDeviceTest.kt @@ -0,0 +1,87 @@ +package org.mobilenativefoundation.store.store5 + +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.runTest +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +@OptIn(ExperimentalStoreApi::class) +class ClearBarrierDeviceTest { + + @Test + fun clearKeyEvictsSourceOfTruthOnDevice() = runTest { + val storage = mutableMapOf() + val dataFlow = MutableStateFlow("initial_device_val") + storage["item"] = "initial_device_val" + + val sot = SourceOfTruth.of( + reader = { dataFlow }, + writer = { k, v -> + storage[k] = v + dataFlow.value = v + }, + delete = { k -> + storage.remove(k) + dataFlow.value = null + }, + deleteAll = { + storage.clear() + dataFlow.value = null + } + ) + + val store = StoreBuilder.from( + fetcher = Fetcher.of { key -> "fetched_$key" }, + sourceOfTruth = sot + ).disableCache() + .build() + + // Read initial data from SourceOfTruth + val initial = store.stream(StoreReadRequest.cached("item", refresh = false)) + .filterIsInstance>() + .first() + assertEquals("initial_device_val", initial.value) + + // Clear key should trigger delete on SOT + store.clear("item") + assertNull(storage["item"]) + assertNull(dataFlow.value) + } + + @Test + fun clearAllEvictsAllSourceOfTruthEntriesOnDevice() = runTest { + val storage = mutableMapOf() + val dataFlow = MutableStateFlow("initial_all") + storage["k1"] = "initial_all" + + val sot = SourceOfTruth.of( + reader = { dataFlow }, + writer = { k, v -> + storage[k] = v + dataFlow.value = v + }, + delete = { k -> + storage.remove(k) + dataFlow.value = null + }, + deleteAll = { + storage.clear() + dataFlow.value = null + } + ) + + val store = StoreBuilder.from( + fetcher = Fetcher.of { key -> "fetched_$key" }, + sourceOfTruth = sot + ).disableCache() + .build() + + store.clear() + assertNull(storage["k1"]) + assertNull(dataFlow.value) + } +}