tests: settle launch work in its own test; drop a wall-clock alarm assertion
- Commit
455a4956d795f9bf344aa287554846006959af81- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-22 10:45 UTC (14 days ago)
- Parents
12f9640
CI failed 4 youpipe launch tests + 1 findfamily timing test. Youpipe's RootSlice3 harness destroyed the activity without draining its looper, so queued ViewModel/LaunchedEffect work executed inside LATER tests and was attributed there - the harness now idles the looper while alive and after destroy (looper-idle is the signal, no sleeps), assertions untouched. The findfamily test measured alarm delay from its own stale wall-clock read and waited on a 10s wall-clock deadline (the pattern proven to starve under CI CPU caps); it now asserts from production's own attempt stamp with the same bound, via a step-bounded pumped wait. Production untouched both cases. Honest caveat: CI contention doesn't reproduce on the 192-core box - these are code-evident couplings matching the CI pattern, locally green 3x per class and in full suites under taskset with no build cache.
Diff
findfamily/src/test/java/com/vayunmathur/findfamily/platform/FixCeilingAlarmTest.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/platform/FixCeilingAlarmTest.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/platform/FixCeilingAlarmTest.ktindex 4f430b4ae4..0ab6513154 100644--- a/findfamily/src/test/java/com/vayunmathur/findfamily/platform/FixCeilingAlarmTest.kt+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/platform/FixCeilingAlarmTest.kt@@ -76,17 +76,34 @@ class FixCeilingAlarmTest { private fun advance(ms: Long) = ShadowSystemClock.advanceBy(Duration.ofMillis(ms)) private fun now() = SystemClock.elapsedRealtime() - /** Lets background coroutines that hop onto the (paused) main looper finish. */+ /**+ * Lets background coroutines that hop onto the (paused) main looper finish.+ *+ * Step-bounded, never wall-clock-bounded: the previous+ * `System.currentTimeMillis() + 10_000` deadline burned its 10 s under+ * CI CPU contention (5-CPU cap, several test JVMs) before the+ * Dispatchers.IO producers it waits for got a slice, failing whichever+ * test happened to be waiting. Each step idles the paused looper (the+ * deterministic signal that queued main-thread work ran), then parks+ * briefly so IO producers get a slice; the bound is pumping steps.+ */ private fun pumpUntil(done: () -> Boolean) {- val deadline = System.currentTimeMillis() + 10_000L- while (!done()) {- check(System.currentTimeMillis() < deadline) { "timed out waiting for the service" }+ var steps = 0+ while (true) { shadowOf(Looper.getMainLooper()).idle()- Thread.sleep(5)+ if (done()) break+ java.util.concurrent.locks.LockSupport.parkNanos(+ java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(5L),+ )+ check(++steps < MAX_PUMP_STEPS) { "timed out waiting for the service (step-bounded)" } } shadowOf(Looper.getMainLooper()).idle() } + private companion object {+ const val MAX_PUMP_STEPS = 6_000+ }+ private fun await(job: Job?) { assertNotNull(job, "expected a forced fix to start") pumpUntil { job.isCompleted }@@ -219,13 +236,21 @@ class FixCeilingAlarmTest { fun inexactFallbackArmsEarlyEnoughToLandInsideTheCeiling() { ShadowAlarmManager.setCanScheduleExactAlarms(false) service.updateFixCeiling(true)- val checkedAt = now()- await(service.fixCeilingAlarmTick())+ val job = service.fixCeilingAlarmTick()+ // Measure from the attempt production actually armed against+ // (lastForcedFixAttemptMs, stamped synchronously by the tick), not+ // from a test-side clock read taken before the tick: any shadow-clock+ // movement between that read and the tick's attempt stamp (looper+ // drains, a heartbeat tick) inflates the apparent delay past the+ // exact bound even though production armed correctly, and under CI+ // load that made this assertion flaky. Same bound, correct origin.+ val attemptMs = assertNotNull(service.lastForcedFixAttemptMs, "the tick recorded an attempt")+ await(job) val alarm = assertNotNull(alarms.peekNextScheduledAlarm()) assertTrue(alarm.isAllowWhileIdle)- val delay = alarm.triggerAtMs - checkedAt+ val delay = alarm.triggerAtMs - attemptMs // Latest inexact delivery is 1.75x the delay after arming.- assertTrue(checkedAt + (delay * 1.75).toLong() <= checkedAt + ForcedFixPolicy.ACTIVE_CHECK_MAX_GAP_MS)+ assertTrue(attemptMs + (delay * 1.75).toLong() <= attemptMs + ForcedFixPolicy.ACTIVE_CHECK_MAX_GAP_MS) assertTrue(delay > 0L) }
youpipe/src/test/java/com/vayunmathur/youpipe/RootSlice3Pass2Test.kt
diff --git a/youpipe/src/test/java/com/vayunmathur/youpipe/RootSlice3Pass2Test.kt b/youpipe/src/test/java/com/vayunmathur/youpipe/RootSlice3Pass2Test.ktindex 017bbca792..63d1fb5143 100644--- a/youpipe/src/test/java/com/vayunmathur/youpipe/RootSlice3Pass2Test.kt+++ b/youpipe/src/test/java/com/vayunmathur/youpipe/RootSlice3Pass2Test.kt@@ -114,7 +114,20 @@ class RootSlice3Pass2Test { val controller = Robolectric.buildActivity(MainActivity::class.java, intent) val activity = controller.setup().get() assertTrue(!activity.isFinishing)+ // setup() returns with the composition, its LaunchedEffects and the+ // ViewModel's viewModelScope collectors still queued on Robolectric's+ // paused main looper. Destroying immediately races that queued work:+ // under CI load it (and any exception it raises, e.g. a VideoPage+ // load against the shared in-memory DB) then executes during a later+ // test's looper drain and is attributed to that test — which is why+ // the first launch in the class passed in CI while launches 2-5+ // failed. Drain deterministically instead: idle while the activity+ // is alive so its work settles here, then idle again after destroy+ // so teardown work is also attributed to this test. No sleeps: the+ // looper being idle is the signal, however long the box takes.+ org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() controller.pause().stop().destroy()+ org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() return activity }