Skip to content

Commit 632d016

Browse files
dkhalanskyjbmurfel
authored andcommitted
Fix a couple of flaky tests (#4478)
* Fix cancellable continuation's toString check A smart enough compiler can inline a lambda being called immediately after being defined, which can trigger the tail call optimization, leading to a test error. * Fix RunningThreadStackMergeTest flakiness Ensure that the thread actually calls `park` before proceeding.
1 parent 274e6f4 commit 632d016

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CancellableContinuationJvmTest : TestBase() {
1515
it.resume(Unit)
1616
assertTrue(it.toString().contains("kotlinx.coroutines.CancellableContinuationJvmTest.checkToString(CancellableContinuationJvmTest.kt"))
1717
}
18-
suspend {}() // Eliminate tail-call optimization
18+
yield() // Eliminate tail-call optimization
1919
}
2020

2121
@Test

kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlin.test.*
99

1010
class RunningThreadStackMergeTest : DebugTestBase() {
1111

12+
private var coroutineThread: Thread? = null
1213
private val testMainBlocker = CountDownLatch(1) // Test body blocks on it
1314
private val coroutineBlocker = CyclicBarrier(2) // Launched coroutine blocks on it
1415

@@ -39,6 +40,10 @@ class RunningThreadStackMergeTest : DebugTestBase() {
3940
while (coroutineBlocker.numberWaiting != 1) {
4041
Thread.sleep(10)
4142
}
43+
// Wait for the coroutine to actually call `LockSupport.park`
44+
while (coroutineThread?.state != Thread.State.WAITING) {
45+
Thread.sleep(10)
46+
}
4247
}
4348

4449
private fun CoroutineScope.launchCoroutine() {
@@ -59,6 +64,7 @@ class RunningThreadStackMergeTest : DebugTestBase() {
5964
}
6065

6166
private fun nonSuspendingFun() {
67+
coroutineThread = Thread.currentThread()
6268
testMainBlocker.countDown()
6369
coroutineBlocker.await()
6470
}
@@ -67,7 +73,6 @@ class RunningThreadStackMergeTest : DebugTestBase() {
6773
fun testStackMergeEscapeSuspendMethod() = runTest {
6874
launchEscapingCoroutine()
6975
awaitCoroutineStarted()
70-
Thread.sleep(10)
7176
verifyDump(
7277
"Coroutine \"coroutine#2\":StandaloneCoroutine{Active}@3aea3c67, state: RUNNING\n" +
7378
"\tat jdk.internal.misc.Unsafe.park(Native Method)\n" +

0 commit comments

Comments
 (0)