Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Vsevolod Tolstopyatov | 39f5cf8 | 2019-06-28 16:47:30 +0300 | [diff] [blame] | 2 | * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.rx2 |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.* |
| 8 | import kotlinx.coroutines.channels.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 9 | import org.junit.* |
| 10 | import org.junit.Assert.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 11 | |
| 12 | class ConvertTest : TestBase() { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 13 | @Test |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 14 | fun testToCompletableSuccess() = runBlocking { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 15 | expect(1) |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 16 | val job = launch { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 17 | expect(3) |
| 18 | } |
Vsevolod Tolstopyatov | d100a3f | 2019-07-16 16:14:35 +0300 | [diff] [blame] | 19 | val completable = job.asCompletable(coroutineContext.minusKey(Job)) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 20 | completable.subscribe { |
| 21 | expect(4) |
| 22 | } |
| 23 | expect(2) |
| 24 | yield() |
| 25 | finish(5) |
| 26 | } |
| 27 | |
| 28 | @Test |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 29 | fun testToCompletableFail() = runBlocking { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 30 | expect(1) |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 31 | val job = async(NonCancellable) { // don't kill parent on exception |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 32 | expect(3) |
| 33 | throw RuntimeException("OK") |
| 34 | } |
Vsevolod Tolstopyatov | d100a3f | 2019-07-16 16:14:35 +0300 | [diff] [blame] | 35 | val completable = job.asCompletable(coroutineContext.minusKey(Job)) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 36 | completable.subscribe { |
| 37 | expect(4) |
| 38 | } |
| 39 | expect(2) |
| 40 | yield() |
| 41 | finish(5) |
| 42 | } |
| 43 | |
| 44 | @Test |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 45 | fun testToMaybe() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 46 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 47 | delay(50) |
| 48 | "OK" |
| 49 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 50 | val maybe1 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 51 | checkMaybeValue(maybe1) { |
| 52 | assertEquals("OK", it) |
| 53 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 54 | val maybe2 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 55 | checkMaybeValue(maybe2) { |
| 56 | assertEquals("OK", it) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | fun testToMaybeEmpty() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 62 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 63 | delay(50) |
| 64 | null |
| 65 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 66 | val maybe1 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 67 | checkMaybeValue(maybe1, ::assertNull) |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 68 | val maybe2 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 69 | checkMaybeValue(maybe2, ::assertNull) |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | fun testToMaybeFail() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 74 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 75 | delay(50) |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 76 | throw TestRuntimeException("OK") |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 77 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 78 | val maybe1 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 79 | checkErroneous(maybe1) { |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 80 | check(it is TestRuntimeException && it.message == "OK") { "$it" } |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 81 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 82 | val maybe2 = d.asMaybe(Dispatchers.Unconfined) |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 83 | checkErroneous(maybe2) { |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 84 | check(it is TestRuntimeException && it.message == "OK") { "$it" } |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | @Test |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 89 | fun testToSingle() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 90 | val d = GlobalScope.async { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 91 | delay(50) |
| 92 | "OK" |
| 93 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 94 | val single1 = d.asSingle(Dispatchers.Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 95 | checkSingleValue(single1) { |
| 96 | assertEquals("OK", it) |
| 97 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 98 | val single2 = d.asSingle(Dispatchers.Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 99 | checkSingleValue(single2) { |
| 100 | assertEquals("OK", it) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun testToSingleFail() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 106 | val d = GlobalScope.async { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 107 | delay(50) |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 108 | throw TestRuntimeException("OK") |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 109 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 110 | val single1 = d.asSingle(Dispatchers.Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 111 | checkErroneous(single1) { |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 112 | check(it is TestRuntimeException && it.message == "OK") { "$it" } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 113 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 114 | val single2 = d.asSingle(Dispatchers.Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 115 | checkErroneous(single2) { |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 116 | check(it is TestRuntimeException && it.message == "OK") { "$it" } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun testToObservable() { |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 122 | val c = GlobalScope.produce { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 123 | delay(50) |
| 124 | send("O") |
| 125 | delay(50) |
| 126 | send("K") |
| 127 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 128 | val observable = c.asObservable(Dispatchers.Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 129 | checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) { |
| 130 | assertEquals("OK", it) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | fun testToObservableFail() { |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame] | 136 | val c = GlobalScope.produce { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 137 | delay(50) |
| 138 | send("O") |
| 139 | delay(50) |
| 140 | throw TestException("K") |
| 141 | } |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 142 | val observable = c.asObservable(Dispatchers.Unconfined) |
Vsevolod Tolstopyatov | d100a3f | 2019-07-16 16:14:35 +0300 | [diff] [blame] | 143 | val single = rxSingle(Dispatchers.Unconfined) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 144 | var result = "" |
| 145 | try { |
Vsevolod Tolstopyatov | 97863c3 | 2019-07-17 17:02:14 +0300 | [diff] [blame^] | 146 | observable.collect { result += it } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 147 | } catch(e: Throwable) { |
| 148 | check(e is TestException) |
| 149 | result += e.message |
| 150 | } |
| 151 | result |
| 152 | } |
| 153 | checkSingleValue(single) { |
| 154 | assertEquals("OK", it) |
| 155 | } |
| 156 | } |
| 157 | } |