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