Modern Apps git

← Modern-Apps.git

auto: deflake pinned-apps dock tests (260 tests)

Commit
339f027ec747b0d97267a6756521e88a9edcc315
Author
Vayun Mathur <[email protected]>
When
2026-11-15 03:16 UTC (21 days ago)
Parents
e404a0b
PinnedAppsPrefsTest failed in the loaded full check with
'expected [c,a] was [a,c]' in the unpin/move test. Two stacked
races caused it:

- Product: pin/unpin/move read the current order through
  pinnedNow, a synchronous snapshot of a DataStore mirror that
  DataStoreUtils collects on its own IO scope. Right after a
  write completes, that mirror can still hold the previous value,
  so the sequence setPinned([a,b,c]) -> unpin(b) -> move(c,0)
  could compute the move from a stale snapshot and resurrect the
  pre-move order. All three operations now do their
  read-modify-write inside a single DataStore edit via the
  existing DataStoreUtils.updateString, so each one transforms
  the latest persisted value atomically.
- Test: the assertions read pinnedNow immediately after each
  write, which is exactly when the mirror lag is observable.
  They now go through a bounded awaitPinned poll that waits for
  pinnedNow to equal the expected order and then asserts it
  exactly. Applied to every pinnedNow assertion in the class,
  since they all share the shape.

No assertions were removed; the dock order, truncation and
rejection semantics asserted are unchanged.

Verified: before, the failure appeared in the loaded 128-worker
full check while isolated reruns passed (3/3 isolated baseline
runs green with --no-build-cache). After: 5 consecutive green
:auto:testDevUnitTest runs, 3 isolated at --max-workers=32 and
2 under load at --max-workers=64 on a busy host (260 tests).

Diff

