Skip to content

Commit

Permalink
Fix a flaky test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dkhalanskyjb committed Nov 15, 2024
1 parent f39e482 commit a4dc964
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions kotlinx-coroutines-core/jvm/test/JobChildStressTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class JobChildStressTest : TestBase() {
fun testChildAttachmentRacingWithLastChildCompletion() {
// All exceptions should get aggregated here
repeat(N_ITERATIONS) {
val canCloseThePool = CountDownLatch(1)
val pool = newFixedThreadPoolContext(3, "JobChildStressTest")
runBlocking {
val rogueJob = AtomicReference<Job?>()
/** not using [createCompletableDeferredForTesting] because we don't need extra children. */
Expand All @@ -89,23 +91,27 @@ class JobChildStressTest : TestBase() {
launch(pool + deferred) {
deferred.complete(Unit) // Transition deferred into "completing" state waiting for current child
// **Asynchronously** submit task that launches a child so it races with completion
try {
pool.executor.execute {
rogueJob.set(launch(pool + deferred) {
throw TestException("isCancelled: ${coroutineContext.job.isCancelled}")
})
}
} catch (_: RejectedExecutionException) {
// This is expected if the pool is closed
pool.executor.execute {
rogueJob.set(launch(pool + deferred) {
throw TestException("isCancelled: ${coroutineContext.job.isCancelled}")
})
canCloseThePool.countDown()
}
}

deferred.join()
val rogue = rogueJob.get()
if (rogue?.isActive == true) {
throw TestException("Rogue job $rogue with parent " + rogue.parent + " and children list: " + rogue.parent?.children?.toList())
} else {
canCloseThePool.await()
rogueJob.get().let {
assertNotNull(it)
assertTrue(it.isCancelled)
}
}
}
pool.close()
}
}
}

0 comments on commit a4dc964

Please sign in to comment.