Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +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 | |
| 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 | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 20 | import kotlin.test.* |
| 21 | |
| 22 | class AbstractCoroutineTest : TestBase() { |
| 23 | @Test |
| 24 | fun testNotifications() = runTest { |
| 25 | expect(1) |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 26 | val coroutineContext = coroutineContext // workaround for KT-22984 |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 27 | val coroutine = object : AbstractCoroutine<String>(coroutineContext, false) { |
| 28 | override fun onStart() { |
| 29 | expect(3) |
| 30 | } |
| 31 | |
| 32 | override fun onCancellation(cause: Throwable?) { |
| 33 | assertTrue(cause == null) |
| 34 | expect(5) |
| 35 | } |
| 36 | |
| 37 | override fun onCompleted(value: String) { |
| 38 | assertEquals("OK", value) |
| 39 | expect(6) |
| 40 | } |
| 41 | |
| 42 | override fun onCompletedExceptionally(exception: Throwable) { |
| 43 | expectUnreached() |
| 44 | } |
| 45 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 46 | coroutine.invokeOnCompletion(onCancelling = true) { |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 47 | assertTrue(it == null) |
| 48 | expect(7) |
| 49 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 50 | coroutine.invokeOnCompletion { |
| 51 | assertTrue(it == null) |
| 52 | expect(8) |
| 53 | } |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 54 | expect(2) |
| 55 | coroutine.start() |
| 56 | expect(4) |
| 57 | coroutine.resume("OK") |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 58 | finish(9) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun testNotificationsWithException() = runTest { |
| 63 | expect(1) |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 64 | val coroutineContext = coroutineContext // workaround for KT-22984 |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 65 | val coroutine = object : AbstractCoroutine<String>(coroutineContext, false) { |
| 66 | override fun onStart() { |
| 67 | expect(3) |
| 68 | } |
| 69 | |
| 70 | override fun onCancellation(cause: Throwable?) { |
| 71 | assertTrue(cause is TestException0) |
| 72 | expect(5) |
| 73 | } |
| 74 | |
| 75 | override fun onCompleted(value: String) { |
| 76 | expectUnreached() |
| 77 | } |
| 78 | |
| 79 | override fun onCompletedExceptionally(exception: Throwable) { |
| 80 | assertTrue(exception is TestException1) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 81 | expect(8) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 82 | } |
| 83 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 84 | coroutine.invokeOnCompletion(onCancelling = true) { |
| 85 | assertTrue(it is TestException0) |
| 86 | expect(6) |
| 87 | } |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 88 | coroutine.invokeOnCompletion { |
| 89 | assertTrue(it is TestException1) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 90 | expect(9) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 91 | } |
| 92 | expect(2) |
| 93 | coroutine.start() |
| 94 | expect(4) |
| 95 | coroutine.cancel(TestException0()) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 96 | expect(7) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 97 | coroutine.resumeWithException(TestException1()) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 98 | finish(10) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private class TestException0 : Throwable() |
| 102 | private class TestException1 : Throwable() |
| 103 | } |