1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.rx2
import io.reactivex.*
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Assert.*
import java.io.*
import kotlin.experimental.*
/**
* Test emitting multiple values with [rxObservable].
*/
class ObservableMultiTest : TestBase() {
@Test
fun testNumbers() {
val n = 100 * stressTestMultiplier
val observable = GlobalScope.rxObservable {
repeat(n) { send(it) }
}
checkSingleValue(observable.toList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testConcurrentStress() {
val n = 10_000 * stressTestMultiplier
val observable = GlobalScope.rxObservable {
newCoroutineContext(coroutineContext)
// concurrent emitters (many coroutines)
val jobs = List(n) {
// launch
launch {
val i = it
send(i)
}
}
jobs.forEach { it.join() }
}
checkSingleValue(observable.toList()) { list ->
assertEquals(n, list.size)
assertEquals((0 until n).toList(), list.sorted())
}
}
@Test
fun testIteratorResendUnconfined() {
val n = 10_000 * stressTestMultiplier
val observable = GlobalScope.rxObservable(Dispatchers.Unconfined) {
Observable.range(0, n).consumeEach { send(it) }
}
checkSingleValue(observable.toList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testIteratorResendPool() {
val n = 10_000 * stressTestMultiplier
val observable = GlobalScope.rxObservable {
Observable.range(0, n).consumeEach { send(it) }
}
checkSingleValue(observable.toList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testSendAndCrash() {
val observable = GlobalScope.rxObservable {
send("O")
throw IOException("K")
}
val single = GlobalScope.rxSingle {
var result = ""
try {
observable.consumeEach { result += it }
} catch(e: IOException) {
result += e.message
}
result
}
checkSingleValue(single) {
assertEquals("OK", it)
}
}
}
|