-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathPublishTest.kt
287 lines (267 loc) · 8.92 KB
/
PublishTest.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.jdk9
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import org.junit.Test
import java.util.concurrent.Flow as JFlow
import kotlin.test.*
class PublishTest : TestBase() {
@Test
fun testBasicEmpty() = runTest {
expect(1)
val publisher = flowPublish<Int>(currentDispatcher()) {
expect(5)
}
expect(2)
publisher.subscribe(object : JFlow.Subscriber<Int> {
override fun onSubscribe(s: JFlow.Subscription?) { expect(3) }
override fun onNext(t: Int?) { expectUnreached() }
override fun onComplete() { expect(6) }
override fun onError(t: Throwable?) { expectUnreached() }
})
expect(4)
yield() // to publish coroutine
finish(7)
}
@Test
fun testBasicSingle() = runTest {
expect(1)
val publisher = flowPublish(currentDispatcher()) {
expect(5)
send(42)
expect(7)
}
expect(2)
publisher.subscribe(object : JFlow.Subscriber<Int> {
override fun onSubscribe(s: JFlow.Subscription) {
expect(3)
s.request(1)
}
override fun onNext(t: Int) {
expect(6)
assertEquals(42, t)
}
override fun onComplete() { expect(8) }
override fun onError(t: Throwable?) { expectUnreached() }
})
expect(4)
yield() // to publish coroutine
finish(9)
}
@Test
fun testBasicError() = runTest {
expect(1)
val publisher = flowPublish<Int>(currentDispatcher()) {
expect(5)
throw RuntimeException("OK")
}
expect(2)
publisher.subscribe(object : JFlow.Subscriber<Int> {
override fun onSubscribe(s: JFlow.Subscription) {
expect(3)
s.request(1)
}
override fun onNext(t: Int) { expectUnreached() }
override fun onComplete() { expectUnreached() }
override fun onError(t: Throwable) {
expect(6)
assertTrue(t is RuntimeException)
assertEquals("OK", t.message)
}
})
expect(4)
yield() // to publish coroutine
finish(7)
}
@Test
fun testHandleFailureAfterCancel() = runTest {
expect(1)
val eh = CoroutineExceptionHandler { _, t ->
assertTrue(t is RuntimeException)
expect(6)
}
val publisher = flowPublish<Unit>(Dispatchers.Unconfined + eh) {
try {
expect(3)
delay(10000)
} finally {
expect(5)
throw RuntimeException("FAILED") // crash after cancel
}
}
var sub: JFlow.Subscription? = null
publisher.subscribe(object : JFlow.Subscriber<Unit> {
override fun onComplete() {
expectUnreached()
}
override fun onSubscribe(s: JFlow.Subscription) {
expect(2)
sub = s
}
override fun onNext(t: Unit?) {
expectUnreached()
}
override fun onError(t: Throwable?) {
expectUnreached()
}
})
expect(4)
sub!!.cancel()
finish(7)
}
/** Tests that, as soon as `ProducerScope.close` is called, `isClosedForSend` starts returning `true`. */
@Test
fun testChannelClosing() = runTest {
expect(1)
val publisher = flowPublish<Int>(Dispatchers.Unconfined) {
expect(3)
close()
assert(isClosedForSend)
expect(4)
}
try {
expect(2)
publisher.awaitFirstOrNull()
} catch (e: CancellationException) {
expect(5)
}
finish(6)
}
@Test
fun testOnNextError() = runTest {
val latch = CompletableDeferred<Unit>()
expect(1)
assertCallsExceptionHandlerWith<TestException> { exceptionHandler ->
val publisher = flowPublish(currentDispatcher() + exceptionHandler) {
expect(4)
try {
send("OK")
} catch (e: Throwable) {
expect(6)
assert(e is TestException)
assert(isClosedForSend)
latch.complete(Unit)
}
}
expect(2)
publisher.subscribe(object : JFlow.Subscriber<String> {
override fun onComplete() {
expectUnreached()
}
override fun onSubscribe(s: JFlow.Subscription) {
expect(3)
s.request(1)
}
override fun onNext(t: String) {
expect(5)
assertEquals("OK", t)
throw TestException()
}
override fun onError(t: Throwable) {
expectUnreached()
}
})
latch.await()
}
finish(7)
}
/** Tests the behavior when a call to `onNext` fails after the channel is already closed. */
@Test
fun testOnNextErrorAfterCancellation() = runTest {
assertCallsExceptionHandlerWith<TestException> { handler ->
var producerScope: ProducerScope<Int>? = null
CompletableDeferred<Unit>()
expect(1)
var job: Job? = null
val publisher = flowPublish<Int>(handler + Dispatchers.Unconfined) {
producerScope = this
expect(4)
job = launch {
delay(Long.MAX_VALUE)
}
}
expect(2)
publisher.subscribe(object: JFlow.Subscriber<Int> {
override fun onSubscribe(s: JFlow.Subscription) {
expect(3)
s.request(Long.MAX_VALUE)
}
override fun onNext(t: Int) {
expect(6)
assertEquals(1, t)
job!!.cancel()
throw TestException()
}
override fun onError(t: Throwable?) {
/* Correct changes to the implementation could lead to us entering or not entering this method, but
it only matters that if we do, it is the "correct" exception that was validly used to cancel the
coroutine that gets passed here and not `TestException`. */
assertTrue(t is CancellationException)
}
override fun onComplete() { expectUnreached() }
})
expect(5)
val result: ChannelResult<Unit> = producerScope!!.trySend(1)
val e = result.exceptionOrNull()!!
assertTrue(e is CancellationException, "The actual error: $e")
assertTrue(producerScope!!.isClosedForSend)
assertTrue(result.isFailure)
}
finish(7)
}
@Test
fun testFailingConsumer() = runTest {
val pub = flowPublish(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> { flowPublish<Int>(Job()) { } }
}
/** Tests that `trySend` doesn't throw in `flowPublish`. */
@Test
fun testTrySendNotThrowing() = runTest {
var producerScope: ProducerScope<Int>? = null
expect(1)
val publisher = flowPublish<Int>(Dispatchers.Unconfined) {
producerScope = this
expect(3)
delay(Long.MAX_VALUE)
}
val job = launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
publisher.awaitFirstOrNull()
expectUnreached()
}
job.cancel()
expect(4)
val result = producerScope!!.trySend(1)
assertTrue(result.isFailure)
finish(5)
}
/** Tests that all methods on `flowPublish` fail without closing the channel when attempting to emit `null`. */
@Test
fun testEmittingNull() = runTest {
val publisher = flowPublish {
assertFailsWith<NullPointerException> { send(null) }
assertFailsWith<NullPointerException> { trySend(null) }
@Suppress("DEPRECATION")
assertFailsWith<NullPointerException> { offer(null) }
send("OK")
}
assertEquals("OK", publisher.awaitFirstOrNull())
}
}