cast: batch unit tests; the TV receiver shell is its own bottleneck
- Commit
d644864c26342b1b75feb106e0de4b3816dd0881- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-21 23:01 UTC (14 days ago)
- Parents
8b2733d
Cast: 13 JNI-chain classes (FakeTv->Pqc pairing, MirrorClient sealing) and 6 polluters quarantined, 26 batch - 219/219 green, 47s -> 33s. Cast:tv: 6 native + 7 controller-singleton isolated, 20 batch - 141/141 green but no wall win: ReceiverShellTest alone takes ~291s per-class either way (the other 32 classes sum to ~25s), pre-existing intra-class slowness, flagged not churned. One sleep-poll converted to DeterministicAwait; two conversions were tried, failed on step bounds under parallel forks, and reverted byte-for-byte with the evidence recorded.
Diff
cast/build.gradle.kts
diff --git a/cast/build.gradle.kts b/cast/build.gradle.ktsindex 4eb00c9bd6..5315e26acc 100644--- a/cast/build.gradle.kts+++ b/cast/build.gradle.kts@@ -18,6 +18,64 @@ android { } } +// This module has no Rust of its own, but :cast:protocol (and the+// test-only dep below) reach :library:e2ee-p2p, which registers+// libe2ee_pqc (rustNativeLib), so every unit test here has kept+// forkEvery = 1. Only the classes below actually load that library:+// they construct FakeTv, whose init runs PqcIdentity.loadOrCreate over+// an empty in-memory store -> Pqc.generateKem/generateDsa ->+// PqcNative (NativeLoader.load("e2ee_pqc")), and/or pair a+// MirrorClient / CastController against it, which seals the session+// secret via SecretSealing.seal -> Pqc.encryptTo and opens it in+// FakeTv via SecretSealing.open -> PqcIdentity.decrypt. Tests that+// merely drive the pure protocol/codec helpers, compose with fake+// actions, or run MirrorClient against a closed port never load the+// library and stay in the main task. Quarantine the loaders and the+// shared-JVM polluters below into testDevUnitTestNative+// (forkEvery = 1); the remaining classes batch 20-per-JVM in+// testDevUnitTest, which depends on the quarantine task so the usual+// invocation still runs everything.+rustNativeTests {+ nativeClasses.addAll(+ listOf(+ "com.vayunmathur.cast.platform.CastControllerTest",+ "com.vayunmathur.cast.platform.ConnectionExtraTest",+ "com.vayunmathur.cast.platform.ControllerBranchesTest",+ "com.vayunmathur.cast.platform.DesktopAndCaptureTest",+ "com.vayunmathur.cast.platform.DesktopRenegotiateTest",+ "com.vayunmathur.cast.platform.MirrorClientExtraTest",+ "com.vayunmathur.cast.platform.MirrorClientTest",+ "com.vayunmathur.cast.platform.ProviderRouteTest",+ "com.vayunmathur.cast.platform.SessionEncoderBackedTest",+ "com.vayunmathur.cast.platform.TileIdleClickTest",+ "com.vayunmathur.cast.platform.mirror.EncoderBackedTest",+ "com.vayunmathur.cast.service.ContentCastIntegrationTest",+ "com.vayunmathur.cast.service.ContentStartedTest",+ )+ )+ // Shared-JVM polluters keep forkEvery = 1 in the same companion task:+ // CastViewModel turns CastController's flows into a viewModelScope+ // stateIn collector (SharingStarted.WhileSubscribed) that is never+ // cleared when the test never calls onCleared, and+ // MirrorPreferences reads/writes through the DataStoreUtils+ // process-wide singleton (its init eagerly collects the preferences+ // DataStore), so persisted DataStore state leaks into classes+ // expecting defaults. Audit rule: any test instantiating+ // CastViewModel or launching MainActivity (which builds one), or+ // using MirrorPreferences directly, belongs here, unless it already+ // loads the native library above. All other classes batch.+ isolatedClasses.addAll(+ listOf(+ "com.vayunmathur.cast.platform.ActivitiesTest",+ "com.vayunmathur.cast.platform.CastViewModelTest",+ "com.vayunmathur.cast.platform.RemoteDisplayAndTileTest",+ "com.vayunmathur.cast.platform.mirror.MirrorPreferencesEmptyTest",+ "com.vayunmathur.cast.platform.mirror.MirrorPreferencesTest",+ "com.vayunmathur.cast.ui.CastComposeTest",+ )+ )+}+ dependencies { // Compile-only stubs for `com.android.media.remotedisplay`, the remote display provider API. // A shared library on the system image, pulled in at runtime by the `<uses-library>` tag;
cast/src/test/java/com/vayunmathur/cast/platform/CastViewModelTest.kt
diff --git a/cast/src/test/java/com/vayunmathur/cast/platform/CastViewModelTest.kt b/cast/src/test/java/com/vayunmathur/cast/platform/CastViewModelTest.ktindex 4daf82526b..fe2a4cc27a 100644--- a/cast/src/test/java/com/vayunmathur/cast/platform/CastViewModelTest.kt+++ b/cast/src/test/java/com/vayunmathur/cast/platform/CastViewModelTest.kt@@ -10,6 +10,7 @@ import com.vayunmathur.cast.domain.ClientFailure import com.vayunmathur.cast.domain.ClientPhase import com.vayunmathur.cast.domain.ClientState import com.vayunmathur.cast.platform.mirror.MirrorDegradation+import com.vayunmathur.testsupport.awaitWithMainIdle import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertTrue@@ -44,14 +45,13 @@ class CastViewModelTest { private fun awaitState(vm: CastViewModel, condition: (CastUiState) -> Boolean): CastUiState { collectors += kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.IO) .launch { vm.uiState.collect { } }- val deadline = System.currentTimeMillis() + 8_000- while (System.currentTimeMillis() < deadline) {- Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()- val state = vm.uiState.value- if (condition(state)) return state- Thread.sleep(20)+ // Producers run on IO (the collector above, CastController's+ // scope), so the step bound must leave them wall time to land:+ // the clock precedent uses 100k steps for background waits.+ awaitWithMainIdle("uiState matching condition", maxSteps = 200_000) {+ condition(vm.uiState.value) }- throw AssertionError("uiState never matched; last=${vm.uiState.value}")+ return vm.uiState.value } @Test
cast/tv/build.gradle.kts
diff --git a/cast/tv/build.gradle.kts b/cast/tv/build.gradle.ktsindex cc74ea3847..c594f83ce8 100644--- a/cast/tv/build.gradle.kts+++ b/cast/tv/build.gradle.kts@@ -29,6 +29,57 @@ android { } } +// This module has no Rust of its own, but :library:e2ee-p2p (below)+// registers libe2ee_pqc (rustNativeLib), so every unit test here has+// kept forkEvery = 1. Only the classes below actually load that+// library: they call ReceiverController.start (whose listen() runs+// PqcIdentity.loadOrCreate over PairingStore -> Pqc.generateKem /+// generateDsa -> PqcNative, NativeLoader.load("e2ee_pqc")), seal a+// session secret directly via SecretSealing.seal -> Pqc.encryptTo, or+// drive ReceiverService.onStartCommand / MainActivity, which start+// the controller the same way. Tests that merely call the pure+// pairing/session helpers, compose TV surfaces, or drive decoders+// with fake codecs never load the library and stay in the main task.+// Quarantine the loaders and the shared-JVM polluters below into+// testDevUnitTestNative (forkEvery = 1); the remaining classes batch+// 20-per-JVM in testDevUnitTest, which depends on the quarantine task+// so the usual invocation still runs everything.+rustNativeTests {+ nativeClasses.addAll(+ listOf(+ "com.vayunmathur.cast.tv.ReceiverShellTest",+ "com.vayunmathur.cast.tv.ServiceNotificationsTest",+ "com.vayunmathur.cast.tv.platform.ContentSessionSuccessTest",+ "com.vayunmathur.cast.tv.platform.ControlChannelDeepTest",+ "com.vayunmathur.cast.tv.platform.FullSessionTest",+ "com.vayunmathur.cast.tv.service.ServiceCollectTest",+ )+ )+ // Shared-JVM polluters keep forkEvery = 1 in the same companion task:+ // ReceiverController is a process-wide object (state StateFlow,+ // serverSocket, session/media jobs, surface/contentPlayer fields)+ // whose session state one class leaves behind is the next class's+ // starting phase, and PairingStore persists through the+ // DataStoreUtils process-wide singleton (its init eagerly collects+ // the preferences DataStore), so remembered-device keys and the+ // friendly name leak into classes expecting defaults. Audit rule:+ // any test calling ReceiverController.authenticate / serveContent /+ // startStreaming / pump, mutating its state or fields, or using+ // PairingStore / DataStoreUtils directly belongs here, unless it+ // already loads the native library above. All other classes batch.+ isolatedClasses.addAll(+ listOf(+ "com.vayunmathur.cast.tv.platform.AuthenticateDeepTest",+ "com.vayunmathur.cast.tv.platform.AuthenticateTest",+ "com.vayunmathur.cast.tv.platform.FinalSmallTest",+ "com.vayunmathur.cast.tv.platform.PlatformTvTest",+ "com.vayunmathur.cast.tv.platform.ServeContentDeepTest",+ "com.vayunmathur.cast.tv.platform.StartStreamingDeepTest",+ "com.vayunmathur.cast.tv.platform.StreamingPumpDeepTest",+ )+ )+}+ dependencies { // The wire format, shared verbatim with the phone. Nothing in this app re-implements it. implementation(project(":cast:protocol"))