blob: d059eb662214707bf7c887d6c1ae4be4d5750543 (
plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* 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.channels.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.reactive.*
import org.junit.Test
import kotlin.test.*
class FluxTest : TestBase() {
@Test
fun testBasicSuccess() = runBlocking {
expect(1)
val flux = flux(currentDispatcher()) {
expect(4)
send("OK")
}
expect(2)
flux.subscribe { value ->
expect(5)
assertEquals("OK", value)
}
expect(3)
yield() // to started coroutine
finish(6)
}
@Test
fun testBasicFailure() = runBlocking {
expect(1)
val flux = flux<String>(currentDispatcher()) {
expect(4)
throw RuntimeException("OK")
}
expect(2)
flux.subscribe({
expectUnreached()
}, { error ->
expect(5)
assertTrue(error is RuntimeException)
assertEquals("OK", error.message)
})
expect(3)
yield() // to started coroutine
finish(6)
}
@Test
fun testBasicUnsubscribe() = runBlocking {
expect(1)
val flux = flux<String>(currentDispatcher()) {
expect(4)
yield() // back to main, will get cancelled
expectUnreached()
}
expect(2)
val sub = flux.subscribe({
expectUnreached()
}, {
expectUnreached()
})
expect(3)
yield() // to started coroutine
expect(5)
sub.dispose() // will cancel coroutine
yield()
finish(6)
}
@Test
fun testNotifyOnceOnCancellation() = runTest {
expect(1)
val observable =
flux(currentDispatcher()) {
expect(5)
send("OK")
try {
delay(Long.MAX_VALUE)
} catch (e: CancellationException) {
expect(11)
}
}
.doOnNext {
expect(6)
assertEquals("OK", it)
}
.doOnCancel {
expect(10) // notified once!
}
expect(2)
val job = launch(start = CoroutineStart.UNDISPATCHED) {
expect(3)
observable.collect {
expect(8)
assertEquals("OK", it)
}
}
expect(4)
yield() // to observable code
expect(7)
yield() // to consuming coroutines
expect(9)
job.cancel()
job.join()
finish(12)
}
@Test
fun testFailingConsumer() = runTest {
val pub = flux(currentDispatcher()) {
repeat(3) {
expect(it + 1) // expect(1), expect(2) *should* be invoked
send(it)
}
}
try {
pub.collect {
throw TestException()
}
} catch (e: TestException) {
finish(3)
}
}
@Test
fun testIllegalArgumentException() {
assertFailsWith<IllegalArgumentException> { flux<Int>(Job()) { } }
}
@Test
fun testLeakedException() = runBlocking {
// Test exception is not reported to global handler
val flow = flux<Unit> { throw TestException() }.asFlow()
repeat(2000) {
combine(flow, flow) { _, _ -> Unit }
.catch {}
.collect { }
}
}
/** Tests that `trySend` doesn't throw in `flux`. */
@Test
fun testTrySendNotThrowing() = runTest {
var producerScope: ProducerScope<Int>? = null
expect(1)
val flux = flux<Int>(Dispatchers.Unconfined) {
producerScope = this
expect(3)
delay(Long.MAX_VALUE)
}
val job = launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
flux.awaitFirstOrNull()
expectUnreached()
}
job.cancel()
expect(4)
val result = producerScope!!.trySend(1)
assertTrue(result.isFailure)
finish(5)
}
/** Tests that all methods on `flux` fail without closing the channel when attempting to emit `null`. */
@Test
fun testEmittingNull() = runTest {
val flux = flux {
assertFailsWith<NullPointerException> { send(null) }
assertFailsWith<NullPointerException> { trySend(null) }
@Suppress("DEPRECATION")
assertFailsWith<NullPointerException> { offer(null) }
send("OK")
}
assertEquals("OK", flux.awaitFirstOrNull())
}
}
|