Modern Apps git

← Modern-Apps.git

email: await draft saves and pump the main looper in EML viewer tests (523 tests)

Commit
1c63b614d0189a0d1c4e824451d287cf6778efc0
Author
Vayun Mathur <[email protected]>
When
2026-11-15 19:17 UTC (20 days ago)
Parents
9444a45
Two CI-only failures:

- 'save with an id updates the existing draft' reported 'expected 1
  was -1': the test polled a callback flag for 200 x 10ms while the
  save ran Room work on real threads; under CI instrumentation the
  2s budget elapsed first. Await the save callback with a
  CompletableDeferred (30s timeout) in all three draft tests, and
  wait for the deleted row to actually disappear instead of sleeping
  a fixed budget. Assertions unchanged.

- 'parseable eml renders subject sender body and attachment' hit a
  compose timeout: EmlViewerScreen parses on Dispatchers.IO and the
  result resumes on Dispatchers.Main, which Robolectric's paused
  looper only runs when idled; the compose rule's waitUntil does not
  pump it (the same trap ComposerSendTest documents). Wait for the
  rendered text with a helper that idles the main looper per poll,
  in all four tests of EmlViewerSuccessTest.

Verified green 3x under TZ=UTC and TZ=America/New_York.

Diff

email/src/test/java/com/vayunmathur/email/draft/EmailDraftActionsTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/draft/EmailDraftActionsTest.kt b/email/src/test/java/com/vayunmathur/email/draft/EmailDraftActionsTest.ktindex 5793ec818d..f3eced7c69 100644--- a/email/src/test/java/com/vayunmathur/email/draft/EmailDraftActionsTest.kt+++ b/email/src/test/java/com/vayunmathur/email/draft/EmailDraftActionsTest.kt@@ -5,9 +5,11 @@ import androidx.room3.Room import androidx.test.core.app.ApplicationProvider import com.vayunmathur.email.data.EmailDatabase import com.vayunmathur.email.platform.EmailDraftActions+import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.withTimeout import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test@@ -46,17 +48,15 @@ class EmailDraftActionsTest {      @Test     fun `save inserts a new draft and reports its id`() = runBlocking {-        var savedId = -1L+        // The save runs on the actions' scope; await its callback directly+        // rather than polling a flag on a wall-clock budget.+        val saved = CompletableDeferred<Long>()         actions.save(             id = null, accountEmail = "[email protected]", to = "[email protected]",             cc = "", bcc = "", subject = "Hi", body = "hello",-            onSaved = { savedId = it },+            onSaved = { saved.complete(it) },         )-        // The save runs on the actions' scope; wait for the callback.-        repeat(200) {-            if (savedId != -1L) return@repeat-            Thread.sleep(10)-        }+        val savedId = withTimeout(30_000) { saved.await() }         val loaded = actions.load(savedId)         assertEquals("[email protected]", loaded?.to)         assertEquals("Hi", loaded?.subject)@@ -65,26 +65,20 @@ class EmailDraftActionsTest {      @Test     fun `save with an id updates the existing draft`() = runBlocking {-        var firstId = -1L+        val first = CompletableDeferred<Long>()         actions.save(             id = null, accountEmail = "[email protected]", to = "[email protected]",             cc = "", bcc = "", subject = "v1", body = "b1",-            onSaved = { firstId = it },+            onSaved = { first.complete(it) },         )-        repeat(200) {-            if (firstId != -1L) return@repeat-            Thread.sleep(10)-        }-        var secondId = -2L+        val firstId = withTimeout(30_000) { first.await() }+        val second = CompletableDeferred<Long>()         actions.save(             id = firstId, accountEmail = "[email protected]", to = "[email protected]",             cc = "", bcc = "", subject = "v2", body = "b2",-            onSaved = { secondId = it },+            onSaved = { second.complete(it) },         )-        repeat(200) {-            if (secondId != -2L) return@repeat-            Thread.sleep(10)-        }+        val secondId = withTimeout(30_000) { second.await() }         assertEquals(firstId, secondId)         val loaded = actions.load(firstId)         assertEquals("[email protected]", loaded?.to)@@ -93,20 +87,20 @@ class EmailDraftActionsTest {      @Test     fun `delete removes the draft`() = runBlocking {-        var savedId = -1L+        val saved = CompletableDeferred<Long>()         actions.save(             id = null, accountEmail = "[email protected]", to = "[email protected]",             cc = "", bcc = "", subject = "bye", body = "b",-            onSaved = { savedId = it },+            onSaved = { saved.complete(it) },         )-        repeat(200) {-            if (savedId != -1L) return@repeat-            Thread.sleep(10)-        }+        val savedId = withTimeout(30_000) { saved.await() }         actions.delete(savedId)-        repeat(200) {-            if (actions.load(savedId) == null && savedId != -1L) return@repeat-            Thread.sleep(10)+        // Delete is fire-and-forget on the actions' scope: wait until the row+        // is actually gone instead of sleeping a fixed budget.+        withTimeout(30_000) {+            while (actions.load(savedId) != null) {+                kotlinx.coroutines.delay(10)+            }         }         assertNull(actions.load(savedId))     }
email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt b/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.ktindex 06c0ae9fa4..ff455372c0 100644--- a/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt+++ b/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt@@ -37,6 +37,25 @@ class EmlViewerSuccessTest {     @get:Rule     val composeRule = createComposeRule() ++    /**+     * Waits for [text] to appear, idling Robolectric's paused main looper so+     * the IO parse's Main-thread resume can actually land: the compose+     * rule's waitUntil does not pump that looper, so under load the parse+     * result sat queued past the wait (see ComposerSendTest).+     */+    private fun awaitText(text: String, substring: Boolean = false, unmerged: Boolean = false) {+        val deadline = System.currentTimeMillis() + 15_000+        while (System.currentTimeMillis() < deadline) {+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()+            if (composeRule.onAllNodesWithText(text, substring = substring, useUnmergedTree = unmerged)+                    .fetchSemanticsNodes().isNotEmpty()+            ) return+            Thread.sleep(10)+        }+        throw AssertionError("timed out waiting for text: $text")+    }+     private fun writeEml(name: String, content: String): File {         val context = ApplicationProvider.getApplicationContext<Application>()         return File(context.cacheDir, name).apply { writeText(content) }@@ -72,10 +91,7 @@ class EmlViewerSuccessTest {         // The scaffold title flips to the subject when the parse lands, a         // frame before the loading flag clears and the detail column         // composes: wait for a detail row, not just the title.-        composeRule.waitUntil(15_000) {-            composeRule.onAllNodesWithText("Dana Sender", useUnmergedTree = true)-                .fetchSemanticsNodes().isNotEmpty()-        }+        awaitText("Dana Sender", unmerged = true)         // The subject renders as scaffold title (and again as the body         // headline below the fold); assert it landed at all, plus the         // detail rows in the unmerged tree below.@@ -120,10 +136,7 @@ class EmlViewerSuccessTest {         composeRule.setContent {             EmlViewerScreen(uriString = Uri.fromFile(file).toString(), onBack = {})         }-        composeRule.waitUntil(15_000) {-            composeRule.onAllNodesWithText("Open", useUnmergedTree = true)-                .fetchSemanticsNodes().isNotEmpty()-        }+        awaitText("Open", unmerged = true)         composeRule.onNodeWithText("Open", useUnmergedTree = true).performClick()         composeRule.waitForIdle()         val context = ApplicationProvider.getApplicationContext<Application>()@@ -150,10 +163,7 @@ class EmlViewerSuccessTest {         composeRule.setContent {             EmlViewerScreen(uriString = Uri.fromFile(file).toString(), onBack = {})         }-        composeRule.waitUntil(15_000) {-            composeRule.onAllNodesWithText("Remote images blocked", substring = true)-                .fetchSemanticsNodes().isNotEmpty()-        }+        awaitText("Remote images blocked", substring = true)         composeRule.onNodeWithText("Remote images blocked", substring = true).assertExists()         composeRule.onNodeWithText("Load images").performClick()         composeRule.waitForIdle()@@ -180,10 +190,7 @@ class EmlViewerSuccessTest {             root = LocalView.current             EmlViewerScreen(uriString = Uri.fromFile(file).toString(), onBack = {})         }-        composeRule.waitUntil(15_000) {-            composeRule.onAllNodesWithText("Remote images blocked", substring = true)-                .fetchSemanticsNodes().isNotEmpty()-        }+        awaitText("Remote images blocked", substring = true)         composeRule.waitForIdle()         val webViews = mutableListOf<WebView>()         val q = ArrayDeque<View>().apply { add(assertNotNull(root).rootView) }