This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
/
CallbackTest.kt
219 lines (184 loc) · 4.53 KB
/
CallbackTest.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
package arrow.fx.coroutines.stream
import arrow.fx.coroutines.Atomic
import arrow.fx.coroutines.CancelToken
import arrow.fx.coroutines.CancellableContinuation
import arrow.fx.coroutines.ForkAndForget
import arrow.fx.coroutines.ForkConnected
import arrow.fx.coroutines.Promise
import arrow.fx.coroutines.Schedule
import arrow.fx.coroutines.UnsafePromise
import arrow.fx.coroutines.cancelBoundary
import kotlin.time.milliseconds
import arrow.fx.coroutines.parTupledN
import arrow.fx.coroutines.startCoroutineCancellable
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.list
import io.kotest.property.arbitrary.map
import kotlinx.coroutines.delay
class CallbackTest : StreamSpec(iterations = 250, spec = {
"should be lazy" {
checkAll(Arb.int()) {
val effect = Atomic(false)
val s = Stream.callback<Int> {
effect.set(true)
end()
}
effect.get() shouldBe false
s.drain()
effect.get() shouldBe true
}
}
"emits values" {
checkAll(Arb.list(Arb.int())) { list ->
Stream.callback {
list.forEach { emit(it) }
end()
}
.toList() shouldBe list
}
}
"emits varargs" {
checkAll(Arb.list(Arb.int()).map { it.toTypedArray() }) { list ->
Stream.callback {
emit(*list)
end()
}
.chunks()
.toList() shouldBe listOf(Chunk(*list))
}
}
"emits iterable" {
checkAll(Arb.list(Arb.int())) { list ->
Stream.callback<Int> {
emit(list)
end()
}
.chunks()
.toList() shouldBe listOf(Chunk.iterable(list))
}
}
"emits chunks" {
checkAll(Arb.chunk(Arb.int()), Arb.chunk(Arb.int())) { ch, ch2 ->
Stream.callback<Int> {
emit(ch)
emit(ch2)
end()
}
.chunks()
.toList() shouldBe listOf(ch, ch2)
}
}
"long running emission" {
Stream.callback {
ForkAndForget {
countToCallback(4, { it }, { emit(it) }) { end() }
}
}
.toList() shouldBe listOf(1, 2, 3, 4, 5)
}
"parallel emission/pulling" {
val ref = Atomic(false)
Stream.callback {
emit(1)
delay(500.milliseconds)
emit(2)
ref.set(true)
end()
}
.effectMap {
if (it == 1) ref.get() shouldBe false
else Unit
}
.drain()
}
"forwards errors" {
checkAll(Arb.throwable()) { e ->
val s = Stream.cancellable<Int> {
throw e
}
assertThrowable {
s.drain()
} shouldBe e
}
}
"forwards suspended errors" {
checkAll(Arb.throwable()) { e ->
val s = Stream.cancellable<Int> {
e.suspend()
}
assertThrowable {
s.drain()
} shouldBe e
}
}
"runs cancel token" {
checkAll(Arb.int()) {
val latch = Promise<Unit>()
val effect = Atomic(false)
val s = Stream.cancellable<Int> {
CancelToken { effect.set(true) }
}
val f = ForkAndForget {
parTupledN(
{ s.drain() },
{ latch.complete(Unit) }
)
}
parTupledN({ latch.get() }, { delay(20.milliseconds) })
f.cancel()
effect.get() shouldBe true
}
}
"cancelableF executes generated task uninterruptedly" {
checkAll(Arb.int()) { i ->
val latch = Promise<Unit>()
val start = Promise<Unit>()
val done = Promise<Int>()
val task = suspend {
Stream.cancellable {
latch.complete(Unit)
start.get()
cancelBoundary()
emit(Unit)
done.complete(i)
CancelToken.unit
}
.lastOrError()
}
val p = UnsafePromise<Unit>()
val cancel = task.startCoroutineCancellable(CancellableContinuation { r -> p.complete(r) })
latch.get()
ForkConnected { cancel.invoke() }
// Let cancel schedule
delay(10.milliseconds)
start.complete(Unit) // Continue cancellableF
done.get() shouldBe i
p.tryGet() shouldBe null
}
}
"doesn't run cancel token without cancellation" {
val effect = Atomic(false)
Stream.cancellable<Int> {
end()
CancelToken { effect.set(true) }
}
.drain()
effect.get() shouldBe false
}
})
private suspend fun <A> countToCallback(
iterations: Int,
map: (Int) -> A,
cb: suspend (A) -> Unit,
onEnd: suspend () -> Unit = { }
): Unit {
var i = 0
arrow.fx.coroutines.repeat(Schedule.recurs(iterations)) {
i += 1
cb(map(i))
delay(500.milliseconds)
}
onEnd()
}