|
| 1 | +/* |
| 2 | + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | +package kotlinx.coroutines.debug |
| 5 | + |
| 6 | +import kotlinx.coroutines.* |
| 7 | +import org.junit.* |
| 8 | +import org.junit.Test |
| 9 | +import java.util.concurrent.* |
| 10 | +import kotlin.test.* |
| 11 | + |
| 12 | +class RunningThreadStackMergeTest : TestBase() { |
| 13 | + |
| 14 | + private val testMainBlocker = CountDownLatch(1) // Test body blocks on it |
| 15 | + private val coroutineBlocker = CyclicBarrier(2) // Launched coroutine blocks on it |
| 16 | + |
| 17 | + @Before |
| 18 | + fun setUp() { |
| 19 | + before() |
| 20 | + DebugProbes.install() |
| 21 | + } |
| 22 | + |
| 23 | + @After |
| 24 | + fun tearDown() { |
| 25 | + try { |
| 26 | + DebugProbes.uninstall() |
| 27 | + } finally { |
| 28 | + onCompletion() |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun testStackMergeWithContext() = runTest { |
| 34 | + launchCoroutine() |
| 35 | + awaitCoroutineStarted() |
| 36 | + |
| 37 | + verifyDump( |
| 38 | + "Coroutine \"coroutine#1\":BlockingCoroutine{Active}@62230679", // <- this one is ignored |
| 39 | + "Coroutine \"coroutine#2\":StandaloneCoroutine{Active}@50284dc4, state: RUNNING\n" + |
| 40 | + "\tat sun.misc.Unsafe.park(Native Method)\n" + |
| 41 | + "\tat java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)\n" + |
| 42 | + "\tat java.util.concurrent.locks.AbstractQueuedSynchronizer\$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)\n" + |
| 43 | + "\tat java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)\n" + |
| 44 | + "\tat java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)\n" + |
| 45 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.nonSuspendingFun(RunningThreadStackMergeTest.kt:86)\n" + |
| 46 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.access\$nonSuspendingFun(RunningThreadStackMergeTest.kt:12)\n" + |
| 47 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$suspendingFunction\$2.invokeSuspend(RunningThreadStackMergeTest.kt:77)\n" + |
| 48 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.suspendingFunction(RunningThreadStackMergeTest.kt:75)\n" + |
| 49 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$launchCoroutine\$1.invokeSuspend(RunningThreadStackMergeTest.kt:68)\n" + |
| 50 | + "\t(Coroutine creation stacktrace)\n" + |
| 51 | + "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)", |
| 52 | + ignoredCoroutine = "BlockingCoroutine" |
| 53 | + ) |
| 54 | + coroutineBlocker.await() |
| 55 | + } |
| 56 | + |
| 57 | + private fun awaitCoroutineStarted() { |
| 58 | + testMainBlocker.await() |
| 59 | + while (coroutineBlocker.numberWaiting != 1) { |
| 60 | + Thread.sleep(10) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun CoroutineScope.launchCoroutine() { |
| 65 | + launch(Dispatchers.Default) { |
| 66 | + suspendingFunction() |
| 67 | + assertTrue(true) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private suspend fun suspendingFunction() { |
| 72 | + // Typical use-case |
| 73 | + withContext(Dispatchers.IO) { |
| 74 | + yield() |
| 75 | + nonSuspendingFun() |
| 76 | + } |
| 77 | + |
| 78 | + assertTrue(true) |
| 79 | + } |
| 80 | + |
| 81 | + private fun nonSuspendingFun() { |
| 82 | + testMainBlocker.countDown() |
| 83 | + coroutineBlocker.await() |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun testStackMergeEscapeSuspendMethod() = runTest { |
| 88 | + launchEscapingCoroutine() |
| 89 | + awaitCoroutineStarted() |
| 90 | + verifyDump( |
| 91 | + "Coroutine \"coroutine#1\":BlockingCoroutine{Active}@62230679", // <- this one is ignored |
| 92 | + "Coroutine \"coroutine#2\":StandaloneCoroutine{Active}@3aea3c67, state: RUNNING\n" + |
| 93 | + "\tat sun.misc.Unsafe.park(Native Method)\n" + |
| 94 | + "\tat java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)\n" + |
| 95 | + "\tat java.util.concurrent.locks.AbstractQueuedSynchronizer\$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)\n" + |
| 96 | + "\tat java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)\n" + |
| 97 | + "\tat java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)\n" + |
| 98 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.nonSuspendingFun(RunningThreadStackMergeTest.kt:83)\n" + |
| 99 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.access\$nonSuspendingFun(RunningThreadStackMergeTest.kt:12)\n" + |
| 100 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$suspendingFunctionWithContext\$2.invokeSuspend(RunningThreadStackMergeTest.kt:124)\n" + |
| 101 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest.suspendingFunctionWithContext(RunningThreadStackMergeTest.kt:122)\n" + |
| 102 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$launchEscapingCoroutine\$1.invokeSuspend(RunningThreadStackMergeTest.kt:116)\n" + |
| 103 | + "\t(Coroutine creation stacktrace)\n" + |
| 104 | + "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)", |
| 105 | + ignoredCoroutine = "BlockingCoroutine" |
| 106 | + ) |
| 107 | + coroutineBlocker.await() |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun testRunBlocking() = runBlocking { |
| 112 | + verifyDump("Coroutine \"coroutine#1\":BlockingCoroutine{Active}@4bcd176c, state: RUNNING\n" + |
| 113 | + "\tat java.lang.Thread.getStackTrace(Thread.java:1552)\n" + |
| 114 | + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDump(DebugProbesImpl.kt:147)\n" + |
| 115 | + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt:122)\n" + |
| 116 | + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt:109)\n" + |
| 117 | + "\tat kotlinx.coroutines.debug.DebugProbes.dumpCoroutines(DebugProbes.kt:122)\n" + |
| 118 | + "\tat kotlinx.coroutines.debug.StracktraceUtilsKt.verifyDump(StracktraceUtils.kt)\n" + |
| 119 | + "\tat kotlinx.coroutines.debug.StracktraceUtilsKt.verifyDump\$default(StracktraceUtils.kt)\n" + |
| 120 | + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$testRunBlocking\$1.invokeSuspend(RunningThreadStackMergeTest.kt:112)\n" + |
| 121 | + "\t(Coroutine creation stacktrace)\n" + |
| 122 | + "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)\n") |
| 123 | + } |
| 124 | + |
| 125 | + private fun CoroutineScope.launchEscapingCoroutine() { |
| 126 | + launch(Dispatchers.Default) { |
| 127 | + suspendingFunctionWithContext() |
| 128 | + assertTrue(true) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private suspend fun suspendingFunctionWithContext() { |
| 133 | + withContext(Dispatchers.IO) { |
| 134 | + actualSuspensionPoint() |
| 135 | + nonSuspendingFun() |
| 136 | + } |
| 137 | + |
| 138 | + assertTrue(true) |
| 139 | + } |
| 140 | + |
| 141 | + private suspend fun actualSuspensionPoint() { |
| 142 | + nestedSuspensionPoint() |
| 143 | + assertTrue(true) |
| 144 | + } |
| 145 | + |
| 146 | + private suspend fun nestedSuspensionPoint() { |
| 147 | + yield() |
| 148 | + assertTrue(true) |
| 149 | + } |
| 150 | +} |
0 commit comments