Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt (Kotlin)

Code before:
            expect(3)
            emit(2)
        }

        assertEquals(listOf(1, 2), flow.toList())
        finish(4)
    }

    @Test
    fun testUpstreamNoTimeoutIsolatedContext() = runTest {
        val flow = flow {
            assertEquals("upstream", NamedDispatchers.name())
            expect(1)
            emit(1)
            expect(2)
            delay(10)
        }.flowOn(NamedDispatchers("upstream")).timeout(100.milliseconds)

        assertEquals(listOf(1), flow.toList())
        finish(3)
    }

    @Test
    fun testSharedFlowTimeout() = runTest {
        assertFailsWith<TimeoutCancellationException>(MutableSharedFlow<Int>().asSharedFlow().timeout(100.milliseconds))
    }

    @Test
    fun testSharedFlowCancelledNoTimeout() = runTest {
        val mutableSharedFlow = MutableSharedFlow<Int>()
        val list = arrayListOf<Int>()

        expect(1)
        val consumerJob = launch {
            expect(3)
            mutableSharedFlow.asSharedFlow().timeout(100.milliseconds).collect { list.add(it) }
            expectUnreached()
        }
        val producerJob = launch {
            expect(4)
            repeat(10) {
                delay(50)
                mutableSharedFlow.emit(it)
            }
            yield()
            consumerJob.cancel()
            expect(5)
        }

        expect(2)


Review comment (bug):
```suggestion
    @Test
    fun testSharedFlowTimeout() = runTest {
        // Workaround for JS legacy bug
        try {
            MutableSharedFlow<Int>().asSharedFlow().timeout(100.milliseconds).collect()
            expectUnreached()
        } catch (e: TimeoutCancellationException) {
            finish(1)
        }
    }
```

Code after:
            emit(1)
            expect(2)
            delay(300)
            expectUnreached()
        }.flowOn(NamedDispatchers("upstream")).timeout(100.milliseconds).catch {
            expect(3)
            emit(2)
        }

        assertEquals(listOf(1, 2), flow.toList())
        finish(4)
    }

    @Test
    fun testUpstreamNoTimeoutIsolatedContext() = runTest {
        val flow = flow {
            assertEquals("upstream", NamedDispatchers.name())
            expect(1)
            emit(1)
            expect(2)
            delay(10)
        }.flowOn(NamedDispatchers("upstream")).timeout(100.milliseconds)

        assertEquals(listOf(1), flow.toList())
        finish(3)
    }

    @Test
    fun testSharedFlowTimeout() = runTest {
        // Workaround for JS legacy bug
        try {
            MutableSharedFlow<Int>().asSharedFlow().timeout(100.milliseconds).collect()
            expectUnreached()
        } catch (e: TimeoutCancellationException) {
            finish(1)
        }
    }

    @Test
    fun testSharedFlowCancelledNoTimeout() = runTest {
        val mutableSharedFlow = MutableSharedFlow<Int>()
        val list = arrayListOf<Int>()

        expect(1)
        val consumerJob = launch {
            expect(3)
            mutableSharedFlow.asSharedFlow().timeout(100.milliseconds).collect { list.add(it) }
            expectUnreached()
        }
        val producerJob = launch {
            expect(4)