Open Tetraquark opened 2 years ago
Workaround using withTimeout
:
fun CoroutineScope.waitChildrenCompletion() = runBlocking {
val job = this@waitChildrenCompletionWithTimeout.coroutineContext[Job]
val children = job?.children.orEmpty().toList()
children.forEach {
try {
withTimeout(100) {
it.join()
}
} catch (timeOutEx: TimeoutCancellationException) {}
}
}
If some inheritor of
ViewModel
has any endless collectors ofFlow
(e.g. launch coroutine ininit
block for someFlow
from abstract repository or collect data using extensionasLiveData
from moko-mvvm), thenwaitChildrenCompletion
will start endless join of that endless collector coroutines. So, the test will never end.