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.* |
| 11 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 12 | |
| 13 | class ConvertTest : TestBase() { |
| 14 | class TestException(s: String): RuntimeException(s) |
| 15 | |
| 16 | @Test |
| 17 | fun testToCompletableSuccess() = runBlocking<Unit> { |
| 18 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 19 | val job = launch(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 20 | expect(3) |
| 21 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 22 | val completable = job.asCompletable(coroutineContext) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 23 | completable.subscribe { |
| 24 | expect(4) |
| 25 | } |
| 26 | expect(2) |
| 27 | yield() |
| 28 | finish(5) |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | fun testToCompletableFail() = runBlocking<Unit> { |
| 33 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 34 | val job = async(coroutineContext + NonCancellable) { // don't kill parent on exception |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 35 | expect(3) |
| 36 | throw RuntimeException("OK") |
| 37 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 38 | val completable = job.asCompletable(coroutineContext) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 39 | completable.subscribe { |
| 40 | expect(4) |
| 41 | } |
| 42 | expect(2) |
| 43 | yield() |
| 44 | finish(5) |
| 45 | } |
| 46 | |
| 47 | @Test |
Konrad KamiĆski | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 48 | fun testToMaybe() { |
| 49 | val d = async(CommonPool) { |
| 50 | delay(50) |
| 51 | "OK" |
| 52 | } |
| 53 | val maybe1 = d.asMaybe(Unconfined) |
| 54 | checkMaybeValue(maybe1) { |
| 55 | assertEquals("OK", it) |
| 56 | } |
| 57 | val maybe2 = d.asMaybe(Unconfined) |
| 58 | checkMaybeValue(maybe2) { |
| 59 | assertEquals("OK", it) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | fun testToMaybeEmpty() { |
| 65 | val d = async(CommonPool) { |
| 66 | delay(50) |
| 67 | null |
| 68 | } |
| 69 | val maybe1 = d.asMaybe(Unconfined) |
| 70 | checkMaybeValue(maybe1, ::assertNull) |
| 71 | val maybe2 = d.asMaybe(Unconfined) |
| 72 | checkMaybeValue(maybe2, ::assertNull) |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun testToMaybeFail() { |
| 77 | val d = async(CommonPool) { |
| 78 | delay(50) |
| 79 | throw TestException("OK") |
| 80 | } |
| 81 | val maybe1 = d.asMaybe(Unconfined) |
| 82 | checkErroneous(maybe1) { |
| 83 | check(it is TestException && it.message == "OK") { "$it" } |
| 84 | } |
| 85 | val maybe2 = d.asMaybe(Unconfined) |
| 86 | checkErroneous(maybe2) { |
| 87 | check(it is TestException && it.message == "OK") { "$it" } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Test |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 92 | fun testToSingle() { |
| 93 | val d = async(CommonPool) { |
| 94 | delay(50) |
| 95 | "OK" |
| 96 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 97 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 98 | checkSingleValue(single1) { |
| 99 | assertEquals("OK", it) |
| 100 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 101 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 102 | checkSingleValue(single2) { |
| 103 | assertEquals("OK", it) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | fun testToSingleFail() { |
| 109 | val d = async(CommonPool) { |
| 110 | delay(50) |
| 111 | throw TestException("OK") |
| 112 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 113 | val single1 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 114 | checkErroneous(single1) { |
| 115 | check(it is TestException && it.message == "OK") { "$it" } |
| 116 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 117 | val single2 = d.asSingle(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 118 | checkErroneous(single2) { |
| 119 | check(it is TestException && it.message == "OK") { "$it" } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | fun testToObservable() { |
| 125 | val c = produce(CommonPool) { |
| 126 | delay(50) |
| 127 | send("O") |
| 128 | delay(50) |
| 129 | send("K") |
| 130 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 131 | val observable = c.asObservable(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 132 | checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) { |
| 133 | assertEquals("OK", it) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | fun testToObservableFail() { |
| 139 | val c = produce(CommonPool) { |
| 140 | delay(50) |
| 141 | send("O") |
| 142 | delay(50) |
| 143 | throw TestException("K") |
| 144 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 145 | val observable = c.asObservable(Unconfined) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 146 | val single = rxSingle(Unconfined) { |
| 147 | var result = "" |
| 148 | try { |
Roman Elizarov | 7adb876 | 2017-03-17 17:54:13 +0300 | [diff] [blame] | 149 | observable.consumeEach { result += it } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 150 | } catch(e: Throwable) { |
| 151 | check(e is TestException) |
| 152 | result += e.message |
| 153 | } |
| 154 | result |
| 155 | } |
| 156 | checkSingleValue(single) { |
| 157 | assertEquals("OK", it) |
| 158 | } |
| 159 | } |
| 160 | } |