do I need to cancel my coroutine scopes in tests i...
# coroutines
u
do I need to cancel my coroutine scopes in tests if it seems the test process gets killed? or does it?
n
Not really sure what you mean. Do you have a simple repro that demonstrates the issue?
u
Copy code
@Test 
fun foo() {
	GlobalScope.launch {
		intervalFlow(10)
	        .collect {
	            log("---- INTERVAL=$it")
	        }
    }
}
why does the flow stops, it should continue for ever, i.e. a leak, right?
n
coroutines are not threads, they will not keep your process alive.
The thread running
foo()
returns right away (
launch
starts the coroutine but then returns immediately). Once it's done, process ends.
If you want the process to live on, then you need to force a non-daemon thread to wait:
Copy code
@Test 
fun foo() {
	runBlocking { //This blocks test thread
		intervalFlow(10)
	        .collect {
	            log("---- INTERVAL=$it")
	        }
    }
}
u
I dont want it to wait, im just studying how it works. Also, if I do pure thread it stops.. which means I think it got gced, since other tests run afterwards, so I assume the process didnt get killed per test
n
Non-daemon threads are GC roots. They can't be GC'd and the process will never exit while one is still running.
I haven't come across a unit testing framework that creates a process for each test execution. This means that any threads or coroutines you create in one test could still be running when other tests run if you don't clean up and wait for them.
u
that makes sense, but they dont, printlns stop