Skip to content

Commit 3eaaefd

Browse files
committed
Fix a flaky test
Follow-up for #4227 This time, the fix is less likely to work just by accident. With enough loop iterations if we close the pool after each iteration, the current `develop` fails in 10/10 runs, whereas after this fix, running the test for 20 runs didn't fail once.
1 parent f39e482 commit 3eaaefd

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.test.*
88
/**
99
* Testing the procedure of attaching a child to the parent job.
1010
*/
11-
class JobChildStressTest : TestBase() {
11+
class JobChildStressTest : TestBase {
1212
private val N_ITERATIONS = 10_000 * stressTestMultiplier
1313
private val pool = newFixedThreadPoolContext(3, "JobChildStressTest")
1414

@@ -77,6 +77,7 @@ class JobChildStressTest : TestBase() {
7777
fun testChildAttachmentRacingWithLastChildCompletion() {
7878
// All exceptions should get aggregated here
7979
repeat(N_ITERATIONS) {
80+
val canCloseThePool = CountDownLatch(1)
8081
runBlocking {
8182
val rogueJob = AtomicReference<Job?>()
8283
/** not using [createCompletableDeferredForTesting] because we don't need extra children. */
@@ -89,21 +90,20 @@ class JobChildStressTest : TestBase() {
8990
launch(pool + deferred) {
9091
deferred.complete(Unit) // Transition deferred into "completing" state waiting for current child
9192
// **Asynchronously** submit task that launches a child so it races with completion
92-
try {
93-
pool.executor.execute {
94-
rogueJob.set(launch(pool + deferred) {
95-
throw TestException("isCancelled: ${coroutineContext.job.isCancelled}")
96-
})
97-
}
98-
} catch (_: RejectedExecutionException) {
99-
// This is expected if the pool is closed
93+
pool.executor.execute {
94+
rogueJob.set(launch(pool + deferred) {
95+
throw TestException("isCancelled: ${coroutineContext.job.isCancelled}")
96+
})
97+
canCloseThePool.countDown()
10098
}
10199
}
102100

103101
deferred.join()
104102
val rogue = rogueJob.get()
105103
if (rogue?.isActive == true) {
106104
throw TestException("Rogue job $rogue with parent " + rogue.parent + " and children list: " + rogue.parent?.children?.toList())
105+
} else {
106+
canCloseThePool.await()
107107
}
108108
}
109109
}

0 commit comments

Comments
 (0)