Modern Apps git

← Modern-Apps.git

build-logic: add shared pumpUntil to testsupport DeterministicAwait

Commit
372e40eefb11fc1c3d0f7f6c97e0ae04ab8d2d8f
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:13 UTC (4 days ago)
Parents
589da91
The appstore unit tests carry 26 near-identical private copies of a
step-bounded pumped wait (an await wrapper, a pumpUntil body and a
pump-only companion of PUMP_STEP_MS/PARK_MS/MAX_PUMP_STEPS constants,
about 44 lines per class). This adds the one shared form of that wait
to DeterministicAwait.kt, beside awaitWithMainIdle, so the copies can
be deleted without changing what any test waits on.

pumpUntil pumps the paused main looper first and only then checks the
condition, parks 50 ms between steps so IO/Default producers get a CPU
slice, and fails after 6,000 steps with the same two-level
AssertionError the per-class wrappers produced ("timed out waiting
for: ..." caused by "timed out (step-bounded): ... after 6000
pumps"). The stepMs parameter preserves the two pumping primitives
the copies actually used: a positive value advances the looper clock
with idleFor(stepMs), and 0 idles without advancing the clock, for the
classes where driving delayed work early broke the terminal states
they wait on. The default is 200L, the most common copy.

Nothing calls the new function yet; it is purely additive, and every
module's unit-test source set already compiles this file via the two
convention plugins. Verified by the appstore conversion that follows
and by :appstore:testDevUnitTest staying green.

Diff

build-logic/shared-test/java/com/vayunmathur/testsupport/DeterministicAwait.kt
diff --git a/build-logic/shared-test/java/com/vayunmathur/testsupport/DeterministicAwait.kt b/build-logic/shared-test/java/com/vayunmathur/testsupport/DeterministicAwait.ktindex d11f709d29..9b0ffd7602 100644--- a/build-logic/shared-test/java/com/vayunmathur/testsupport/DeterministicAwait.kt+++ b/build-logic/shared-test/java/com/vayunmathur/testsupport/DeterministicAwait.kt@@ -27,6 +27,8 @@ import org.robolectric.Shadows  * - [awaitSemantics] is a Compose semantics wait on a terminal node state,  *   pumping the looper *and* the composition's test scheduler on every  *   check for the same reason (see its doc).+ * - [pumpUntil] is the step-bounded pumped wait the per-class copies used+ *   for ViewModel/DataStore terminal states, now shared verbatim.  *  * None of these sleep, burn CPU, or read `System.currentTimeMillis`.  */@@ -129,3 +131,54 @@ fun ComposeContentTestRule.awaitSemantics(description: String, condition: () ->  * point where virtual time itself becomes the thing being tested.  */ private const val SCHEDULER_ADVANCE_SECONDS = 60++/**+ * Step-bounded pumped wait on a terminal [condition]: every step pumps+ * the paused main looper (running the ViewModel/DataStore work queued+ * there, including delayed tasks), then parks briefly so+ * Dispatchers.IO/Default producers get a CPU slice on a contended box.+ * The bound is pumping steps, never wall-clock seconds that CI contention+ * can burn before any work runs. This is the shared form of the identical+ * `pumpUntil` copies that used to live in individual test classes.+ *+ * With `stepMs > 0` each step also advances the looper's clock by that+ * many milliseconds, running delayed work due in that window; classes+ * whose delayed work must not be driven early pass `stepMs = 0`, which+ * idles without advancing the clock. Pump first, then check: work that is+ * already queued still needs that one pump to settle before the condition+ * behind it can hold.+ */+fun pumpUntil(description: String, stepMs: Long = 200L, condition: () -> Boolean) {+    var steps = 0+    while (true) {+        // Pump first, then check: the loops this replaces always idled at+        // least once before judging the condition, and a condition that is+        // already true at entry can still need that one pump for the work+        // behind it to settle.+        val looper = Looper.getMainLooper()+        if (looper != null) {+            val shadow = Shadows.shadowOf(looper)+            if (stepMs > 0L) {+                shadow.idleFor(stepMs, java.util.concurrent.TimeUnit.MILLISECONDS)+            } else {+                shadow.idle()+            }+        }+        if (condition()) break+        java.util.concurrent.locks.LockSupport.parkNanos(+            java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(PUMP_PARK_MS),+        )+        if (++steps >= MAX_PUMP_STEPS) {+            throw AssertionError(+                "timed out waiting for: $description",+                AssertionError("timed out (step-bounded): $description after $MAX_PUMP_STEPS pumps"),+            )+        }+    }+}++/** Park between [pumpUntil] steps so IO/Default producers get a CPU slice. */+private const val PUMP_PARK_MS = 50L++/** Step bound for [pumpUntil]: steps are looper pumps, never wall-clock time. */+private const val MAX_PUMP_STEPS = 6_000