Roman Elizarov | 331750b | 2017-02-15 17:59:17 +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.rx2 |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlinx.coroutines.experimental.channels.produce |
| 21 | import org.junit.Assert.assertEquals |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame^] | 22 | import org.junit.Assert.assertNull |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 23 | import org.junit.Test |
| 24 | |
| 25 | class ConvertTest : TestBase() { |
| 26 | class TestException(s: String): RuntimeException(s) |
| 27 | |
| 28 | @Test |
| 29 | fun testToCompletableSuccess() = runBlocking<Unit> { |
| 30 | expect(1) |
| 31 | val job = launch(context) { |
| 32 | expect(3) |
| 33 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 34 | val completable = job.asCompletable(context) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 35 | 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) |
| 46 | val job = async(context + NonCancellable) { // don't kill parent on exception |
| 47 | expect(3) |
| 48 | throw RuntimeException("OK") |
| 49 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 50 | val completable = job.asCompletable(context) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 51 | completable.subscribe { |
| 52 | expect(4) |
| 53 | } |
| 54 | expect(2) |
| 55 | yield() |
| 56 | finish(5) |
| 57 | } |
| 58 | |
| 59 | @Test |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame^] | 60 | 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 Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 104 | fun testToSingle() { |
| 105 | val d = async(CommonPool) { |
| 106 | delay(50) |
| 107 | "OK" |
| 108 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 109 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 110 | checkSingleValue(single1) { |
| 111 | assertEquals("OK", it) |
| 112 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 113 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 114 | 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 Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 125 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 126 | checkErroneous(single1) { |
| 127 | check(it is TestException && it.message == "OK") { "$it" } |
| 128 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 129 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 130 | 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 Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 143 | val observable = c.asObservable(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 144 | 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 Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 157 | val observable = c.asObservable(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 158 | val single = rxSingle(Unconfined) { |
| 159 | var result = "" |
| 160 | try { |
Roman Elizarov | 7adb876 | 2017-03-17 17:54:13 +0300 | [diff] [blame] | 161 | observable.consumeEach { result += it } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 162 | } 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 | } |