Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellation requirement not enforced for Hot Flows #90

Closed
mhernand40 opened this issue Mar 8, 2022 · 0 comments · Fixed by #107
Closed

Cancellation requirement not enforced for Hot Flows #90

mhernand40 opened this issue Mar 8, 2022 · 0 comments · Fixed by #107

Comments

@mhernand40
Copy link

With Coroutines 1.6.0 and Turbine 0.7.0 the following tests should fail but they actually pass:

class TestHotFlowWithoutCancellingFlowTurbine {
    @Test
    fun `test runTest + StandardTestDispatcher`() = runTest(StandardTestDispatcher()) {
        testHotFlowsWithoutCancellingFlowTurbine()
    }

    @Test
    fun `test runTest + UnconfinedTestDispatcher`() = runTest(UnconfinedTestDispatcher()) {
        testHotFlowsWithoutCancellingFlowTurbine()
    }

    @Test
    fun `test runBlockingTest`() = runBlockingTest {
        testHotFlowsWithoutCancellingFlowTurbine()
    }

    @Test
    fun `test runBlocking`() = runBlocking {
        testHotFlowsWithoutCancellingFlowTurbine()
    }

    private suspend fun testHotFlowsWithoutCancellingFlowTurbine() {
        val sharedFlow = MutableSharedFlow<Int>()
        sharedFlow.test {
            sharedFlow.emit(0)
            assertEquals(0, awaitItem())
        }

        val stateFlow = MutableStateFlow(0)
        stateFlow.test {
            assertEquals(0, awaitItem())
        }

        val channel = Channel<Int>()
        channel.consumeAsFlow().test {
            channel.send(0)
            assertEquals(0, awaitItem())
        }
    }
}

Ideally these tests would require a call to either cancelAndConsumeRemainingEvents() or cancelAndIgnoreRemainingEvents() towards the end of each test { … } block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant