blob: d3315a296ff9194905a0f2041d325c09509cd6d4 [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +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.rx2
18
19import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030020import kotlinx.coroutines.experimental.channels.*
21import org.junit.*
22import org.junit.Assert.*
23import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030024
25class ConvertTest : TestBase() {
26 class TestException(s: String): RuntimeException(s)
27
28 @Test
29 fun testToCompletableSuccess() = runBlocking<Unit> {
30 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030031 val job = launch(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030032 expect(3)
33 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030034 val completable = job.asCompletable(coroutineContext)
Roman Elizarov331750b2017-02-15 17:59:17 +030035 completable.subscribe {
36 expect(4)
37 }
38 expect(2)
39 yield()
40 finish(5)
41 }
42
43 @Test
44 fun testToCompletableFail() = runBlocking<Unit> {
45 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030046 val job = async(coroutineContext + NonCancellable) { // don't kill parent on exception
Roman Elizarov331750b2017-02-15 17:59:17 +030047 expect(3)
48 throw RuntimeException("OK")
49 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030050 val completable = job.asCompletable(coroutineContext)
Roman Elizarov331750b2017-02-15 17:59:17 +030051 completable.subscribe {
52 expect(4)
53 }
54 expect(2)
55 yield()
56 finish(5)
57 }
58
59 @Test
Konrad KamiƄskid6bb1482017-04-07 09:26:40 +020060 fun testToMaybe() {
61 val d = async(CommonPool) {
62 delay(50)
63 "OK"
64 }
65 val maybe1 = d.asMaybe(Unconfined)
66 checkMaybeValue(maybe1) {
67 assertEquals("OK", it)
68 }
69 val maybe2 = d.asMaybe(Unconfined)
70 checkMaybeValue(maybe2) {
71 assertEquals("OK", it)
72 }
73 }
74
75 @Test
76 fun testToMaybeEmpty() {
77 val d = async(CommonPool) {
78 delay(50)
79 null
80 }
81 val maybe1 = d.asMaybe(Unconfined)
82 checkMaybeValue(maybe1, ::assertNull)
83 val maybe2 = d.asMaybe(Unconfined)
84 checkMaybeValue(maybe2, ::assertNull)
85 }
86
87 @Test
88 fun testToMaybeFail() {
89 val d = async(CommonPool) {
90 delay(50)
91 throw TestException("OK")
92 }
93 val maybe1 = d.asMaybe(Unconfined)
94 checkErroneous(maybe1) {
95 check(it is TestException && it.message == "OK") { "$it" }
96 }
97 val maybe2 = d.asMaybe(Unconfined)
98 checkErroneous(maybe2) {
99 check(it is TestException && it.message == "OK") { "$it" }
100 }
101 }
102
103 @Test
Roman Elizarov331750b2017-02-15 17:59:17 +0300104 fun testToSingle() {
105 val d = async(CommonPool) {
106 delay(50)
107 "OK"
108 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300109 val single1 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300110 checkSingleValue(single1) {
111 assertEquals("OK", it)
112 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300113 val single2 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300114 checkSingleValue(single2) {
115 assertEquals("OK", it)
116 }
117 }
118
119 @Test
120 fun testToSingleFail() {
121 val d = async(CommonPool) {
122 delay(50)
123 throw TestException("OK")
124 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300125 val single1 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300126 checkErroneous(single1) {
127 check(it is TestException && it.message == "OK") { "$it" }
128 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300129 val single2 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300130 checkErroneous(single2) {
131 check(it is TestException && it.message == "OK") { "$it" }
132 }
133 }
134
135 @Test
136 fun testToObservable() {
137 val c = produce(CommonPool) {
138 delay(50)
139 send("O")
140 delay(50)
141 send("K")
142 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300143 val observable = c.asObservable(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300144 checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) {
145 assertEquals("OK", it)
146 }
147 }
148
149 @Test
150 fun testToObservableFail() {
151 val c = produce(CommonPool) {
152 delay(50)
153 send("O")
154 delay(50)
155 throw TestException("K")
156 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300157 val observable = c.asObservable(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300158 val single = rxSingle(Unconfined) {
159 var result = ""
160 try {
Roman Elizarov7adb8762017-03-17 17:54:13 +0300161 observable.consumeEach { result += it }
Roman Elizarov331750b2017-02-15 17:59:17 +0300162 } catch(e: Throwable) {
163 check(e is TestException)
164 result += e.message
165 }
166 result
167 }
168 checkSingleValue(single) {
169 assertEquals("OK", it)
170 }
171 }
172}