been seeing a fun interaction where creating more ...
# javascript
r
been seeing a fun interaction where creating more than 9-10 scopes (with at least one job) within a "kotlinx.coroutines.test.runTest" call will cause its internals to stall and timeout instead of finishing the test naturally. AFAICT so far canceling the scopes or the jobs (or awaiting the jobs) has no effect
ran into this in react testing as I had a few tests that made a number of components that use the revamped useLayoutEffect hook (that internally spawns a one-off coroutine scope)
Copy code
@Test
    fun pleaseDontExplode() = runTest {
        val scopes = generateSequence { MainScope() }.take(10)
        scopes
            .map { it.async { println("running job") } }
            .forEach { it.await() }
        scopes.forEach { it.cancel() }
}
this explodes, naturally. Haven't run it on other platforms, but its possible this is a js implementation specific problem
i have workarounds for my project so its not blocking me, but figured this might be the best place to raise the issue in the hope someone who could actually do something about it might see it 😅
t
2 times
forEach
for
Sequence
? 😉 Should it work in fact? 😀
r
oh i'm not trying to do anything in this case
haha
as you probably know, just trying to get the error without all the extra rubric
Copy code
@OptIn(DelicateCoroutinesApi::class)
    @Test
    fun pleaseDontExplode() = GlobalScope.promise {
        val scopes = generateSequence { MainScope() }.take(15).toList()
        scopes
            .map { it.async { println("running job") } }
            .forEach { it.await(); println("completed job") }
        println("test completed - 1")
        scopes.forEach { it.cancel() }
        println("test completed")
    }
Interestingly, this also produces the problem. Possibly a strange interaction with mocha and js coroutines. when the problem happens, it looks like the job runs, completes the lambda, but never finishes, and this causes everything to hang. That's probably enough investigation for me into this curiosity for now
t
> it looks like the job runs, completes the lambda, but never finishes I see only scopes 🙂
r
yeah when it fails it never seems to get to the 'completed job' line
when it succeeds (by turning down the scope number) its all smooth and fairly fast
makes me wonder if coroutines has a throttle or some such that has a hard time limit that blows past the default mocha limit
i'm off to other things now but another thing to try would be changing the mocha time limit to see if it finishes or not
t
What if use
CoroutineScope(EmptyCoroutineContext)
?
r
AFAICT it has the same effect
t
Instead of
GlobalScope
r
but give it a shot
I know runTest makes its own scope as well and still has the problem