blob: 72ec5197a7816c1d82d063d28807536f462cc854 [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
Roman Elizarov41c5c8b2017-01-25 13:37:15 +030017package kotlinx.coroutines.experimental
18
Roman Elizarov9fe5f462018-02-21 19:05:52 +030019import kotlin.coroutines.experimental.*
Roman Elizarove3f28842017-12-21 19:15:40 +030020import kotlin.test.*
Roman Elizarov41c5c8b2017-01-25 13:37:15 +030021
Roman Elizarovaa461cf2018-04-11 13:20:29 +030022class AsyncJvmTest : TestBase() {
Roman Elizarove3f28842017-12-21 19:15:40 +030023 // This must be a common test but it fails on JS because of KT-21961
Roman Elizarov41c5c8b2017-01-25 13:37:15 +030024 @Test
Roman Elizarove3f28842017-12-21 19:15:40 +030025 fun testAsyncWithFinally() = runTest {
Roman Elizarovd82b3a92017-06-23 21:52:08 +030026 expect(1)
Vsevolod Tolstopyatov87f2faa2018-04-30 22:53:02 +030027
28 @Suppress("UNREACHABLE_CODE")
29 val d = async(coroutineContext) {
Roman Elizarovd82b3a92017-06-23 21:52:08 +030030 expect(3)
31 try {
32 yield() // to main, will cancel
33 } finally {
34 expect(6) // will go there on await
35 return@async "Fail" // result will not override cancellation
36 }
37 expectUnreached()
38 "Fail2"
39 }
40 expect(2)
41 yield() // to async
42 expect(4)
43 check(d.isActive && !d.isCompleted && !d.isCompletedExceptionally && !d.isCancelled)
44 check(d.cancel())
45 check(!d.isActive && !d.isCompleted && !d.isCompletedExceptionally && d.isCancelled)
46 check(!d.cancel()) // second attempt returns false
47 check(!d.isActive && !d.isCompleted && !d.isCompletedExceptionally && d.isCancelled)
48 expect(5)
49 try {
50 d.await() // awaits
51 expectUnreached() // does not complete normally
52 } catch (e: Throwable) {
53 expect(7)
54 check(e is CancellationException)
55 }
56 check(!d.isActive && d.isCompleted && d.isCompletedExceptionally && d.isCancelled)
57 finish(8)
58 }
Roman Elizarov41c5c8b2017-01-25 13:37:15 +030059}