Status Update
Comments
sa...@google.com <sa...@google.com> #2
Hi, are you able to step through and see which line it is hanging on? Also, what are you specifically trying to test here?
ji...@gmail.com <ji...@gmail.com> #3
Hi, are you able to step through and see which line it is hanging on?
It hangs on the following line:
lifecycleOwner.currentState = Lifecycle.State.RESUMED
Specifically, it hangs when currentState
's setter calls runBlocking
. runBlocking
is not safe to call from a coroutine.
Also, what are you specifically trying to test here?
Sorry, my naming wasn't clear. This might make it more clear:
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalStdlibApi::class)
class TestLifecycleOwnerTest {
@Test
fun canAssignToCurrentStateInRunTest() = runTest(dispatchTimeoutMs = 5000) {
Dispatchers.setMain(coroutineContext[CoroutineDispatcher]!!)
val lifecycleOwner = TestLifecycleOwner()
lifecycleOwner.currentState = Lifecycle.State.RESUMED
Dispatchers.resetMain()
}
}
Assigning to currenState
is basic functionality for using TestLifecycleOwner
.
Our current workaround is to use UnconfinedTestDispatcher
. UnconfinedTestDispatcher
forces behaviors on us that StandardTestDispatcher
does not, though, so this is not great.
il...@google.com <il...@google.com>
sa...@google.com <sa...@google.com>
ap...@google.com <ap...@google.com> #4
Branch: androidx-main
commit 7c76b7013a0229dd0f75cedd910a5005d53e52f3
Author: Sanura Njaka <sanura@google.com>
Date: Mon Aug 07 10:43:36 2023
Add new API to fix TestLifecycleOwner hanging inside of runTest
The use of runBlocking in TestLifecycleOwner causes
tests to hang when the api is used from the context of
another coroutine (such as runTest). To fix this, we
have added a suspending function to allow using
TestLifecycleOwner from within a coroutine.
RelNote: "`TestLifecycleOwner` now includes the suspending
function `setCurrentState()` to give users the option of using `TestLifecycleOwner` from within a coroutine."
Test: testSetCurrentStateInRunTest
Fixes: 259344129
Change-Id: I329de3d76bcd7fa5e04077916ff551f9f2f3f864
M lifecycle/lifecycle-runtime-testing/api/current.txt
M lifecycle/lifecycle-runtime-testing/api/restricted_current.txt
M lifecycle/lifecycle-runtime-testing/build.gradle
M lifecycle/lifecycle-runtime-testing/src/androidTest/java/androidx/lifecycle/testing/TestLifecycleOwnerAndroidTest.kt
M lifecycle/lifecycle-runtime-testing/src/main/java/androidx/lifecycle/testing/TestLifecycleOwner.kt
il...@google.com <il...@google.com> #5
We've added a suspend fun setCurrentState(Lifecycle.State)
method on TestLifecycleOwner
that keeps the requirement of dispatching all events on the coroutineDispatcher
you've passed on TestLifecycleOwner
's constructor while not blocking the current thread. This will be available in Lifecycle 2.7.0-alpha02.
The follow up bug,
pr...@google.com <pr...@google.com> #6
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.lifecycle:lifecycle-runtime-testing:2.7.0-alpha02
Description
Component used: TestLifecycleOwner
Version used: 2.5.1
Devices/Android versions reproduced on: N/A
When used inside of
runTest
,...will hang and never return (not even after
dispatchTimeoutMs
expires)