share: fix the main-looper deadlock and tile lifecycle in the coverage test
- Commit
955a6bbfad61e632bd8ae906898814f3ba2c3647- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:12 UTC (28 days ago)
- Parents
705c6e0
Two test-only fixes, no production change (ShareReceiveController.kt is untouched): - controller start stop and receive flag: setReceiveEnabled reconciles via withContext(Dispatchers.Main); called from a runBlocking on the Robolectric main thread this deadlocks — the paused main looper can never run the posted block while runBlocking occupies the thread — and hung the whole :share suite in CI. The suspend calls now run on a worker thread while this thread pumps the main looper until the latch releases. - tile service lifecycle: grant POST_NOTIFICATIONS so onClick takes the toggle path instead of the startActivityAndCollapse path whose null mService NPEs under Robolectric, and treat controller.destroy() as best-effort — Robolectric's ShadowTileService cannot cast to ShadowService inside TileService.onDestroy (framework bug, not our code). Verified: full :share:testDevUnitTest green on devserver (3m58s).
Diff
share/src/test/java/com/vayunmathur/share/platform/MoreCoverageTest.kt
diff --git a/share/src/test/java/com/vayunmathur/share/platform/MoreCoverageTest.kt b/share/src/test/java/com/vayunmathur/share/platform/MoreCoverageTest.ktindex c0448b142b..536e8a5b1d 100644--- a/share/src/test/java/com/vayunmathur/share/platform/MoreCoverageTest.kt+++ b/share/src/test/java/com/vayunmathur/share/platform/MoreCoverageTest.kt@@ -62,19 +62,41 @@ class MoreCoverageTest { } @Test- fun `controller start stop and receive flag`() = runBlocking {+ fun `controller start stop and receive flag`() { ShareReceiveController.start(app) ShareReceiveController.start(app) // idempotent ShareReceiveController.stop() ShareReceiveController.stop() ShareReceiveController.refreshLocalIdentity(app)- ShareReceiveController.setReceiveEnabled(app, false)- assertEquals(false, ShareReceiveController.isReceiveEnabled(app))+ // setReceiveEnabled reconciles the service via+ // withContext(Dispatchers.Main); calling it from a runBlocking on+ // the Robolectric main thread deadlocks (the paused main looper+ // can never run the posted block while runBlocking occupies the+ // thread — this hung the whole :share suite in CI). Run the+ // suspend calls on a worker thread and pump the main looper here.+ val done = java.util.concurrent.CountDownLatch(1)+ var failure: Throwable? = null+ Thread {+ runBlocking {+ try {+ ShareReceiveController.setReceiveEnabled(app, false)+ assertEquals(false, ShareReceiveController.isReceiveEnabled(app))+ } catch (t: Throwable) {+ failure = t+ } finally {+ done.countDown()+ }+ }+ }.start()+ while (!done.await(50, java.util.concurrent.TimeUnit.MILLISECONDS)) {+ Shadows.shadowOf(Looper.getMainLooper()).idle()+ }+ failure?.let { throw it } assertTrue(ShareReceiveController.connectionFor(app, 1L) == null) }- @Test fun `tile service lifecycle`() {+ Shadows.shadowOf(app).grantPermissions(android.Manifest.permission.POST_NOTIFICATIONS) val controller = Robolectric.buildService(ShareReceiveTileService::class.java) val service = controller.create().get() service.onTileAdded()@@ -82,7 +104,14 @@ class MoreCoverageTest { service.onClick() Thread.sleep(300) Shadows.shadowOf(Looper.getMainLooper()).idle()- controller.destroy()+ // Robolectric's ShadowTileService cannot cast to ShadowService on+ // destroy() (ClassCastException inside TileService.onDestroy) —+ // not our code. Best-effort destroy; the service under test+ // already ran its lifecycle above.+ try {+ controller.destroy()+ } catch (_: ClassCastException) {+ } } @Test