I'm a bit confused on the expected behavior of for...
# coroutines
c
I'm a bit confused on the expected behavior of foreground and background scopes in kotlinx-coroutines-test. My understanding was that a coroutine running in the foreground scope (the
TestScope
itself): • is waited on by the test (the test only finishes after they have all run) • is printed in the dump if the timeout is reached before it finishes, and marks the test as failed and a coroutine running the background scope (
TestScope.backgroundScope
): • is not waited on by the test • is killed when the test finishes However, after testing a few things, most of this seems incorrect. What I see: • when the test function returns, all other coroutines are dumped, no matter in which scope they were launched • the test ends right then, even if other foreground coroutines are still active Is this the expected behavior? Is the difference between foreground and background scopes documented anywhere? https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-scope/background-scope.html mentions: > Failures in coroutines in this scope do not terminate the test. Instead, they are reported at the end of the test. It doesn't mention normal termination. I'm surprised that the test just ends successfully and dumps the coroutines when there are still coroutines running in the foreground scope. I would have expected either the test to wait for all foreground tasks, or be marked as failed because it finished because all work executed. Is the difference just about error reporting?
a
WDYM? In my experience, if there are foreground tasks when the test ends,
runTest
waits for them: https://pl.kotl.in/3W1_URKzW Do you have an example?
c
I do have an example, but it's deep into another project and hard to extract. But thanks, your example does confirm that something weird is going on.
s
Have you looked into KotlinX Coroutines Debug to debug @CLOVIS? It should be enabled by default in
runTest
, and provide a coroutine dump when timing out due to a hanging coroutine. It's really convenient to figure out why or what is hanging in the background.
c
I do have it enabled, and it does print the dump of the coroutine I launched, but... I launched it in the foreground scope, so the test shouldn't have finished before it, no?
a
c
In my case, it is suspended on a
delay
(according to the dump). It was started with
TestScope.launch
.
c
Always a good idea to create a simple reproducer. in my case a lot of the time I then find out that in isolation it acts totally different than I thought and its my fault.
👍 1
c
Ah, this seems to have been a Kotest bug. Reported.
👀 1