Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 1 | /* |
| 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 Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 19 | import kotlin.coroutines.experimental.* |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 20 | import kotlin.test.* |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 21 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 22 | class AsyncJvmTest : TestBase() { |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 23 | // This must be a common test but it fails on JS because of KT-21961 |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 24 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 25 | fun testAsyncWithFinally() = runTest { |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 26 | expect(1) |
Vsevolod Tolstopyatov | 87f2faa | 2018-04-30 22:53:02 +0300 | [diff] [blame] | 27 | |
| 28 | @Suppress("UNREACHABLE_CODE") |
| 29 | val d = async(coroutineContext) { |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 30 | 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 Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 59 | } |