Is it possible that this gets stuck in `runBlockin...
# coroutines
f
Is it possible that this gets stuck in
runBlockingTest
because
TICK_DELAY
is skipped so the CPU doesn't have time to execute something else? Normally, I leave this loop by collecting the value in another place and cancelling the coroutine around it.
Copy code
while (true) {
                    delay(TICK_DELAY)
                    val totalElapsedTime = systemClockProvider.getElapsedRealTime() - startTime
                    tasksDataSource.increaseMillisCompletedToday(
                        selectedTask.id,
                        totalElapsedTime - lastReportedTime
                    )
                    lastReportedTime = totalElapsedTime
                }
j
Skipping delays shouldn't be the cause of it, because it should still act as a suspension point that yields the thread to other coroutines. Maybe you could share more of the surrounding code? It would help to see how the other coroutines are organized
f
@Joffrey
startTimer
starts the timer and the coroutine in the
init
block observes and stops it. But in my tests, I get stuck in an infinite
while
loop:
Copy code
init {
    applicationScope.launch {
        combine(selectedTask, activeDay) { selectedTask, activeDay ->
            Pair(selectedTask, activeDay)
        }.collect { (selectedTask, activeDay) ->
            if (selectedTask == null || activeDay == null ||
                !selectedTask.isActiveOnDay(activeDay)
            ) {
                stopTimer()
            } else if (selectedTask.dailyGoalCompleted && timerRunning.first()) {
                // Task has just finished
                stopTimer()
                shaveOffOvershootTime(selectedTask)
                notificationHelper.showTaskCompletedNotification(selectedTask)
                stopTimer()
            }
        }
    }
}

fun startTimer() {
    timerJob?.cancel()
    timerJob = applicationScope.launch {
        val selectedTask = selectedTask.first()
        if (selectedTask != null) {
            tasksDataSource.updateLastActiveTimestamp(selectedTask.id, System.currentTimeMillis())
            startTimerService()
            timerRunningFlow.value = true

            val startTime = SystemClock.elapsedRealtime()
            var lastReportedTime = 0L
            while (true) {
                delay(TICK_DELAY)
                val totalElapsedTime = SystemClock.elapsedRealtime() - startTime
                tasksDataSource.increaseMillisCompletedToday(selectedTask.id, totalElapsedTime - lastReportedTime)
                lastReportedTime = totalElapsedTime
            }
        }
    }
}
e
using SystemClock definitely isn't gonna give you values that match the test dispatcher's virtual time
f
I replaced it for a fake class but it's still stuck in the loop