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 6 7 import kotlinx.coroutines.internal.* 8 import platform.CoreFoundation.* 9 import platform.darwin.* 10 import kotlin.coroutines.* 11 import kotlin.test.* 12 13 class MainDispatcherTest : TestBase() { 14 isMainThreadnull15 private fun isMainThread(): Boolean = CFRunLoopGetCurrent() == CFRunLoopGetMain() 16 private fun canTestMainDispatcher() = !isMainThread() && multithreadingSupported 17 18 private fun runTestNotOnMainDispatcher(block: suspend CoroutineScope.() -> Unit) { 19 // skip if already on the main thread, run blocking doesn't really work well with that 20 if (!canTestMainDispatcher()) return 21 runTest(block = block) 22 } 23 24 @Test <lambda>null25 fun testDispatchNecessityCheckWithMainImmediateDispatcher() = runTestNotOnMainDispatcher { 26 val main = Dispatchers.Main.immediate 27 assertTrue(main.isDispatchNeeded(EmptyCoroutineContext)) 28 withContext(Dispatchers.Default) { 29 assertTrue(main.isDispatchNeeded(EmptyCoroutineContext)) 30 withContext(Dispatchers.Main) { 31 assertFalse(main.isDispatchNeeded(EmptyCoroutineContext)) 32 } 33 assertTrue(main.isDispatchNeeded(EmptyCoroutineContext)) 34 } 35 } 36 37 @Test <lambda>null38 fun testWithContext() = runTestNotOnMainDispatcher { 39 expect(1) 40 assertFalse(isMainThread()) 41 withContext(Dispatchers.Main) { 42 assertTrue(isMainThread()) 43 expect(2) 44 } 45 assertFalse(isMainThread()) 46 finish(3) 47 } 48 49 @Test <lambda>null50 fun testWithContextDelay() = runTestNotOnMainDispatcher { 51 expect(1) 52 withContext(Dispatchers.Main) { 53 assertTrue(isMainThread()) 54 expect(2) 55 delay(100) 56 assertTrue(isMainThread()) 57 expect(3) 58 } 59 assertFalse(isMainThread()) 60 finish(4) 61 } 62 63 @Test <lambda>null64 fun testWithTimeoutContextDelayNoTimeout() = runTestNotOnMainDispatcher { 65 expect(1) 66 withTimeout(1000) { 67 withContext(Dispatchers.Main) { 68 assertTrue(isMainThread()) 69 expect(2) 70 delay(100) 71 assertTrue(isMainThread()) 72 expect(3) 73 } 74 } 75 assertFalse(isMainThread()) 76 finish(4) 77 } 78 79 @Test <lambda>null80 fun testWithTimeoutContextDelayTimeout() = runTestNotOnMainDispatcher { 81 expect(1) 82 assertFailsWith<TimeoutCancellationException> { 83 withTimeout(100) { 84 withContext(Dispatchers.Main) { 85 assertTrue(isMainThread()) 86 expect(2) 87 delay(1000) 88 expectUnreached() 89 } 90 } 91 expectUnreached() 92 } 93 assertFalse(isMainThread()) 94 finish(3) 95 } 96 97 @Test <lambda>null98 fun testWithContextTimeoutDelayNoTimeout() = runTestNotOnMainDispatcher { 99 expect(1) 100 withContext(Dispatchers.Main) { 101 withTimeout(1000) { 102 assertTrue(isMainThread()) 103 expect(2) 104 delay(100) 105 assertTrue(isMainThread()) 106 expect(3) 107 } 108 } 109 assertFalse(isMainThread()) 110 finish(4) 111 } 112 113 @Test <lambda>null114 fun testWithContextTimeoutDelayTimeout() = runTestNotOnMainDispatcher { 115 expect(1) 116 assertFailsWith<TimeoutCancellationException> { 117 withContext(Dispatchers.Main) { 118 withTimeout(100) { 119 assertTrue(isMainThread()) 120 expect(2) 121 delay(1000) 122 expectUnreached() 123 } 124 } 125 expectUnreached() 126 } 127 assertFalse(isMainThread()) 128 finish(3) 129 } 130 } 131