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
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.reactor
import kotlinx.coroutines.*
import kotlinx.coroutines.reactive.*
import org.junit.*
import org.junit.Assert.*
import reactor.core.publisher.*
import java.io.*
class FluxMultiTest : TestBase() {
@Test
fun testNumbers() {
val n = 100 * stressTestMultiplier
val flux = GlobalScope.flux {
repeat(n) { send(it) }
}
checkMonoValue(flux.collectList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testConcurrentStress() {
val n = 10_000 * stressTestMultiplier
val flux = GlobalScope.flux {
// concurrent emitters (many coroutines)
val jobs = List(n) {
// launch
launch {
send(it)
}
}
jobs.forEach { it.join() }
}
checkMonoValue(flux.collectList()) { list ->
assertEquals(n, list.size)
assertEquals((0 until n).toList(), list.sorted())
}
}
@Test
fun testIteratorResendUnconfined() {
val n = 10_000 * stressTestMultiplier
val flux = GlobalScope.flux(Dispatchers.Unconfined) {
Flux.range(0, n).consumeEach { send(it) }
}
checkMonoValue(flux.collectList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testIteratorResendPool() {
val n = 10_000 * stressTestMultiplier
val flux = GlobalScope.flux {
Flux.range(0, n).consumeEach { send(it) }
}
checkMonoValue(flux.collectList()) { list ->
assertEquals((0 until n).toList(), list)
}
}
@Test
fun testSendAndCrash() {
val flux = GlobalScope.flux {
send("O")
throw IOException("K")
}
val mono = GlobalScope.mono {
var result = ""
try {
flux.consumeEach { result += it }
} catch(e: IOException) {
result += e.message
}
result
}
checkMonoValue(mono) {
assertEquals("OK", it)
}
}
}
|