diff options
Diffstat (limited to 'kotlinx-coroutines-core/common/test/CoroutineExceptionHandlerTest.kt')
-rw-r--r-- | kotlinx-coroutines-core/common/test/CoroutineExceptionHandlerTest.kt | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/kotlinx-coroutines-core/common/test/CoroutineExceptionHandlerTest.kt b/kotlinx-coroutines-core/common/test/CoroutineExceptionHandlerTest.kt new file mode 100644 index 00000000..95e93664 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/CoroutineExceptionHandlerTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlin.test.* + +class CoroutineExceptionHandlerTest : TestBase() { + // Parent Job() does not handle exception --> handler is invoked on child crash + @Test + fun testJob() = runTest { + expect(1) + var coroutineException: Throwable? = null + val handler = CoroutineExceptionHandler { _, ex -> + coroutineException = ex + expect(3) + } + val parent = Job() + val job = launch(handler + parent) { + throw TestException() + } + expect(2) + job.join() + finish(4) + assertTrue(coroutineException is TestException) + assertTrue(parent.isCancelled) + } + + // Parent CompletableDeferred() "handles" exception --> handler is NOT invoked on child crash + @Test + fun testCompletableDeferred() = runTest { + expect(1) + val handler = CoroutineExceptionHandler { _, _ -> + expectUnreached() + } + val parent = CompletableDeferred<Unit>() + val job = launch(handler + parent) { + throw TestException() + } + expect(2) + job.join() + finish(3) + assertTrue(parent.isCancelled) + assertTrue(parent.getCompletionExceptionOrNull() is TestException) + } +} |