Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
setMain
in common code (#2967)
Fixes #1720
- Loading branch information
1 parent
2706a76
commit 5a62781
Showing
14 changed files
with
142 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
kotlinx-coroutines-test/common/src/internal/TestMainDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.test.internal | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* The testable main dispatcher used by kotlinx-coroutines-test. | ||
* It is a [MainCoroutineDispatcher] that delegates all actions to a settable delegate. | ||
*/ | ||
internal class TestMainDispatcher(private var delegate: CoroutineDispatcher): | ||
MainCoroutineDispatcher(), | ||
Delay by (delegate as? Delay ?: defaultDelay) | ||
{ | ||
private val mainDispatcher = delegate // the initial value passed to the constructor | ||
|
||
override val immediate: MainCoroutineDispatcher | ||
get() = (delegate as? MainCoroutineDispatcher)?.immediate ?: this | ||
|
||
override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block) | ||
|
||
override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context) | ||
|
||
override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block) | ||
|
||
fun setDispatcher(dispatcher: CoroutineDispatcher) { | ||
delegate = dispatcher | ||
} | ||
|
||
fun resetDispatcher() { | ||
delegate = mainDispatcher | ||
} | ||
} | ||
|
||
@Suppress("INVISIBLE_MEMBER") | ||
private val defaultDelay | ||
inline get() = DefaultDelay | ||
|
||
@Suppress("INVISIBLE_MEMBER") | ||
internal expect fun Dispatchers.getTestMainDispatcher(): TestMainDispatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
kotlinx-coroutines-test/js/src/internal/TestMainDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.test.internal | ||
import kotlinx.coroutines.* | ||
|
||
@Suppress("INVISIBLE_MEMBER") | ||
internal actual fun Dispatchers.getTestMainDispatcher(): TestMainDispatcher = | ||
when (val mainDispatcher = Main) { | ||
is TestMainDispatcher -> mainDispatcher | ||
else -> TestMainDispatcher(mainDispatcher).also { injectMain(it) } | ||
} |
This file was deleted.
Oops, something went wrong.
76 changes: 0 additions & 76 deletions
76
kotlinx-coroutines-test/jvm/src/internal/TestMainDispatcher.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
kotlinx-coroutines-test/jvm/src/internal/TestMainDispatcherJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.test.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.internal.* | ||
|
||
internal class TestMainDispatcherFactory : MainDispatcherFactory { | ||
|
||
override fun createDispatcher(allFactories: List<MainDispatcherFactory>): MainCoroutineDispatcher { | ||
val otherFactories = allFactories.filter { it !== this } | ||
val secondBestFactory = otherFactories.maxByOrNull { it.loadPriority } ?: MissingMainCoroutineDispatcherFactory | ||
val dispatcher = secondBestFactory.tryCreateDispatcher(otherFactories) | ||
return TestMainDispatcher(dispatcher) | ||
} | ||
|
||
/** | ||
* [Int.MAX_VALUE] -- test dispatcher always wins no matter what factories are present in the classpath. | ||
* By default, all actions are delegated to the second-priority dispatcher, so that it won't be the issue. | ||
*/ | ||
override val loadPriority: Int | ||
get() = Int.MAX_VALUE | ||
} | ||
|
||
internal actual fun Dispatchers.getTestMainDispatcher(): TestMainDispatcher { | ||
val mainDispatcher = Main | ||
require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." } | ||
return mainDispatcher | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.test.internal | ||
import kotlinx.coroutines.* | ||
|
||
@Suppress("INVISIBLE_MEMBER") | ||
internal actual fun Dispatchers.getTestMainDispatcher(): TestMainDispatcher = | ||
when (val mainDispatcher = Main) { | ||
is TestMainDispatcher -> mainDispatcher | ||
else -> TestMainDispatcher(mainDispatcher).also { injectMain(it) } | ||
} |