Skip to content

Commit aa7a4fd

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 aa7a4fd

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -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,24 @@ 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()
107+
rogueJob.get().let {
108+
assertNotNull(it)
109+
assertTrue(it.isCancelled)
110+
}
107111
}
108112
}
109113
}

0 commit comments

Comments
 (0)