in kotlinx-coroutines-test/jvm/src/internal/TestMainDispatcherJvm.kt [8:28]
override fun createDispatcher(allFactories: List<MainDispatcherFactory>): MainCoroutineDispatcher {
val otherFactories = allFactories.filter { it !== this }
val secondBestFactory = otherFactories.maxByOrNull { it.loadPriority } ?: MissingMainCoroutineDispatcherFactory
/* Do not immediately create the alternative dispatcher, as with `SUPPORT_MISSING` set to `false`,
it will throw an exception. Instead, create it lazily. */
return TestMainDispatcher({
val dispatcher = try {
secondBestFactory.tryCreateDispatcher(otherFactories)
} catch (e: Throwable) {
reportMissingMainCoroutineDispatcher(e)
}
if (dispatcher.isMissing()) {
reportMissingMainCoroutineDispatcher(runCatching {
// attempt to dispatch something to the missing dispatcher to trigger the exception.
dispatcher.dispatch(dispatcher, Runnable { })
}.exceptionOrNull()) // can not be null, but it does not matter.
} else {
dispatcher
}
})
}