forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableAsFlowTest.kt
185 lines (168 loc) · 5.11 KB
/
ObservableAsFlowTest.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
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.rx2
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.Observer
import io.reactivex.disposables.Disposables
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlin.test.*
class ObservableAsFlowTest : TestBase() {
@Test
fun testCancellation() = runTest {
var onNext = 0
var onCancelled = 0
var onError = 0
val source = rxObservable(currentDispatcher()) {
coroutineContext[Job]?.invokeOnCompletion {
if (it is CancellationException) ++onCancelled
}
repeat(100) {
send(it)
}
}
source.asFlow().launchIn(CoroutineScope(Dispatchers.Unconfined)) {
onEach {
++onNext
throw RuntimeException()
}
catch<Throwable> {
++onError
}
}.join()
assertEquals(1, onNext)
assertEquals(1, onError)
assertEquals(1, onCancelled)
}
@Test
fun testImmediateCollection() {
val source = PublishSubject.create<Int>()
val flow = source.asFlow()
GlobalScope.launch(Dispatchers.Unconfined) {
expect(1)
flow.collect { expect(it) }
expect(6)
}
expect(2)
source.onNext(3)
expect(4)
source.onNext(5)
source.onComplete()
finish(7)
}
@Test
fun testOnErrorCancellation() {
val source = PublishSubject.create<Int>()
val flow = source.asFlow()
val exception = RuntimeException()
GlobalScope.launch(Dispatchers.Unconfined) {
try {
expect(1)
flow.collect { expect(it) }
expectUnreached()
}
catch (e: Exception) {
assertSame(exception, e.cause)
expect(5)
}
expect(6)
}
expect(2)
source.onNext(3)
expect(4)
source.onError(exception)
finish(7)
}
@Test
fun testUnsubscribeOnCollectionException() {
val source = PublishSubject.create<Int>()
val flow = source.asFlow()
val exception = RuntimeException()
GlobalScope.launch(Dispatchers.Unconfined) {
try {
expect(1)
flow.collect {
expect(it)
if (it == 3) throw exception
}
expectUnreached()
}
catch (e: Exception) {
assertSame(exception, e.cause)
expect(4)
}
expect(5)
}
expect(2)
assertTrue(source.hasObservers())
source.onNext(3)
assertFalse(source.hasObservers())
finish(6)
}
@Test
fun testLateOnSubscribe() {
var observer: Observer<in Int>? = null
val source = ObservableSource<Int> { observer = it }
val flow = source.asFlow()
assertNull(observer)
val job = GlobalScope.launch(Dispatchers.Unconfined) {
expect(1)
flow.collect { expectUnreached() }
expectUnreached()
}
expect(2)
assertNotNull(observer)
job.cancel()
val disposable = Disposables.empty()
observer!!.onSubscribe(disposable)
assertTrue(disposable.isDisposed)
finish(3)
}
@Test
fun testBufferUnlimited() = runTest {
val source = rxObservable(currentDispatcher()) {
expect(1); send(10)
expect(2); send(11)
expect(3); send(12)
expect(4); send(13)
expect(5); send(14)
expect(6); send(15)
expect(7); send(16)
expect(8); send(17)
expect(9)
}
source.asFlow().buffer(Channel.UNLIMITED).collect { expect(it) }
finish(18)
}
@Test
fun testConflated() = runTest {
val source = Observable.range(1, 5)
val list = source.asFlow().conflate().toList()
assertEquals(listOf(1, 5), list)
}
@Test
fun testLongRange() = runTest {
val source = Observable.range(1, 10_000)
val count = source.asFlow().count()
assertEquals(10_000, count)
}
@Test
fun testProduce() = runTest {
val source = Observable.range(0, 10)
val flow = source.asFlow()
check((0..9).toList(), flow.produceIn(this))
check((0..9).toList(), flow.buffer(Channel.UNLIMITED).produceIn(this))
check((0..9).toList(), flow.buffer(2).produceIn(this))
check((0..9).toList(), flow.buffer(0).produceIn(this))
check(listOf(0, 9), flow.conflate().produceIn(this))
}
private suspend fun check(expected: List<Int>, channel: ReceiveChannel<Int>) {
val result = ArrayList<Int>(10)
channel.consumeEach { result.add(it) }
assertEquals(expected, result)
}
}