blob: bafb2eb9b5248be67ecaa0bfccda54453c5910b5 [file] [log] [blame]
Roman Elizarovebc88662018-01-24 23:58:56 +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
17package kotlinx.coroutines.experimental
18
Roman Elizarov9fe5f462018-02-21 19:05:52 +030019import kotlin.coroutines.experimental.*
Roman Elizarovebc88662018-01-24 23:58:56 +030020import kotlin.test.*
21
22class AbstractCoroutineTest : TestBase() {
23 @Test
24 fun testNotifications() = runTest {
25 expect(1)
Roman Elizarov9fe5f462018-02-21 19:05:52 +030026 val coroutineContext = coroutineContext // workaround for KT-22984
Roman Elizarovebc88662018-01-24 23:58:56 +030027 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 Elizarov9da9e352018-01-25 10:06:39 +030046 coroutine.invokeOnCompletion(onCancelling = true) {
Roman Elizarovebc88662018-01-24 23:58:56 +030047 assertTrue(it == null)
48 expect(7)
49 }
Roman Elizarov9da9e352018-01-25 10:06:39 +030050 coroutine.invokeOnCompletion {
51 assertTrue(it == null)
52 expect(8)
53 }
Roman Elizarovebc88662018-01-24 23:58:56 +030054 expect(2)
55 coroutine.start()
56 expect(4)
57 coroutine.resume("OK")
Roman Elizarov9da9e352018-01-25 10:06:39 +030058 finish(9)
Roman Elizarovebc88662018-01-24 23:58:56 +030059 }
60
61 @Test
62 fun testNotificationsWithException() = runTest {
63 expect(1)
Roman Elizarov9fe5f462018-02-21 19:05:52 +030064 val coroutineContext = coroutineContext // workaround for KT-22984
Roman Elizarovebc88662018-01-24 23:58:56 +030065 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 Elizarov9da9e352018-01-25 10:06:39 +030081 expect(8)
Roman Elizarovebc88662018-01-24 23:58:56 +030082 }
83 }
Roman Elizarov9da9e352018-01-25 10:06:39 +030084 coroutine.invokeOnCompletion(onCancelling = true) {
85 assertTrue(it is TestException0)
86 expect(6)
87 }
Roman Elizarovebc88662018-01-24 23:58:56 +030088 coroutine.invokeOnCompletion {
89 assertTrue(it is TestException1)
Roman Elizarov9da9e352018-01-25 10:06:39 +030090 expect(9)
Roman Elizarovebc88662018-01-24 23:58:56 +030091 }
92 expect(2)
93 coroutine.start()
94 expect(4)
95 coroutine.cancel(TestException0())
Roman Elizarov9da9e352018-01-25 10:06:39 +030096 expect(7)
Roman Elizarovebc88662018-01-24 23:58:56 +030097 coroutine.resumeWithException(TestException1())
Roman Elizarov9da9e352018-01-25 10:06:39 +030098 finish(10)
Roman Elizarovebc88662018-01-24 23:58:56 +030099 }
100
101 private class TestException0 : Throwable()
102 private class TestException1 : Throwable()
103}