Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/jvm/test/exceptions/WithContextCancellationStressTest.kt (Kotlin)

Code before:
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Test
import java.util.concurrent.*
import kotlin.coroutines.*
import kotlin.test.*

class WithContextCancellationStressTest : TestBase() {

    private val iterations = 15_000 * stressTestMultiplier
    private val pool = newFixedThreadPoolContext(3, "WithContextCancellationStressTest")

    @After
    fun tearDown() {
        pool.close()
    }

    @Test
    @Suppress("DEPRECATION")
    fun testConcurrentFailure() = runBlocking {
        var eCnt = 0
        var e1Cnt = 0
        var e2Cnt = 0
        var loopsToMake = iterations

        while (eCnt == 0 || e1Cnt == 0 || e2Cnt == 0 || loopsToMake-- > 0) {
            val barrier = CyclicBarrier(4)
            val ctx = pool + NonCancellable
            var e1 = false
            var e2 = false
            val jobWithContext = async(ctx) {
                withContext(wrapperDispatcher(coroutineContext)) {
                    launch {
                        barrier.await()
                        e1 = true
                        throw TestException1()
                    }

                    launch {
                        barrier.await()
                        e2 = true
                        throw TestException2()
                    }

                    barrier.await()
                    throw TestException()
                }
            }

            barrier.await()


Review comment (suggestion):
The rationale behind this test was the following: given enough parallelism (3 threads) and iterations, we expect to observe every exception type at least once.

Taking into account recent changes in our CI and the nature of this test, I would suggest changing it to be more robust, but keep the original intent: iterate until all exception types are observed OR really many iterations passed (to address flakyness), but add a large enough timeout so CI won't hang for four hours

Code after:
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Test
import java.util.concurrent.*
import kotlin.coroutines.*
import kotlin.test.*
import kotlin.time.Duration.Companion.minutes

class WithContextCancellationStressTest : TestBase() {

    private val timeoutAfter = 1.minutes
    private val pool = newFixedThreadPoolContext(3, "WithContextCancellationStressTest")

    @After
    fun tearDown() {
        pool.close()
    }

    @Test
    @Suppress("DEPRECATION")
    fun testConcurrentFailure() = runBlocking {
        var eCnt = 0
        var e1Cnt = 0
        var e2Cnt = 0

        withTimeout(timeoutAfter) {
            while (eCnt == 0 || e1Cnt == 0 || e2Cnt == 0) {
                val barrier = CyclicBarrier(4)
                val ctx = pool + NonCancellable
                var e1 = false
                var e2 = false
                val jobWithContext = async(ctx) {
                    withContext(wrapperDispatcher(coroutineContext)) {
                        launch {
                            barrier.await()
                            e1 = true
                            throw TestException1()
                        }

                        launch {
                            barrier.await()
                            e2 = true
                            throw TestException2()
                        }

                        barrier.await()
                        throw TestException()
                    }
                }

                barrier.await()