Modern Apps git

← Modern-Apps.git

todo: make update-before-load deterministic

Commit
5d53206be7a5e5c1d7965e07a5628d74137d9905
Author
Vayun Mathur <[email protected]>
When
2026-11-17 02:44 UTC (19 days ago)
Parents
e2f03a6
updateBeforeLoadAndAfterCloseAreIgnored raced the editor load: the update only drops when state is still null, but on a fast thread the Dispatchers.Default load can complete first, the update then legitimately applies, and the load await timed out waiting for the overwritten title.

Gate the editor on a single occupied scope thread so the update provably happens before load, then release. Product behaviour unchanged.

Verified: TZ=UTC TodoEditorTest green.

Diff

todo/src/test/java/com/vayunmathur/todo/platform/TodoEditorTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/platform/TodoEditorTest.kt b/todo/src/test/java/com/vayunmathur/todo/platform/TodoEditorTest.ktindex 4dc261cac2..660d4d8d9f 100644--- a/todo/src/test/java/com/vayunmathur/todo/platform/TodoEditorTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/platform/TodoEditorTest.kt@@ -10,6 +10,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel+import kotlinx.coroutines.launch import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain@@ -129,13 +130,25 @@ class TodoEditorTest {      @Test fun updateBeforeLoadAndAfterCloseAreIgnored() = runBlocking {         repo.upsert(Todo(syncId = "a", title = "Stable"))-        val editor = editorFor(id = repo.getBySyncId("a")!!.id)+        // Gate the editor's scope on a single occupied thread so the load+        // cannot complete before the update below: on an unloaded box the+        // load (Dispatchers.Default) can win that race, the "too early"+        // update then legitimately applies, and the load await times out+        // waiting for a title that was correctly overwritten.+        val gate = java.util.concurrent.CountDownLatch(1)+        val gatedScope = kotlinx.coroutines.CoroutineScope(+            kotlinx.coroutines.SupervisorJob() + Dispatchers.Default.limitedParallelism(1)+        )+        gatedScope.launch { gate.await() }+        val editor = TodoEditor(repo, gatedScope, repo.getBySyncId("a")!!.id, null, Todo())         // state is still null here, so this update must be dropped, not crash.         editor.update { it.copy(title = "Too early") }+        gate.countDown()         await("load") { editor.state.value?.title == "Stable" }         editor.close()         editor.update { it.copy(title = "Too late") }         assertEquals("Stable", repo.getBySyncId("a")!!.title)+        gatedScope.cancel()     }      @Test fun closeFlushesPendingEditWithoutWaitingForDebounce() = runBlocking {