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

Can not control virtual time for multiple delay calls when using runTest #164

Closed
byencho opened this issue Oct 31, 2022 · 4 comments · Fixed by #168
Closed

Can not control virtual time for multiple delay calls when using runTest #164

byencho opened this issue Oct 31, 2022 · 4 comments · Fixed by #168
Labels
bug Something isn't working

Comments

@byencho
Copy link

byencho commented Oct 31, 2022

When using 0.12.0, it appears that the virtual time of the test cannot be controlled when using runTest after the first call to awaitItem when there are multiple delay calls. Consider the following test:

    @Test
    fun virtualTimeCanBeControlled() = runTest {
        flow {
            delay(5000)
            emit("1")
            delay(5000)
            emit("2")
        }
            .test {
                expectNoEvents()

                advanceTimeBy(5000)

                expectNoEvents()
                runCurrent()
                assertEquals("1", awaitItem())

                advanceTimeBy(5000)

                expectNoEvents() // <---------- Fails here
                runCurrent()
                assertEquals("2", awaitItem())

                awaitComplete()
            }
    }

This fails with the following error where indicated:

Expected no events but found Item(2)
app.cash.turbine.TurbineAssertionError: Expected no events but found Item(2)
	at app//app.cash.turbine.ChannelKt.unexpectedEvent(channel.kt:258)
	at app//app.cash.turbine.ChannelKt.unexpectedResult-JslgfBc(channel.kt:253)
	at app//app.cash.turbine.ChannelKt.expectNoEvents(channel.kt:71)
	at app//app.cash.turbine.ChannelTurbine.expectNoEvents(Turbine.kt:184)
	...

This test passes, however, when using runBlocking and a manually-supplied UnconfinedTestDispatcher:

    private val testDispatcher = UnconfinedTestDispatcher()

    @Test
    fun virtualTimeCanBeControlled() = runBlocking {
        flow {
            delay(5000)
            emit("1")
            delay(5000)
            emit("2")
        }
            .flowOn(testDispatcher)
            .test {
                expectNoEvents()

                testDispatcher.scheduler.advanceTimeBy(5000)

                expectNoEvents()
                testDispatcher.scheduler.runCurrent()
                assertEquals("1", awaitItem())

                testDispatcher.scheduler.advanceTimeBy(5000)

                expectNoEvents()
                testDispatcher.scheduler.runCurrent()
                assertEquals("2", awaitItem())

                awaitComplete()
            }
    }

This may be related to the changes in #151, which bases logic on whether the current scope is using a test scheduler.

  • Kotlin version : 1.7.10
  • Coroutines version : 1.6.4
@byencho byencho changed the title Can not control virtual time when using runTest Can not control virtual time with multiple delay calls when using runTest Oct 31, 2022
@byencho byencho changed the title Can not control virtual time with multiple delay calls when using runTest Can not control virtual time for multiple delay calls when using runTest Oct 31, 2022
@lucaskivi
Copy link

lucaskivi commented Oct 31, 2022

This also affects me! 👍

@jingibus jingibus added the bug Something isn't working label Nov 2, 2022
@JakeWharton
Copy link
Member

JakeWharton commented Nov 3, 2022

Doesn't seem related to that PR. When I write a non-Turbine test using the unconfined test dispatcher for flow collection it still passes.

@JakeWharton
Copy link
Member

Got it. The problem is in withWallclockTimeout. Fix incoming.

@byencho
Copy link
Author

byencho commented Nov 3, 2022

Thanks @JakeWharton !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants