-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathIntegrationTest.kt
135 lines (120 loc) · 4.23 KB
/
IntegrationTest.kt
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* 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 kotlinx.coroutines.flow.*
import org.junit.Test
import org.junit.runner.*
import org.junit.runners.*
import kotlin.coroutines.*
import kotlin.test.*
@RunWith(Parameterized::class)
class IntegrationTest(
private val ctx: Ctx,
private val delay: Boolean
) : TestBase() {
enum class Ctx {
MAIN { override fun invoke(context: CoroutineContext): CoroutineContext = context.minusKey(Job) },
DEFAULT { override fun invoke(context: CoroutineContext): CoroutineContext = Dispatchers.Default },
UNCONFINED { override fun invoke(context: CoroutineContext): CoroutineContext = Dispatchers.Unconfined };
abstract operator fun invoke(context: CoroutineContext): CoroutineContext
}
companion object {
@Parameterized.Parameters(name = "ctx={0}, delay={1}")
@JvmStatic
fun params(): Collection<Array<Any>> = Ctx.values().flatMap { ctx ->
listOf(false, true).map { delay ->
arrayOf(ctx, delay)
}
}
}
@Test
fun testEmpty(): Unit = runBlocking {
val observable = rxObservable<String>(ctx(coroutineContext)) {
if (delay) delay(1)
// does not send anything
}
assertFailsWith<NoSuchElementException> { observable.awaitFirst() }
assertEquals("OK", observable.awaitFirstOrDefault("OK"))
assertNull(observable.awaitFirstOrNull())
assertEquals("ELSE", observable.awaitFirstOrElse { "ELSE" })
assertFailsWith<NoSuchElementException> { observable.awaitLast() }
assertFailsWith<NoSuchElementException> { observable.awaitSingle() }
var cnt = 0
observable.collect {
cnt++
}
assertEquals(0, cnt)
}
@Test
fun testSingle() = runBlocking {
val observable = rxObservable(ctx(coroutineContext)) {
if (delay) delay(1)
send("OK")
}
assertEquals("OK", observable.awaitFirst())
assertEquals("OK", observable.awaitFirstOrDefault("OK"))
assertEquals("OK", observable.awaitFirstOrNull())
assertEquals("OK", observable.awaitFirstOrElse { "ELSE" })
assertEquals("OK", observable.awaitLast())
assertEquals("OK", observable.awaitSingle())
var cnt = 0
observable.collect {
assertEquals("OK", it)
cnt++
}
assertEquals(1, cnt)
}
@Test
fun testNumbers() = runBlocking<Unit> {
val n = 100 * stressTestMultiplier
val observable = rxObservable(ctx(coroutineContext)) {
for (i in 1..n) {
send(i)
if (delay) delay(1)
}
}
assertEquals(1, observable.awaitFirst())
assertEquals(1, observable.awaitFirstOrDefault(0))
assertEquals(1, observable.awaitFirstOrNull())
assertEquals(1, observable.awaitFirstOrElse { 0 })
assertEquals(n, observable.awaitLast())
assertFailsWith<IllegalArgumentException> { observable.awaitSingle() }
checkNumbers(n, observable)
val channel = observable.openSubscription()
checkNumbers(n, channel.consumeAsFlow().asObservable(ctx(coroutineContext)))
channel.cancel()
}
@Test
fun testCancelWithoutValue() = runTest {
val job = launch(Job(), start = CoroutineStart.UNDISPATCHED) {
rxObservable<String> {
hang { }
}.awaitFirst()
}
job.cancel()
job.join()
}
@Test
fun testEmptySingle() = runTest(unhandled = listOf({e -> e is NoSuchElementException})) {
expect(1)
val job = launch(Job(), start = CoroutineStart.UNDISPATCHED) {
rxObservable<String> {
yield()
expect(2)
// Nothing to emit
}.awaitFirst()
}
job.join()
finish(3)
}
private suspend fun checkNumbers(n: Int, observable: Observable<Int>) {
var last = 0
observable.collect {
assertEquals(++last, it)
}
assertEquals(n, last)
}
}