molecule icon indicating copy to clipboard operation
molecule copied to clipboard

Some test is flaky and hangs

Open JakeWharton opened this issue 8 months ago • 4 comments

See https://github.com/cashapp/molecule/actions/runs/14384606655/attempts/1, for example.

Not the first time I've seen it, either.

JakeWharton avatar Apr 10 '25 19:04 JakeWharton

Pretty confident it's MoleculeConcurrentTest

JakeWharton avatar Apr 10 '25 19:04 JakeWharton

I'm fairly certain it is the MoleculeConcurrentTest class that is causing the hanging builds. You can reproduce it locally by placing the test in a loop:

@Test fun coroutineContextHonoredByImmediateClock() = repeat(2) { /* ... existing test ... */ }

I believe the problem is that the original test relies on recomposition occurring naturally, but the implicit StandardTestDispatcher used by runTest doesn't drive Dispatchers.Default—which is where the recomposition is scheduled. Since nothing is forcing recomposition to occur, the cancelLatch.await() hangs indefinitely.

Compose runtime has a similar test in their RecomposerTests.jvm.kt class. You could modify your coroutineContextHonoredByImmediateClock test to do something similar:

@Test fun coroutineContextHonoredByImmediateClock() = runTest(UnconfinedTestDispatcher()) {
  val testThread = Thread.currentThread()

  val job = Job()
  val threadChannel = Channel<Thread>(Channel.UNLIMITED)
  lateinit var recomposeScope: RecomposeScope
  backgroundScope.launchMolecule(Immediate, job + Dispatchers.Default) {
    threadChannel.trySend(Thread.currentThread())
    val scope = currentRecomposeScope
    SideEffect { recomposeScope = scope }
  }

  val firstThread = threadChannel.receive()
  assertThat(firstThread).isSameInstanceAs(testThread)

  recomposeScope.invalidate()

  val secondThread = threadChannel.receive()
  assertThat(secondThread).isNotSameInstanceAs(testThread)
  assertThat(secondThread.name).contains("DefaultDispatcher")

  job.cancelAndJoin()
}

FletchMcKee avatar Apr 22 '25 16:04 FletchMcKee

I can reopen #617 since it looks like there are still hanging builds.

FletchMcKee avatar May 15 '25 21:05 FletchMcKee

It's definitely still problematic, but I haven't had time to look at anything.

JakeWharton avatar May 15 '25 23:05 JakeWharton