-
Notifications
You must be signed in to change notification settings - Fork 437
/
ScheduleTest.kt
353 lines (289 loc) · 10.2 KB
/
ScheduleTest.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package arrow.fx.coroutines
import arrow.core.Either
import arrow.core.Eval
import io.kotest.assertions.fail
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.withTimeoutOrNull
import kotlin.math.pow
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.flow.zip
@ExperimentalTime
class ScheduleTest : ArrowFxSpec(
spec = {
class MyException : Exception()
val exception = MyException()
"Schedule.identity()" {
val dec = Schedule.identity<Int>().calculateSchedule1(1)
val expected = Schedule.Decision<Any?, Int>(true, 0.0, Unit, Eval.now(1))
dec eqv expected
}
"Schedule.unfold()" {
val dec = Schedule.unfold<Any?, Int>(0) { it + 1 }.calculateSchedule1(0)
val expected = Schedule.Decision<Any?, Int>(true, 0.0, 1, Eval.now(1))
dec eqv expected
}
"Schedule.forever() == Schedule.unfold(0) { it + 1 }" {
val foreverDesc = Schedule.forever<Any?>().calculateSchedule1(0)
val unfoldDesc = Schedule.unfold<Any?, Int>(0) { it + 1 }.calculateSchedule1(0)
foreverDesc eqv unfoldDesc
}
"Schedule.recurs(negative number)" {
checkRepeat(Schedule.recurs(-500), expected = 0)
}
"Schedule.recurs(0)" {
checkRepeat(Schedule.recurs(0), expected = 0)
}
"Schedule.recurs(1)" {
checkRepeat(Schedule.recurs(1), expected = 1)
}
"Schedule.recurs(n: Int)" {
val n = 500
val res = Schedule.recurs<Int>(n).calculateSchedule(0, n + 1)
res.dropLast(1).map { it.delayInNanos.nanoseconds } shouldBe res.dropLast(1).map { 0.nanoseconds }
res.dropLast(1).map { it.cont } shouldBe res.dropLast(1).map { true }
res.last() eqv Schedule.Decision(false, 0.0, n + 1, Eval.now(n + 1))
}
"Schedule.once() repeats 1 additional time" {
var count = 0
Schedule.once<Int>().repeat {
count++
}
count shouldBe 2
}
"Schedule.doWhile repeats while condition holds and returns itself" {
checkRepeat(Schedule.doWhile { it < 10 }, expected = 10)
checkRepeat(Schedule.doWhile { it > 10 }, expected = 1)
checkRepeat(Schedule.doWhile { it == 1 }, expected = 2)
}
"Schedule.doUntil repeats until the cond is satisfied" {
checkRepeat(Schedule.doUntil { it < 10 }, expected = 1)
checkRepeat(Schedule.doUntil { it > 10 }, expected = 11)
checkRepeat(Schedule.doUntil { it == 1 }, expected = 1)
}
"Schedule.doWhile.collect() collects all inputs into a list" {
checkRepeat(
Schedule
.doWhile<Int> { it < 10 }
.collect(),
expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
)
}
"Schedule.doUntil.collect() collects all inputs into a list" {
checkRepeat(
Schedule
.doUntil<Int> { it > 10 }
.collect(),
expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
)
}
"Repeat a scheduled repeat repeats the whole number" {
val n = 42
var count = 0
Schedule.recurs<Int>(1).repeat {
Schedule.recurs<Int>(n).repeat {
count++
}
}
count shouldBe ((n + 1) * 2)
}
"Schedule.never() times out" {
withTimeoutOrNull(10.milliseconds) {
val a: Nothing = Schedule.never<Int>().repeat {
1
}
} shouldBe null
}
"Schedule.spaced()" {
val duration = 5.seconds
val res = Schedule.spaced<Any>(duration).calculateSchedule(0, 500)
res.map { it.cont } shouldBe res.map { true }
res.map { it.delayInNanos.nanoseconds } shouldBe res.map { duration }
}
fun secondsToNanos(sec: Int): Double =
sec * 1_000_000_000.0
"Schedule.fibonacci()" {
val i = secondsToNanos(10)
val n = 10
val res = Schedule.fibonacci<Any?>(i).calculateSchedule(0, n)
val sum = res.fold(0.0) { acc, v ->
acc + v.delayInNanos
}
val fib = fibs(i).drop(1).take(n)
res.all { it.cont } shouldBe true
sum shouldBe fib.sum()
}
"Schedule.linear()" {
val i = secondsToNanos(10)
val n = 10
val res = Schedule.linear<Any?>(i).calculateSchedule(0, n)
val sum = res.fold(0.0) { acc, v -> acc + v.delayInNanos }
val exp = linear(i).drop(1).take(n)
res.all { it.cont } shouldBe true
sum shouldBe exp.sum()
}
"Schedule.exponential()" {
val i = secondsToNanos(10)
val n = 10
val res = Schedule.exponential<Any?>(i).calculateSchedule(0, n)
val sum = res.fold(0.0) { acc, v -> acc + v.delayInNanos }
val expSum = exp(i).drop(1).take(n).sum()
res.all { it.cont } shouldBe true
sum shouldBe expSum
}
"repeat is stack-safe" {
checkRepeat(Schedule.recurs(20_000), expected = 20_000)
}
"repeatAsFlow is stack-safe" {
checkRepeatAsFlow(Schedule.recurs(500_000), expected = (1..500_000).asFlow())
}
"repeat" {
val stop = RuntimeException("WOOO")
val dec = Schedule.Decision(true, 10.0, 0, Eval.now("state"))
val n = 100
val schedule = Schedule({ 0 }) { _: Unit, _ -> dec }
val eff = SideEffect()
val l = Either.catch {
schedule.repeat {
if (eff.counter >= n) throw stop
else eff.increment()
}
}
eff.counter shouldBe 100
l shouldBe Either.Left(stop)
}
"repeatAsFlow" {
val stop = RuntimeException("WOOO")
val dec = Schedule.Decision(true, 10.0, 0, Eval.now("state"))
val n = 100
val schedule = Schedule({ 0 }) { _: Unit, _ -> dec }
val eff = SideEffect()
val l = Either.catch {
schedule.repeatAsFlow {
if (eff.counter >= n) throw stop
else eff.increment()
}.collect()
}
eff.counter shouldBe 100
l shouldBe Either.Left(stop)
}
"repeat fails fast on errors" {
val ex = Throwable("Hello")
Schedule.recurs<Int>(0).repeatOrElseEither({ throw ex }) { exc, _ -> exc }
.fold({ it shouldBe ex }, { fail("The impossible happened") })
}
"repeatAsFlow fails fast on errors" {
val ex = Throwable("Hello")
Schedule.recurs<Int>(0).repeatOrElseEitherAsFlow({ throw ex }, { t, _ -> t })
.collect { either -> either.fold({ it shouldBe ex }, { fail("The impossible happened") }) }
}
"repeat should run the schedule with the correct input" {
var i = 0
val n = 10
(Schedule.recurs<Int>(n).zipRight(Schedule.collect())).repeat { i++ } shouldBe (0..n).toList()
}
"repeatAsFlow should run the schedule with the correct input" {
var i = 0
val n = 10
(Schedule.recurs<Int>(n).zipRight(Schedule.collect())).repeatAsFlow { i++ }.toList() shouldBe
(0..n).map { (0..it).toList() }
}
"retry is stack-safe" {
val count = Atomic(0)
val l = Either.catch {
Schedule.recurs<Throwable>(20_000).retry {
count.updateAndGet { it + 1 }
throw exception
}
}
l should leftException(exception)
count.get() shouldBe 20_001
}
"retry succeeds if no exception is thrown" {
Schedule.recurs<Throwable>(0).retry { 1 } shouldBe 1
}
"retryOrElseEither runs the schedule with the correct input and runs the orElse handler if it does not retry" {
val ex = Throwable("Hello")
val res = Schedule.recurs<Throwable>(0)
.retryOrElseEither({ throw ex }) { e, _ -> e }
res.fold({ it shouldBe ex }, { fail("The impossible happened") })
}
}
)
@ExperimentalTime
private fun fibs(one: Double): Sequence<Double> =
generateSequence(Pair(0.0, one)) { (a, b) ->
Pair(b, (a + b))
}.map { it.first }
@ExperimentalTime
private fun exp(base: Double): Sequence<Double> =
generateSequence(Pair(base, 1.0)) { (_, n) ->
Pair(base * 2.0.pow(n), n + 1)
}.map { it.first }
@ExperimentalTime
private fun linear(base: Double): Sequence<Double> =
generateSequence(Pair(base, 1.0)) { (_, n) ->
Pair((base * n), (n + 1))
}.map { it.first }
@ExperimentalTime
internal fun Sequence<Double>.sum(): Double {
var sum = 0.0
for (element in this) {
sum += element
}
return sum
}
private suspend fun <I, A> Schedule<I, A>.calculateSchedule1(input: I): Schedule.Decision<Any?, A> =
calculateSchedule(input, 1).first()
/**
* Calculates the schedule for [input] I, and [n] iterations
* This allows to calculate the resulting [Schedule.Decision] state and make assertions.
*/
@Suppress("UNCHECKED_CAST")
private suspend fun <I, A> Schedule<I, A>.calculateSchedule(input: I, n: Int): List<Schedule.Decision<Any?, A>> {
(this as Schedule.ScheduleImpl<Any?, I, A>)
val state = initialState.invoke()
return go(this, input, state, n, emptyList())
}
private tailrec suspend fun <I, A> go(
schedule: Schedule.ScheduleImpl<Any?, I, A>,
input: I,
s: Any?,
rem: Int,
acc: List<Schedule.Decision<Any?, A>>
): List<Schedule.Decision<Any?, A>> =
if (rem <= 0) acc
else {
val res = schedule.update(input, s) // Calculate new decision
go(schedule, input, res.state, rem - 1, acc + listOf(res))
}
private suspend fun <B> checkRepeat(schedule: Schedule<Int, B>, expected: B): Unit {
val count = Atomic(0)
schedule.repeat {
count.updateAndGet { it + 1 }
} shouldBe expected
}
private suspend fun <B> checkRepeatAsFlow(schedule: Schedule<Int, B>, expected: Flow<B>): Unit {
val count = Atomic(0)
schedule.repeatAsFlow {
count.updateAndGet { it + 1 }
}.zip(expected, ::Pair)
.collect { (a, b) -> a shouldBe b }
}
@ExperimentalTime
private infix fun <A> Schedule.Decision<Any?, A>.eqv(other: Schedule.Decision<Any?, A>): Unit {
require(cont == other.cont) { "Decision#cont: ${this.cont} shouldBe ${other.cont}" }
require(delayInNanos.nanoseconds == other.delayInNanos.nanoseconds) { "Decision#delay.nanoseconds: ${this.delayInNanos.nanoseconds} shouldBe ${other.delayInNanos.nanoseconds}" }
if (cont) {
val lh = finish.value()
val rh = other.finish.value()
require(lh == rh) { "Decision#cont: $lh shouldBe $rh" }
}
}