measure: write preferences on IO, unhang Robolectric class (#tests)
- Commit
deed149a4c18af990d5acdbe28f57f5e25a6dcdb- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-15 00:06 UTC (21 days ago)
- Parents
ba090f4
MeasureViewModelTest.preferencesLoadIntoEveryState hung forever in a full-class run (deadlocked sandbox main thread; single-test runs passed). Thread dumps showed the main thread parked in the test's runBlocking with no IO thread progressing: a previous test (settingsActions) leaves a DataStore edit suspended on the main dispatcher, Robolectric discards that queue between tests, and the store's write lock is then never released, so every later edit blocks. Product fix: MeasureViewModel's DataStore writes (persistBoolean, calibrateZero, clearCalibration) now run on Dispatchers.IO instead of viewModelScope's Main, so an edit can never strand the store when Main stops pumping. Test fix: the test writes its seed preferences from an IO launch with a latch (pumping the looper while waiting) and awaits the IO-mirrored snapshot before loading, instead of runBlocking on the main thread. Verified: settings+preferences pair and the full MeasureViewModelTest class (8/8) green.
Diff
measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt
diff --git a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.ktindex 8a01325a51..4936a0d01a 100644--- a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt+++ b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt@@ -17,6 +17,7 @@ import com.vayunmathur.measure.platform.sensor.ImuRecorder import com.vayunmathur.measure.platform.sensor.TiltSensor import com.vayunmathur.measure.domain.polygonArea import com.vayunmathur.measure.domain.polygonPerimeter+import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch@@ -94,7 +95,11 @@ class MeasureViewModel(app: Application) : AndroidViewModel(app), val (pitch, roll, edge) = tiltSensor.captureZero() levelState.value = levelState.value.copy(isCalibrated = true) settingsState.value = settingsState.value.copy(levelCalibrated = true)- viewModelScope.launch {+ // IO, not Main: a DataStore edit suspended on the main+ // dispatcher can strand the store's write lock if that+ // dispatcher stops being pumped (Robolectric resets it+ // between tests, wedging every later edit in the process).+ viewModelScope.launch(Dispatchers.IO) { ds.setDouble(KEY_LEVEL_PITCH, pitch) ds.setDouble(KEY_LEVEL_ROLL, roll) ds.setDouble(KEY_LEVEL_EDGE, edge)@@ -105,7 +110,7 @@ class MeasureViewModel(app: Application) : AndroidViewModel(app), tiltSensor.clearCalibration() levelState.value = levelState.value.copy(isCalibrated = false) settingsState.value = settingsState.value.copy(levelCalibrated = false)- viewModelScope.launch {+ viewModelScope.launch(Dispatchers.IO) { // Remove the keys rather than writing NaN sentinels: a stored // NaN reads back as a non-null Double, so the loader treated // the level as calibrated forever and subtracted NaN offsets@@ -224,7 +229,9 @@ class MeasureViewModel(app: Application) : AndroidViewModel(app), // ---- DiagnosticsActions ---- private fun persistBoolean(key: String, value: Boolean) {- viewModelScope.launch { ds.setBoolean(key, value) }+ // IO for the same reason as calibrateZero: never hold a+ // DataStore edit on the main dispatcher.+ viewModelScope.launch(Dispatchers.IO) { ds.setBoolean(key, value) } } override fun onCleared() {
measure/src/test/java/com/vayunmathur/measure/platform/MeasureViewModelTest.kt
diff --git a/measure/src/test/java/com/vayunmathur/measure/platform/MeasureViewModelTest.kt b/measure/src/test/java/com/vayunmathur/measure/platform/MeasureViewModelTest.ktindex 4fb148f90f..29902a3794 100644--- a/measure/src/test/java/com/vayunmathur/measure/platform/MeasureViewModelTest.kt+++ b/measure/src/test/java/com/vayunmathur/measure/platform/MeasureViewModelTest.kt@@ -15,6 +15,7 @@ import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue+import kotlinx.coroutines.launch import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf@@ -197,12 +198,32 @@ class MeasureViewModelTest { // fresh load applies them all at once. run { val store = vm.ds- kotlinx.coroutines.runBlocking {+ // Write off the main thread and pump while waiting:+ // runBlocking here parks the Robolectric main thread+ // forever once a previous test (settingsActions...) has+ // left a DataStore edit suspended on the main dispatcher+ // - Robolectric discards that queue between tests, the+ // store's write lock is never released, and every later+ // edit blocks with no IO thread progressing (the class+ // run's deadlocked sandbox main thread). The ViewModel+ // now writes on IO; this test does the same.+ val written = java.util.concurrent.CountDownLatch(1)+ kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.IO).launch { store.setBoolean(KEY_IMPERIAL, true) store.setBoolean(KEY_FRACTIONAL, false) store.setBoolean(KEY_DIAGNOSTICS, true)+ written.countDown() }+ val writeDeadline = System.currentTimeMillis() + 15_000+ while (written.count > 0 && System.currentTimeMillis() < writeDeadline) {+ pump()+ Thread.sleep(10)+ }+ assertTrue(written.count == 0L, "timed out writing preferences") }+ // The synchronous getters read the IO-mirrored snapshot; wait+ // for the mirror to catch up before loading.+ await("preferences mirrored") { vm.ds.getBoolean(KEY_IMPERIAL, false) } vm.loadPreferencesImpl() assertEquals(UnitSystem.Imperial, vm.settings.value.unitSystem) assertFalse(vm.settings.value.useFractionalInches)
measure/src/test/java/com/vayunmathur/measure/ui/MeasurePagesTest.kt
diff --git a/measure/src/test/java/com/vayunmathur/measure/ui/MeasurePagesTest.kt b/measure/src/test/java/com/vayunmathur/measure/ui/MeasurePagesTest.ktindex d062a797f5..a246169833 100644--- a/measure/src/test/java/com/vayunmathur/measure/ui/MeasurePagesTest.kt+++ b/measure/src/test/java/com/vayunmathur/measure/ui/MeasurePagesTest.kt@@ -2,9 +2,12 @@ package com.vayunmathur.measure.ui import android.app.Application import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.hasClickAction+import androidx.compose.ui.test.hasText import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick+import androidx.compose.ui.test.performScrollTo import androidx.test.core.app.ApplicationProvider import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.measure.R@@ -36,11 +39,13 @@ class MeasurePagesTest { val vm = MeasureViewModel(app) val backStack = stack(Route.Compass) rule.setContent { CompassPage(backStack, vm) }- rule.onNodeWithText(str(R.string.tool_level)).performClick()+ // The app-bar title also reads "Compass", so match the+ // clickable bottom-bar tab, not just the text.+ rule.onNode(hasText(str(R.string.tool_level)) and hasClickAction()).performClick() assertEquals(Route.Level, backStack.last())- rule.onNodeWithText(str(R.string.tool_ruler)).performClick()+ rule.onNode(hasText(str(R.string.tool_ruler)) and hasClickAction()).performClick() assertEquals(Route.Ruler, backStack.last())- rule.onNodeWithText(str(R.string.tool_compass)).performClick()+ rule.onNode(hasText(str(R.string.tool_compass)) and hasClickAction()).performClick() assertEquals(Route.Compass, backStack.last()) } @@ -75,7 +80,9 @@ class MeasurePagesTest { // in state, then the row appears and navigates. vm.setShowDiagnostics(true) rule.waitForIdle()- rule.onNodeWithText(str(R.string.settings_open_diagnostics)).performClick()+ // The row sits at the bottom of a scrollable column; scroll+ // it into view or the click lands outside the window.+ rule.onNodeWithText(str(R.string.settings_open_diagnostics)).performScrollTo().performClick() assertEquals(Route.Diagnostics, backStack.last()) }