Skip to content

Commit 11daf82

Browse files
authored
Fix a flaky test (#3335)
1 parent 2ee99e2 commit 11daf82

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

kotlinx-coroutines-core/jvm/test/exceptions/WithContextCancellationStressTest.kt

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import org.junit.Test
1010
import java.util.concurrent.*
1111
import kotlin.coroutines.*
1212
import kotlin.test.*
13+
import kotlin.time.Duration.Companion.minutes
1314

1415
class WithContextCancellationStressTest : TestBase() {
1516

16-
private val iterations = 15_000 * stressTestMultiplier
17+
private val timeoutAfter = 1.minutes
1718
private val pool = newFixedThreadPoolContext(3, "WithContextCancellationStressTest")
1819

1920
@After
@@ -28,56 +29,54 @@ class WithContextCancellationStressTest : TestBase() {
2829
var e1Cnt = 0
2930
var e2Cnt = 0
3031

31-
repeat(iterations) {
32-
val barrier = CyclicBarrier(4)
33-
val ctx = pool + NonCancellable
34-
var e1 = false
35-
var e2 = false
36-
val jobWithContext = async(ctx) {
37-
withContext(wrapperDispatcher(coroutineContext)) {
38-
launch {
39-
barrier.await()
40-
e1 = true
41-
throw TestException1()
42-
}
32+
withTimeout(timeoutAfter) {
33+
while (eCnt == 0 || e1Cnt == 0 || e2Cnt == 0) {
34+
val barrier = CyclicBarrier(4)
35+
val ctx = pool + NonCancellable
36+
var e1 = false
37+
var e2 = false
38+
val jobWithContext = async(ctx) {
39+
withContext(wrapperDispatcher(coroutineContext)) {
40+
launch {
41+
barrier.await()
42+
e1 = true
43+
throw TestException1()
44+
}
45+
46+
launch {
47+
barrier.await()
48+
e2 = true
49+
throw TestException2()
50+
}
4351

44-
launch {
4552
barrier.await()
46-
e2 = true
47-
throw TestException2()
53+
throw TestException()
4854
}
49-
50-
barrier.await()
51-
throw TestException()
5255
}
53-
}
5456

55-
barrier.await()
57+
barrier.await()
5658

57-
try {
58-
jobWithContext.await()
59-
} catch (e: Throwable) {
60-
when (e) {
61-
is TestException -> {
62-
eCnt++
63-
e.checkSuppressed(e1 = e1, e2 = e2)
64-
}
65-
is TestException1 -> {
66-
e1Cnt++
67-
e.checkSuppressed(ex = true, e2 = e2)
59+
try {
60+
jobWithContext.await()
61+
} catch (e: Throwable) {
62+
when (e) {
63+
is TestException -> {
64+
eCnt++
65+
e.checkSuppressed(e1 = e1, e2 = e2)
66+
}
67+
is TestException1 -> {
68+
e1Cnt++
69+
e.checkSuppressed(ex = true, e2 = e2)
70+
}
71+
is TestException2 -> {
72+
e2Cnt++
73+
e.checkSuppressed(ex = true, e1 = e1)
74+
}
75+
else -> error("Unexpected exception $e")
6876
}
69-
is TestException2 -> {
70-
e2Cnt++
71-
e.checkSuppressed(ex = true, e1 = e1)
72-
}
73-
else -> error("Unexpected exception $e")
7477
}
7578
}
7679
}
77-
78-
require(eCnt > 0) { "At least one TestException expected" }
79-
require(e1Cnt > 0) { "At least one TestException1 expected" }
80-
require(e2Cnt > 0) { "At least one TestException2 expected" }
8180
}
8281

8382
private fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {

0 commit comments

Comments
 (0)