-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathPublisherCollectTest.kt
146 lines (131 loc) · 4.65 KB
/
PublisherCollectTest.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
/*
* Copyright 2016-2021 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.reactive.*
import org.junit.Test
import org.reactivestreams.*
import kotlin.test.*
import java.util.concurrent.Flow as JFlow
class PublisherCollectTest: TestBase() {
/** Tests the simple scenario where the publisher outputs a bounded stream of values to collect. */
@Test
fun testCollect() = runTest {
val x = 100
val xSum = x * (x + 1) / 2
val publisher = JFlow.Publisher<Int> { subscriber ->
var requested = 0L
var lastOutput = 0
subscriber.onSubscribe(object: JFlow.Subscription {
override fun request(n: Long) {
requested += n
if (n <= 0) {
subscriber.onError(IllegalArgumentException())
return
}
while (lastOutput < x && lastOutput < requested) {
lastOutput += 1
subscriber.onNext(lastOutput)
}
if (lastOutput == x)
subscriber.onComplete()
}
override fun cancel() {
/** According to rule 3.5 of the
* [reactive spec](https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#3.5),
* this method can be called by the subscriber at any point, so it's not an error if it's called
* in this scenario. */
}
})
}
var sum = 0
publisher.collect {
sum += it
}
assertEquals(xSum, sum)
}
/** Tests the behavior of [collect] when the publisher raises an error. */
@Test
fun testCollectThrowingPublisher() = runTest {
val errorString = "Too many elements requested"
val x = 100
val xSum = x * (x + 1) / 2
val publisher = Publisher<Int> { subscriber ->
var requested = 0L
var lastOutput = 0
subscriber.onSubscribe(object: Subscription {
override fun request(n: Long) {
requested += n
if (n <= 0) {
subscriber.onError(IllegalArgumentException())
return
}
while (lastOutput < x && lastOutput < requested) {
lastOutput += 1
subscriber.onNext(lastOutput)
}
if (lastOutput == x)
subscriber.onError(IllegalArgumentException(errorString))
}
override fun cancel() {
/** See the comment for the corresponding part of [testCollect]. */
}
})
}
var sum = 0
try {
publisher.collect {
sum += it
}
} catch (e: IllegalArgumentException) {
assertEquals(errorString, e.message)
}
assertEquals(xSum, sum)
}
/** Tests the behavior of [collect] when the action throws. */
@Test
fun testCollectThrowingAction() = runTest {
val errorString = "Too many elements produced"
val x = 100
val xSum = x * (x + 1) / 2
val publisher = Publisher<Int> { subscriber ->
var requested = 0L
var lastOutput = 0
subscriber.onSubscribe(object: Subscription {
override fun request(n: Long) {
requested += n
if (n <= 0) {
subscriber.onError(IllegalArgumentException())
return
}
while (lastOutput < x && lastOutput < requested) {
lastOutput += 1
subscriber.onNext(lastOutput)
}
}
override fun cancel() {
assertEquals(x, lastOutput)
expect(x + 2)
}
})
}
var sum = 0
try {
expect(1)
var i = 1
publisher.collect {
sum += it
i += 1
expect(i)
if (sum >= xSum) {
throw IllegalArgumentException(errorString)
}
}
} catch (e: IllegalArgumentException) {
expect(x + 3)
assertEquals(errorString, e.message)
}
finish(x + 4)
}
}