Skip to content

Commit 01703c3

Browse files
committed
Fix tests for the Darwin main dispatcher
1 parent b25512f commit 01703c3

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

kotlinx-coroutines-core/common/test/MainDispatcherTestBase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ abstract class MainDispatcherTestBase: TestBase() {
1616

1717
abstract fun isMainThread(): Boolean?
1818

19-
abstract fun scheduleOnMainQueue(block: () -> Unit)
20-
2119
/** Runs the given block as a test, unless [shouldSkipTesting] indicates that the environment is not suitable. */
2220
fun runTestOrSkip(block: suspend CoroutineScope.() -> Unit): TestResult {
2321
// written as a block body to make the need to return `TestResult` explicit
@@ -170,6 +168,8 @@ abstract class MainDispatcherTestBase: TestBase() {
170168
}
171169

172170
abstract class WithRealTimeDelay : MainDispatcherTestBase() {
171+
abstract fun scheduleOnMainQueue(block: () -> Unit)
172+
173173
/** Tests that after a delay, the execution gets back to the main thread. */
174174
@Test
175175
fun testDelay() = runTestOrSkip {

kotlinx-coroutines-core/nativeDarwin/test/MainDispatcherTest.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.cinterop.*
78
import platform.CoreFoundation.*
89
import platform.darwin.*
910
import kotlin.coroutines.*
@@ -16,16 +17,11 @@ class MainDispatcherTest : MainDispatcherTestBase.WithRealTimeDelay() {
1617
// skip if already on the main thread, run blocking doesn't really work well with that
1718
override fun shouldSkipTesting(): Boolean = isMainThread()
1819

19-
@Test
20-
fun testDispatchNecessityCheckWithMainImmediateDispatcher() = runTestOrSkip {
21-
val immediate = Dispatchers.Main.immediate
22-
assertTrue(immediate.isDispatchNeeded(EmptyCoroutineContext))
23-
withContext(Dispatchers.Default) {
24-
assertTrue(immediate.isDispatchNeeded(EmptyCoroutineContext))
25-
withContext(Dispatchers.Main) {
26-
assertFalse(immediate.isDispatchNeeded(EmptyCoroutineContext))
20+
override fun scheduleOnMainQueue(block: () -> Unit) {
21+
autoreleasepool {
22+
dispatch_async(dispatch_get_main_queue()) {
23+
block()
2724
}
28-
assertTrue(immediate.isDispatchNeeded(EmptyCoroutineContext))
2925
}
3026
}
3127
}

0 commit comments

Comments
 (0)