8000 Virtual time can't actually be controlled · Issue #268 · cashapp/turbine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Virtual time can't actually be controlled #268
Open
@mightyguava

Description

@mightyguava

While trying to test delays with turbine I noticed that trying to control virtual time didn't actually do anything. Like the test here

@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()
runCurrent()
assertEquals("2", awaitItem())
awaitComplete()
}
}

If you just remove all of the advanceTimeBy() and runCurrent() calls, it still passes

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

I'd expect the lack of advanceTimeBy() calls to fail the first assertEquals(). I think what's happening is that since awaitItem() will yield, the TestDispatcher will advance time automatically. This makes virtual time meaningless when testing with Turbine unless all you want to do is verify ordering.

There's also another annoying problem that an awaitItem() call will cause other coroutines in the same scope that are running a while (true) { delay(...); doSomething(...) } to constantly spin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0