1 package kotlinx.coroutines.test.internal
2 
3 import kotlinx.coroutines.*
4 import kotlinx.coroutines.internal.*
5 
6 internal class TestMainDispatcherFactory : MainDispatcherFactory {
7 
createDispatchernull8     override fun createDispatcher(allFactories: List<MainDispatcherFactory>): MainCoroutineDispatcher {
9         val otherFactories = allFactories.filter { it !== this }
10         val secondBestFactory = otherFactories.maxByOrNull { it.loadPriority } ?: MissingMainCoroutineDispatcherFactory
11         val dispatcher = secondBestFactory.tryCreateDispatcher(otherFactories)
12         return TestMainDispatcher(dispatcher)
13     }
14 
15     /**
16      * [Int.MAX_VALUE] -- test dispatcher always wins no matter what factories are present in the classpath.
17      * By default, all actions are delegated to the second-priority dispatcher, so that it won't be the issue.
18      */
19     override val loadPriority: Int
20         get() = Int.MAX_VALUE
21 }
22 
getTestMainDispatchernull23 internal actual fun Dispatchers.getTestMainDispatcher(): TestMainDispatcher {
24     val mainDispatcher = Main
25     require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
26     return mainDispatcher
27 }