blob: 5284725a1b6f1dcb13f1fb05450260d315306be2 [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov331750b2017-02-15 17:59:17 +03003 */
4
5package kotlinx.coroutines.experimental.rx2
6
7import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03008import kotlinx.coroutines.experimental.channels.*
9import org.junit.*
10import org.junit.Assert.*
Roman Elizarov331750b2017-02-15 17:59:17 +030011
12class ConvertTest : TestBase() {
13 class TestException(s: String): RuntimeException(s)
14
15 @Test
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +030016 fun testToCompletableSuccess() = runBlocking {
Roman Elizarov331750b2017-02-15 17:59:17 +030017 expect(1)
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +030018 val job = launch {
Roman Elizarov331750b2017-02-15 17:59:17 +030019 expect(3)
20 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030021 val completable = job.asCompletable(coroutineContext)
Roman Elizarov331750b2017-02-15 17:59:17 +030022 completable.subscribe {
23 expect(4)
24 }
25 expect(2)
26 yield()
27 finish(5)
28 }
29
30 @Test
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +030031 fun testToCompletableFail() = runBlocking {
Roman Elizarov331750b2017-02-15 17:59:17 +030032 expect(1)
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +030033 val job = async(NonCancellable) { // don't kill parent on exception
Roman Elizarov331750b2017-02-15 17:59:17 +030034 expect(3)
35 throw RuntimeException("OK")
36 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030037 val completable = job.asCompletable(coroutineContext)
Roman Elizarov331750b2017-02-15 17:59:17 +030038 completable.subscribe {
39 expect(4)
40 }
41 expect(2)
42 yield()
43 finish(5)
44 }
45
46 @Test
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020047 fun testToMaybe() {
Roman Elizarovc32579e2018-09-09 19:21:43 +030048 val d = GlobalScope.async {
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020049 delay(50)
50 "OK"
51 }
52 val maybe1 = d.asMaybe(Unconfined)
53 checkMaybeValue(maybe1) {
54 assertEquals("OK", it)
55 }
56 val maybe2 = d.asMaybe(Unconfined)
57 checkMaybeValue(maybe2) {
58 assertEquals("OK", it)
59 }
60 }
61
62 @Test
63 fun testToMaybeEmpty() {
Roman Elizarovc32579e2018-09-09 19:21:43 +030064 val d = GlobalScope.async {
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020065 delay(50)
66 null
67 }
68 val maybe1 = d.asMaybe(Unconfined)
69 checkMaybeValue(maybe1, ::assertNull)
70 val maybe2 = d.asMaybe(Unconfined)
71 checkMaybeValue(maybe2, ::assertNull)
72 }
73
74 @Test
75 fun testToMaybeFail() {
Roman Elizarovc32579e2018-09-09 19:21:43 +030076 val d = GlobalScope.async {
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020077 delay(50)
78 throw TestException("OK")
79 }
80 val maybe1 = d.asMaybe(Unconfined)
81 checkErroneous(maybe1) {
82 check(it is TestException && it.message == "OK") { "$it" }
83 }
84 val maybe2 = d.asMaybe(Unconfined)
85 checkErroneous(maybe2) {
86 check(it is TestException && it.message == "OK") { "$it" }
87 }
88 }
89
90 @Test
Roman Elizarov331750b2017-02-15 17:59:17 +030091 fun testToSingle() {
Roman Elizarovc32579e2018-09-09 19:21:43 +030092 val d = GlobalScope.async {
Roman Elizarov331750b2017-02-15 17:59:17 +030093 delay(50)
94 "OK"
95 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +030096 val single1 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +030097 checkSingleValue(single1) {
98 assertEquals("OK", it)
99 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300100 val single2 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300101 checkSingleValue(single2) {
102 assertEquals("OK", it)
103 }
104 }
105
106 @Test
107 fun testToSingleFail() {
Roman Elizarovc32579e2018-09-09 19:21:43 +0300108 val d = GlobalScope.async {
Roman Elizarov331750b2017-02-15 17:59:17 +0300109 delay(50)
110 throw TestException("OK")
111 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300112 val single1 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300113 checkErroneous(single1) {
114 check(it is TestException && it.message == "OK") { "$it" }
115 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300116 val single2 = d.asSingle(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300117 checkErroneous(single2) {
118 check(it is TestException && it.message == "OK") { "$it" }
119 }
120 }
121
122 @Test
123 fun testToObservable() {
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +0300124 val c = GlobalScope.produce {
Roman Elizarov331750b2017-02-15 17:59:17 +0300125 delay(50)
126 send("O")
127 delay(50)
128 send("K")
129 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300130 val observable = c.asObservable(Unconfined)
Roman Elizarov331750b2017-02-15 17:59:17 +0300131 checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) {
132 assertEquals("OK", it)
133 }
134 }
135
136 @Test
137 fun testToObservableFail() {
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +0300138 val c = GlobalScope.produce {
Roman Elizarov331750b2017-02-15 17:59:17 +0300139 delay(50)
140 send("O")
141 delay(50)
142 throw TestException("K")
143 }
Roman Elizarov3c3aed72017-03-09 12:31:59 +0300144 val observable = c.asObservable(Unconfined)
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +0300145 val single = GlobalScope.rxSingle(Unconfined) {
Roman Elizarov331750b2017-02-15 17:59:17 +0300146 var result = ""
147 try {
Roman Elizarov7adb8762017-03-17 17:54:13 +0300148 observable.consumeEach { result += it }
Roman Elizarov331750b2017-02-15 17:59:17 +0300149 } catch(e: Throwable) {
150 check(e is TestException)
151 result += e.message
152 }
153 result
154 }
155 checkSingleValue(single) {
156 assertEquals("OK", it)
157 }
158 }
159}