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.rx1 |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import org.hamcrest.MatcherAssert.* |
| 9 | import org.hamcrest.core.* |
| 10 | import org.junit.* |
| 11 | import org.junit.runner.* |
| 12 | import org.junit.runners.* |
| 13 | import rx.* |
| 14 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 15 | |
| 16 | @RunWith(Parameterized::class) |
| 17 | class IntegrationTest( |
| 18 | val ctx: Ctx, |
| 19 | val delay: Boolean |
| 20 | ) : TestBase() { |
| 21 | |
| 22 | enum class Ctx { |
| 23 | MAIN { override fun invoke(context: CoroutineContext): CoroutineContext = context }, |
| 24 | COMMON_POOL { override fun invoke(context: CoroutineContext): CoroutineContext = CommonPool }, |
| 25 | UNCONFINED { override fun invoke(context: CoroutineContext): CoroutineContext = Unconfined }; |
| 26 | |
| 27 | abstract operator fun invoke(context: CoroutineContext): CoroutineContext |
| 28 | } |
| 29 | |
| 30 | companion object { |
| 31 | @Parameterized.Parameters(name = "ctx={0}, delay={1}") |
| 32 | @JvmStatic |
| 33 | fun params(): Collection<Array<Any>> = Ctx.values().flatMap { ctx -> |
| 34 | listOf(false, true).map { delay -> |
| 35 | arrayOf<Any>(ctx, delay) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @Test |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 41 | fun testEmpty(): Unit = runBlocking { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 42 | val observable = rxObservable<String>(ctx(coroutineContext)) { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 43 | if (delay) delay(1) |
| 44 | // does not send anything |
| 45 | } |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 46 | assertNSE { observable.awaitFirst() } |
| 47 | assertThat(observable.awaitFirstOrDefault("OK"), IsEqual("OK")) |
Konrad Kamiński | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 48 | assertThat(observable.awaitFirstOrNull(), IsNull()) |
| 49 | assertThat(observable.awaitFirstOrElse {"ELSE" }, IsEqual("ELSE")) |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 50 | assertNSE { observable.awaitLast() } |
| 51 | assertNSE { observable.awaitSingle() } |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 52 | var cnt = 0 |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 53 | observable.consumeEach { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 54 | cnt++ |
| 55 | } |
| 56 | assertThat(cnt, IsEqual(0)) |
| 57 | } |
| 58 | |
| 59 | @Test |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 60 | fun testSingle() = runBlocking<Unit> { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 61 | val observable = rxObservable<String>(ctx(coroutineContext)) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 62 | if (delay) delay(1) |
| 63 | send("OK") |
| 64 | } |
| 65 | assertThat(observable.awaitFirst(), IsEqual("OK")) |
Konrad Kamiński | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 66 | assertThat(observable.awaitFirstOrDefault("OK"), IsEqual("OK")) |
| 67 | assertThat(observable.awaitFirstOrNull(), IsEqual("OK")) |
| 68 | assertThat(observable.awaitFirstOrElse {"OK" }, IsEqual("OK")) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 69 | assertThat(observable.awaitLast(), IsEqual("OK")) |
| 70 | assertThat(observable.awaitSingle(), IsEqual("OK")) |
| 71 | var cnt = 0 |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 72 | observable.consumeEach { |
| 73 | assertThat(it, IsEqual("OK")) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 74 | cnt++ |
| 75 | } |
| 76 | assertThat(cnt, IsEqual(1)) |
| 77 | } |
| 78 | |
| 79 | @Test |
Konrad Kamiński | f1a1f07 | 2018-02-06 22:53:28 +0100 | [diff] [blame] | 80 | fun testObservableWithNull() = runBlocking<Unit> { |
| 81 | val observable = rxObservable<String?>(ctx(coroutineContext)) { |
| 82 | if (delay) delay(1) |
| 83 | send(null) |
| 84 | } |
| 85 | assertThat(observable.awaitFirst(), IsNull()) |
| 86 | assertThat(observable.awaitFirstOrDefault("OK"), IsNull()) |
| 87 | assertThat(observable.awaitFirstOrNull(), IsNull()) |
| 88 | assertThat(observable.awaitFirstOrElse { "OK" }, IsNull()) |
| 89 | assertThat(observable.awaitLast(), IsNull()) |
| 90 | assertThat(observable.awaitSingle(), IsNull()) |
| 91 | var cnt = 0 |
| 92 | observable.consumeEach { |
| 93 | assertThat(it, IsNull()) |
| 94 | cnt++ |
| 95 | } |
| 96 | assertThat(cnt, IsEqual(1)) |
| 97 | } |
| 98 | |
| 99 | @Test |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 100 | fun testNumbers() = runBlocking<Unit> { |
| 101 | val n = 100 * stressTestMultiplier |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 102 | val observable = rxObservable<Int>(ctx(coroutineContext)) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 103 | for (i in 1..n) { |
| 104 | send(i) |
| 105 | if (delay) delay(1) |
| 106 | } |
| 107 | } |
| 108 | assertThat(observable.awaitFirst(), IsEqual(1)) |
Konrad Kamiński | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 109 | assertThat(observable.awaitFirstOrDefault(0), IsEqual(1)) |
| 110 | assertThat(observable.awaitFirstOrNull(), IsEqual(1)) |
| 111 | assertThat(observable.awaitFirstOrElse { 0 }, IsEqual(1)) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 112 | assertThat(observable.awaitLast(), IsEqual(n)) |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 113 | assertIAE { observable.awaitSingle() } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 114 | checkNumbers(n, observable) |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 115 | val channel = observable.openSubscription() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 116 | checkNumbers(n, channel.asObservable(ctx(coroutineContext))) |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 117 | channel.cancel() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private suspend fun checkNumbers(n: Int, observable: Observable<Int>) { |
| 121 | var last = 0 |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 122 | observable.consumeEach { |
| 123 | assertThat(it, IsEqual(++last)) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 124 | } |
| 125 | assertThat(last, IsEqual(n)) |
| 126 | } |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 127 | |
| 128 | inline fun assertIAE(block: () -> Unit) { |
| 129 | try { |
| 130 | block() |
| 131 | expectUnreached() |
| 132 | } catch (e: Throwable) { |
| 133 | assertThat(e, IsInstanceOf(IllegalArgumentException::class.java)) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | inline fun assertNSE(block: () -> Unit) { |
| 138 | try { |
| 139 | block() |
| 140 | expectUnreached() |
| 141 | } catch (e: Throwable) { |
| 142 | assertThat(e, IsInstanceOf(NoSuchElementException::class.java)) |
| 143 | } |
| 144 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 145 | } |