1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.debug 6 7 import kotlinx.coroutines.* 8 import org.junit.* 9 import org.junit.Test 10 import kotlin.test.* 11 12 class StartModeProbesTest : DebugTestBase() { 13 14 @Test testUndispatchednull15 fun testUndispatched() = runTest { 16 expect(1) 17 val job = launch(start = CoroutineStart.UNDISPATCHED) { 18 expect(2) 19 undispatchedSleeping() 20 assertTrue(true) 21 } 22 23 yield() 24 expect(3) 25 verifyPartialDump(2, "StartModeProbesTest.undispatchedSleeping") 26 job.cancelAndJoin() 27 verifyPartialDump(1, "StartModeProbesTest\$testUndispatched") 28 finish(4) 29 } 30 undispatchedSleepingnull31 private suspend fun undispatchedSleeping() { 32 delay(Long.MAX_VALUE) 33 assertTrue(true) 34 } 35 36 @Test <lambda>null37 fun testWithTimeoutWithUndispatched() = runTest { 38 expect(1) 39 val job = launchUndispatched() 40 41 yield() 42 expect(3) 43 verifyPartialDump( 44 2, 45 "StartModeProbesTest\$launchUndispatched\$1.invokeSuspend", 46 "StartModeProbesTest.withTimeoutHelper", 47 "StartModeProbesTest\$withTimeoutHelper\$2.invokeSuspend" 48 ) 49 job.cancelAndJoin() 50 verifyPartialDump(1, "StartModeProbesTest\$testWithTimeoutWithUndispatched") 51 finish(4) 52 } 53 launchUndispatchednull54 private fun CoroutineScope.launchUndispatched(): Job { 55 return launch(start = CoroutineStart.UNDISPATCHED) { 56 withTimeoutHelper() 57 assertTrue(true) 58 } 59 } 60 withTimeoutHelpernull61 private suspend fun withTimeoutHelper() { 62 withTimeout(Long.MAX_VALUE) { 63 expect(2) 64 delay(Long.MAX_VALUE) 65 } 66 67 assertTrue(true) 68 } 69 70 @Test <lambda>null71 fun testWithTimeout() = runTest { 72 withTimeout(Long.MAX_VALUE) { 73 testActiveDump( 74 false, 75 "StartModeProbesTest\$testWithTimeout\$1.invokeSuspend", 76 "state: RUNNING" 77 ) 78 } 79 } 80 81 @Test <lambda>null82 fun testWithTimeoutAfterYield() = runTest { 83 withTimeout(Long.MAX_VALUE) { 84 testActiveDump( 85 true, 86 "StartModeProbesTest\$testWithTimeoutAfterYield\$1.invokeSuspend", 87 "StartModeProbesTest\$testWithTimeoutAfterYield\$1\$1.invokeSuspend", 88 "StartModeProbesTest.testActiveDump", 89 "state: RUNNING" 90 ) 91 } 92 } 93 testActiveDumpnull94 private suspend fun testActiveDump(shouldYield: Boolean, vararg expectedFrames: String) { 95 if (shouldYield) yield() 96 verifyPartialDump(1, *expectedFrames) 97 assertTrue(true) 98 } 99 100 @Test <lambda>null101 fun testWithTailCall() = runTest { 102 expect(1) 103 val job = tailCallMethod() 104 yield() 105 expect(3) 106 verifyPartialDump(2, "StartModeProbesTest\$launchFromTailCall\$2") 107 job.cancelAndJoin() 108 verifyPartialDump(1, "StartModeProbesTest\$testWithTailCall") 109 finish(4) 110 } 111 tailCallMethodnull112 private suspend fun CoroutineScope.tailCallMethod(): Job = launchFromTailCall() 113 private suspend fun CoroutineScope.launchFromTailCall(): Job = launch { 114 expect(2) 115 delay(Long.MAX_VALUE) 116 } 117 118 @Test <lambda>null119 fun testCoroutineScope() = runTest { 120 expect(1) 121 val job = launch(start = CoroutineStart.UNDISPATCHED) { 122 runScope() 123 } 124 125 yield() 126 expect(3) 127 verifyPartialDump( 128 2, 129 "StartModeProbesTest\$runScope\$2.invokeSuspend", 130 "StartModeProbesTest\$testCoroutineScope\$1\$job\$1.invokeSuspend") 131 job.cancelAndJoin() 132 finish(4) 133 } 134 runScopenull135 private suspend fun runScope() { 136 coroutineScope { 137 expect(2) 138 delay(Long.MAX_VALUE) 139 } 140 } 141 } 142