Hello, I'm getting a weird behavior when using cor...
# coroutines
f
Hello, I'm getting a weird behavior when using coroutines in Gradle tests (junit). My goal is simple, to make exceptions thrown in a coroutine terminate the application (via an unhandled exception, not with exitProcess()) so the test will be marked as failed. However, when I do this:
Copy code
@Test
    fun tryToThrow1() {
        GlobalScope.launch {
            throw RuntimeException()
        }
        Thread.sleep(500)
    }
it logs the exception to the console instead of throwing normally https://paste.ee/p/reWaF . Adding a coroutine exception handler does the same (https://paste.ee/p/Lgjri):
Copy code
@Test
    fun tryToThrow2() {
        GlobalScope.launch(CoroutineExceptionHandler { _, throwable -> throw throwable }) {
            throw RuntimeException()
        }
        Thread.sleep(500)
    }
Trying to further overload the thread UncaughtExceptionHandler, completely breaks Coroutines and makes it log a single line instead (instead of just throwing, like I want it to and it should)
Copy code
@Test
fun tryToThrow3() {
    GlobalScope.launch(CoroutineExceptionHandler { _, throwable -> throw throwable }) {
        Thread.setDefaultUncaughtExceptionHandler { _, e -> throw e }
        throw RuntimeException()
    }
    Thread.sleep(1000)
}
Exception: kotlinx.coroutines.CoroutinesInternalError thrown from the UncaughtExceptionHandler in thread "DefaultDispatcher-worker-1"