Repository: Kotlin/kotlinx.coroutines
File: integration/kotlinx-coroutines-guava/test/ListenableFutureTest.kt (Kotlin)

Code before:
        future { expectUnreached() }
        future(start = CoroutineStart.ATOMIC) { }
        future(start = CoroutineStart.UNDISPATCHED) { }
    }

    @Test
    fun testStackOverflow() = runTest {
        val future = SettableFuture.create<Int>()
        val completed = AtomicLong()
        val count = 10000L
        val children = ArrayList<Job>()
        for (i in 0 until count) {
            children += launch(Dispatchers.Default) {
                future.asDeferred().await()
                completed.incrementAndGet()
            }
        }
        future.set(1)
        withTimeout(60_000) {
            children.forEach { it.join() }
            assertEquals(count, completed.get())
        }
    }

    @Test
    fun stressTestFutureDoesNotReportToCoroutineExceptionHandler() = runTest {
        repeat(1000) {
            supervisorScope { // Don't propagate failures in children to parent and other children.
                val innerFuture = SettableFuture.create<Unit>()
                val outerFuture = async { innerFuture.await() }

                withContext(Dispatchers.Default) {
                    launch { innerFuture.setException(TestException("can be lost")) }
                    launch { outerFuture.cancel() }
                    // nothing should be reported to CoroutineExceptionHandler, otherwise `Future.cancel` contract violation.
                }
            }
        }
    }

    @Test
    fun futurePropagatesExceptionToParentAfterCancellation() = runTest {
        val latch = CompletableDeferred<Boolean>()
        val parent = Job()
        val scope = CoroutineScope(parent)
        val exception = TestException("propagated to parent")
        val future = scope.future {
            withContext(NonCancellable) {
                latch.await()
                throw exception
            }

Review comment (suggestion):
[nit] all tests are started with `test` prefix: https://github.com/Kotlin/kotlinx.coroutines/blob/master/CONTRIBUTING.md#submitting-prs

Code after:
        future.set(1)
        withTimeout(60_000) {
            children.forEach { it.join() }
            assertEquals(count, completed.get())
        }
    }

    @Test
    fun futurePropagatesExceptionToParentAfterCancellation() = runTest {
        val latch = CompletableDeferred<Boolean>()
        val parent = Job()
        val scope = CoroutineScope(parent)
        val exception = TestException("propagated to parent")
        val future = scope.future {
            withContext(NonCancellable) {
                latch.await()
                throw exception
            }
        }
        future.cancel(true)
        latch.complete(true)
        parent.join()
        assertTrue(parent.isCancelled)
        assertEquals(exception, parent.getCancellationException().cause)
    }

    // Stress tests.

    @Test
    fun testFutureDoesNotReportToCoroutineExceptionHandler() = runTest {
        repeat(1000) {
            supervisorScope { // Don't propagate failures in children to parent and other children.
                val innerFuture = SettableFuture.create<Unit>()
                val outerFuture = async { innerFuture.await() }

                withContext(Dispatchers.Default) {
                    launch { innerFuture.setException(TestException("can be lost")) }
                    launch { outerFuture.cancel() }
                    // nothing should be reported to CoroutineExceptionHandler, otherwise `Future.cancel` contract violation.
                }
            }
        }
    }

    @Test
    fun testJobListenableFutureIsCancelledDoesNotThrow() = runTest {
        repeat(1000) {
            val deferred = CompletableDeferred<String>()
            val asListenableFuture = deferred.asListenableFuture()
            // We heed two threads to test a race condition.
            withContext(Dispatchers.Default) {