blob: becd2c7fe6204cc86ee3b01035b5ed2b2cb69a7b [file] [log] [blame]
Roman Elizarova7db8ec2017-12-21 22:45:12 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020017package kotlinx.coroutines.experimental
18
Roman Elizarov9fe5f462018-02-21 19:05:52 +030019import kotlin.coroutines.experimental.*
Roman Elizarov9d5abcd2017-12-21 16:54:30 +030020import kotlin.test.*
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020021
Roman Elizarovaa461cf2018-04-11 13:20:29 +030022class CoroutineExceptionHandlerTest : TestBase() {
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020023 @Test
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030024 fun testCoroutineExceptionHandlerCreator() = runTest {
Roman Elizarov01d41252017-06-09 16:55:40 +030025 expect(1)
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020026 var coroutineException: Throwable? = null
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020027 val handler = CoroutineExceptionHandler { _, ex ->
28 coroutineException = ex
Roman Elizarov01d41252017-06-09 16:55:40 +030029 expect(3)
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020030 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030031 val job = launch(coroutineContext + handler) {
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020032 throw TestException()
33 }
Roman Elizarov01d41252017-06-09 16:55:40 +030034 expect(2)
35 job.join()
36 finish(4)
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030037 assertTrue(coroutineException is TestException)
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020038 }
Konrad Kamiński5667bcf2017-05-30 15:34:03 +020039
Roman Elizarov9d5abcd2017-12-21 16:54:30 +030040 private class TestException: RuntimeException()
41}