Skip to content

Commit a2587d2

Browse files
committed
Robust waiting in CoroutinesDumpTest
Fixed incorrect usage of Object.await/notify idiom Fixes #1513
1 parent 0bef2b9 commit a2587d2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.coroutines.debug
@@ -11,6 +11,7 @@ import kotlin.test.*
1111

1212
class CoroutinesDumpTest : DebugTestBase() {
1313
private val monitor = Any()
14+
private var coroutineStarted = false // guarded by monitor
1415

1516
@Test
1617
fun testSuspendedCoroutine() = synchronized(monitor) {
@@ -130,7 +131,7 @@ class CoroutinesDumpTest : DebugTestBase() {
130131

131132
private suspend fun nestedActiveMethod(shouldSuspend: Boolean) {
132133
if (shouldSuspend) yield()
133-
notifyTest()
134+
notifyCoroutineStarted()
134135
while (coroutineContext[Job]!!.isActive) {
135136
Thread.sleep(100)
136137
}
@@ -143,17 +144,18 @@ class CoroutinesDumpTest : DebugTestBase() {
143144

144145
private suspend fun sleepingNestedMethod() {
145146
yield()
146-
notifyTest()
147+
notifyCoroutineStarted()
147148
delay(Long.MAX_VALUE)
148149
}
149150

150151
private fun awaitCoroutineStarted() {
151-
(monitor as Object).wait()
152+
while (!coroutineStarted) (monitor as Object).wait()
152153
}
153154

154-
private fun notifyTest() {
155+
private fun notifyCoroutineStarted() {
155156
synchronized(monitor) {
156-
(monitor as Object).notify()
157+
coroutineStarted = true
158+
(monitor as Object).notifyAll()
157159
}
158160
}
159161
}

0 commit comments

Comments
 (0)