logviewer: de-flake shareUri test against non-blocking Robolectric pipes
- Commit
ad27f3998f640946818c7e57a3884de66eebea38- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:30 UTC (28 days ago)
- Parents
b1f29e4
The shareUri test failed deterministically in full-suite runs (4/4 red) while passing solo: the pipe read returned empty instead of the snapshot. Root cause is in the test environment, not the product. Robolectric's ShadowParcelFileDescriptor emulates pipes with a shared temp file, so unlike a real pipe a read does not block for the writer. BlobProvider serves share bytes through openPipeHelper, whose writer runs on an AsyncTask thread; under full-suite load the test's first read can land before the writer writes, catching the file still empty. Temporary product-side instrumentation confirmed the registry entry, gzip bytes, and writer were all correct (26/26 bytes written) while the reader still got empty. The test now reopens and rereads the uri until the expected snapshot arrives or a 10s deadline passes, keeping the same final assertion: a genuinely empty or wrong snapshot still fails at the deadline. Verified: :logviewer:testDevUnitTest --rerun-tasks green on two consecutive full-suite runs after 4/4 red runs before the change.
Diff
logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogViewerViewModelTest.kt
diff --git a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogViewerViewModelTest.kt b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogViewerViewModelTest.ktindex 1c7bbdafe0..1384f6704e 100644--- a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogViewerViewModelTest.kt+++ b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogViewerViewModelTest.kt@@ -201,9 +201,19 @@ class LogViewerViewModelTest { assertEquals("content", uri.scheme) assertEquals("${app.packageName}.BlobProvider", uri.authority) assertTrue(uri.toString().contains(vm.state.value.snapshotFileName.replace(" ", "%20")))- // The provider serves the snapshot bytes back, gzipped in between.- val text = app.contentResolver.openInputStream(uri)?.bufferedReader()?.use { it.readText() }- assertEquals("type: logcat\nfirst\nsecond\n", text)+ // The provider serves the snapshot bytes back, gzipped in between. Robolectric+ // emulates pipes with temp files, so unlike a real pipe a read does not block+ // for openPipeHelper's writer thread: a first read can catch the file still+ // empty. Reopen and reread until the snapshot arrives or a deadline passes.+ val expected = "type: logcat\nfirst\nsecond\n"+ val deadline = System.nanoTime() + 10_000_000_000L+ var text: String? = null+ while (System.nanoTime() < deadline) {+ text = app.contentResolver.openInputStream(uri)?.bufferedReader()?.use { it.readText() }+ if (text == expected) break+ Thread.sleep(5)+ }+ assertEquals(expected, text) } @Test