Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 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 | |
| 5 | package kotlinx.coroutines.experimental.rx2 |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.channels.* |
| 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() { |
| 13 | class TestException(s: String): RuntimeException(s) |
| 14 | |
| 15 | @Test |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 16 | fun testToCompletableSuccess() = runBlocking { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 17 | expect(1) |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 18 | val job = launch { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 19 | expect(3) |
| 20 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 21 | val completable = job.asCompletable(coroutineContext) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 22 | completable.subscribe { |
| 23 | expect(4) |
| 24 | } |
| 25 | expect(2) |
| 26 | yield() |
| 27 | finish(5) |
| 28 | } |
| 29 | |
| 30 | @Test |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 31 | fun testToCompletableFail() = runBlocking { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 32 | expect(1) |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 33 | val job = async(NonCancellable) { // don't kill parent on exception |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 34 | expect(3) |
| 35 | throw RuntimeException("OK") |
| 36 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 37 | val completable = job.asCompletable(coroutineContext) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 38 | completable.subscribe { |
| 39 | expect(4) |
| 40 | } |
| 41 | expect(2) |
| 42 | yield() |
| 43 | finish(5) |
| 44 | } |
| 45 | |
| 46 | @Test |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 47 | fun testToMaybe() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 48 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 49 | 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 Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 64 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 65 | 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 Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 76 | val d = GlobalScope.async { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 77 | 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 Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 91 | fun testToSingle() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 92 | val d = GlobalScope.async { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 93 | delay(50) |
| 94 | "OK" |
| 95 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 96 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 97 | checkSingleValue(single1) { |
| 98 | assertEquals("OK", it) |
| 99 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 100 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 101 | checkSingleValue(single2) { |
| 102 | assertEquals("OK", it) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | fun testToSingleFail() { |
Roman Elizarov | c32579e | 2018-09-09 19:21:43 +0300 | [diff] [blame] | 108 | val d = GlobalScope.async { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 109 | delay(50) |
| 110 | throw TestException("OK") |
| 111 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 112 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 113 | checkErroneous(single1) { |
| 114 | check(it is TestException && it.message == "OK") { "$it" } |
| 115 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 116 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 117 | checkErroneous(single2) { |
| 118 | check(it is TestException && it.message == "OK") { "$it" } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun testToObservable() { |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 124 | val c = GlobalScope.produce { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 125 | delay(50) |
| 126 | send("O") |
| 127 | delay(50) |
| 128 | send("K") |
| 129 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 130 | val observable = c.asObservable(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 131 | checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) { |
| 132 | assertEquals("OK", it) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | fun testToObservableFail() { |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 138 | val c = GlobalScope.produce { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 139 | delay(50) |
| 140 | send("O") |
| 141 | delay(50) |
| 142 | throw TestException("K") |
| 143 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 144 | val observable = c.asObservable(Unconfined) |
Vsevolod Tolstopyatov | bbaf99d | 2018-09-11 15:55:56 +0300 | [diff] [blame^] | 145 | val single = GlobalScope.rxSingle(Unconfined) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 146 | var result = "" |
| 147 | try { |
Roman Elizarov | 7adb876 | 2017-03-17 17:54:13 +0300 | [diff] [blame] | 148 | observable.consumeEach { result += it } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 149 | } 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 | } |