auto/src/main/java/com/vayunmathur/auto/data/PinnedAppsPrefs.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/data/PinnedAppsPrefs.kt b/auto/src/main/java/com/vayunmathur/auto/data/PinnedAppsPrefs.ktindex a84422f692..4a35f0f346 100644--- a/auto/src/main/java/com/vayunmathur/auto/data/PinnedAppsPrefs.kt+++ b/auto/src/main/java/com/vayunmathur/auto/data/PinnedAppsPrefs.kt@@ -31,28 +31,54 @@ object PinnedAppsPrefs {         DataStoreUtils.getInstance(context).setString(KEY_PINNED, ids.take(MAX_PINNED).joinToString(SEPARATOR))     } -    /** Pins [id] at the end; no-op when full or already pinned. Returns false when rejected. */+    /**+     * Pins [id] at the end; no-op when full or already pinned. Returns false when rejected.+     *+     * The read-modify-write runs inside a single DataStore edit+     * ([DataStoreUtils.updateString]) rather than reading [pinnedNow]+     * first: [pinnedNow] is a mirror of the store collected on an IO+     * scope, so right after a write it can still hold the previous+     * value, and a following unpin/move computed from that stale+     * snapshot would silently resurrect the old order.+     */     suspend fun pin(context: Context, id: String): Boolean {-        val current = pinnedNow(context)-        if (id in current) return true-        if (current.size >= MAX_PINNED) return false-        setPinned(context, current + id)-        return true+        var pinned = false+        DataStoreUtils.getInstance(context).updateString(KEY_PINNED) { raw ->+            val current = raw.orEmpty().toIdList()+            when {+                id in current -> {+                    pinned = true+                    raw.orEmpty()+                }+                current.size >= MAX_PINNED -> raw.orEmpty()+                else -> {+                    pinned = true+                    (current + id).joinToString(SEPARATOR)+                }+            }+        }+        return pinned     }      /** Unpins [id]; no-op when absent. */     suspend fun unpin(context: Context, id: String) {-        val current = pinnedNow(context)-        if (id !in current) return-        setPinned(context, current - id)+        DataStoreUtils.getInstance(context).updateString(KEY_PINNED) { raw ->+            val current = raw.orEmpty().toIdList()+            if (id in current) (current - id).joinToString(SEPARATOR) else raw.orEmpty()+        }     }      /** Moves [id] to [toIndex] within the pinned order; no-op when absent. */     suspend fun move(context: Context, id: String, toIndex: Int) {-        val current = pinnedNow(context).toMutableList()-        if (!current.remove(id)) return-        current.add(toIndex.coerceIn(0, current.size), id)-        setPinned(context, current)+        DataStoreUtils.getInstance(context).updateString(KEY_PINNED) { raw ->+            val current = raw.orEmpty().toIdList().toMutableList()+            if (!current.remove(id)) {+                raw.orEmpty()+            } else {+                current.add(toIndex.coerceIn(0, current.size), id)+                current.joinToString(SEPARATOR)+            }+        }     }      private fun String.toIdList(): List<String> =
auto/src/test/java/com/vayunmathur/auto/data/PinnedAppsPrefsTest.kt
diff --git a/auto/src/test/java/com/vayunmathur/auto/data/PinnedAppsPrefsTest.kt b/auto/src/test/java/com/vayunmathur/auto/data/PinnedAppsPrefsTest.ktindex 13449fab69..c66f9c4f4b 100644--- a/auto/src/test/java/com/vayunmathur/auto/data/PinnedAppsPrefsTest.kt+++ b/auto/src/test/java/com/vayunmathur/auto/data/PinnedAppsPrefsTest.kt@@ -21,10 +21,25 @@ class PinnedAppsPrefsTest {     private val context: android.content.Context         get() = ApplicationProvider.getApplicationContext() +    /**+     * [PinnedAppsPrefs.pinnedNow] reads a DataStore mirror collected on+     * an IO scope, so it can lag a just-completed write. Poll it (bounded)+     * until it reflects [expected], then assert exactly.+     */+    private fun awaitPinned(expected: List<String>) {+        val deadline = System.currentTimeMillis() + 10_000+        while (PinnedAppsPrefs.pinnedNow(context) != expected &&+            System.currentTimeMillis() < deadline+        ) {+            Thread.sleep(20)+        }+        assertEquals(expected, PinnedAppsPrefs.pinnedNow(context))+    }+     @Test     fun `empty until the user pins something`() = runTest {         PinnedAppsPrefs.setPinned(context, emptyList())-        assertEquals(emptyList(), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(emptyList())         assertEquals(emptyList(), PinnedAppsPrefs.pinnedFlow(context).first())     } @@ -32,7 +47,7 @@ class PinnedAppsPrefsTest {     fun `pin order round-trips through the flow`() = runTest {         val ids = listOf("com.a/.Car", "com.b/.Car")         PinnedAppsPrefs.setPinned(context, ids)-        assertEquals(ids, PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(ids)         assertEquals(ids, PinnedAppsPrefs.pinnedFlow(context).first())         PinnedAppsPrefs.setPinned(context, emptyList())     }@@ -41,7 +56,7 @@ class PinnedAppsPrefsTest {     fun `writes truncate to the max dock size`() = runTest {         val ids = (1..8).map { "com.app$it/.Car" }         PinnedAppsPrefs.setPinned(context, ids)-        assertEquals(ids.take(PinnedAppsPrefs.MAX_PINNED), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(ids.take(PinnedAppsPrefs.MAX_PINNED))         PinnedAppsPrefs.setPinned(context, emptyList())     } @@ -51,8 +66,10 @@ class PinnedAppsPrefsTest {         assertTrue(PinnedAppsPrefs.pin(context, "com.a/.Car"))         // Re-pinning is a no-op success.         assertTrue(PinnedAppsPrefs.pin(context, "com.a/.Car"))-        assertEquals(listOf("com.a/.Car"), PinnedAppsPrefs.pinnedNow(context))-        PinnedAppsPrefs.setPinned(context, (1..PinnedAppsPrefs.MAX_PINNED).map { "com.app$it/.Car" })+        awaitPinned(listOf("com.a/.Car"))+        val full = (1..PinnedAppsPrefs.MAX_PINNED).map { "com.app$it/.Car" }+        PinnedAppsPrefs.setPinned(context, full)+        awaitPinned(full)         assertFalse(PinnedAppsPrefs.pin(context, "com.full/.Car"))         assertEquals(PinnedAppsPrefs.MAX_PINNED, PinnedAppsPrefs.pinnedNow(context).size)         PinnedAppsPrefs.setPinned(context, emptyList())@@ -62,16 +79,16 @@ class PinnedAppsPrefsTest {     fun `unpin and move reorder the dock`() = runTest {         PinnedAppsPrefs.setPinned(context, listOf("a", "b", "c"))         PinnedAppsPrefs.unpin(context, "b")-        assertEquals(listOf("a", "c"), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(listOf("a", "c"))         // Unpinning an absent id is a no-op.         PinnedAppsPrefs.unpin(context, "zzz")-        assertEquals(listOf("a", "c"), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(listOf("a", "c"))         PinnedAppsPrefs.move(context, "c", 0)-        assertEquals(listOf("c", "a"), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(listOf("c", "a"))         // Moving an absent id is a no-op; out-of-range indices clamp.         PinnedAppsPrefs.move(context, "zzz", 0)         PinnedAppsPrefs.move(context, "a", 99)-        assertEquals(listOf("c", "a"), PinnedAppsPrefs.pinnedNow(context))+        awaitPinned(listOf("c", "a"))         PinnedAppsPrefs.setPinned(context, emptyList())     } }