https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
j

julioromano

02/28/2024, 9:58 AM
Can somebody help debugging a very strange case where using Molecule even in an empty test fails due to: "After waiting for 1m, the test coroutine is not completing, there were active child jobs" ? This is the test:
Copy code
@Test
    fun `sample test`() = runTest {
        launchMolecule(RecompositionMode.Immediate) {
            // no-op
        }
    }
This the debug log:
Copy code
After waiting for 1m, the test coroutine is not completing, there were active child jobs: ["coroutine#3":StandaloneCoroutine{Active}@c1a65a7, JobImpl{Active}@2fc7cf1d, "coroutine#4":StandaloneCoroutine{Active}@3b5b1500]
kotlinx.coroutines.test.UncompletedCoroutinesError: After waiting for 1m, the test coroutine is not completing, there were active child jobs: ["coroutine#3":StandaloneCoroutine{Active}@c1a65a7, JobImpl{Active}@2fc7cf1d, "coroutine#4":StandaloneCoroutine{Active}@3b5b1500]
	at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$2$1.invoke(TestBuilders.kt:351)
	at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$2$1.invoke(TestBuilders.kt:335)
	at app//kotlinx.coroutines.InvokeOnCancelling.invoke(JobSupport.kt:1428)
	at app//kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1473)
	at app//kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:796)
	at app//kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:756)
	at app//kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:672)
	at app//kotlinx.coroutines.JobSupport.cancelCoroutine(JobSupport.kt:659)
	at app//kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:156)
	at app//kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:498)
	at app//kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)
	at app//kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:105)
	at java.base@17.0.10/java.lang.Thread.run(Thread.java:840)
The project is using Android and Jetpack Compose, so only molecule-runtime is imported. All libraries are updated to their most recent version as of now (though I also tried to downgrade molecule as well as compose and compose compiler but nothing changed). It is most probably not molecule's fault as the same setup is working in many other projects of mine but I can't figure out what's wrong in this one.
1
j

jw

02/28/2024, 11:45 AM
Molecule runs forever, so in this case it blocks the scope from finishing. You either need to create an explicit child scope with a job that you can explicitly cancel, or use
backgroundScope
to launch it.
j

julioromano

02/28/2024, 1:24 PM
Indeed.
Copy code
@Test
    fun `sample test`() = runTest {
        backgroundScope.launchMolecule(RecompositionMode.Immediate) {
            // no-op
        }
    }
This works.
Thanks
But I have several other projects which use molecule without creating a child scope or using
backgroundScope
and do work. Have I just been lucky?
Ohhh crab... I did use
moleculeFlow
in the other projects' tests, not
launchMolecule
.
blob think smart 1
Then the issue is solved. Brainfart on my side. Thanks for the help finding it.
6 Views