krontab/src/commonTest/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt

69 lines
1.7 KiB
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab.utils
2020-01-13 04:15:01 +00:00
2020-11-21 08:58:19 +00:00
import dev.inmo.krontab.builder.buildSchedule
2020-01-13 04:15:01 +00:00
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.takeWhile
2023-03-18 06:23:53 +00:00
import kotlinx.coroutines.test.runTest
2020-01-13 04:15:01 +00:00
import kotlin.test.Test
import kotlin.test.assertEquals
@ExperimentalCoroutinesApi
@FlowPreview
class SchedulerFlowTests {
@Test
fun testThatFlowIsCorrectlyWorkEverySecond() {
val kronScheduler = buildSchedule {
seconds {
0 every 1
}
}
2023-03-18 06:23:53 +00:00
val flow = kronScheduler.asFlowWithoutDelays()
2020-01-13 04:15:01 +00:00
runTest {
val mustBeCollected = 10
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
assertEquals(mustBeCollected, collected)
}
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondWithMuchOfEmitters() {
val kronScheduler = buildSchedule {
seconds {
0 every 1
}
}
2023-03-18 06:23:53 +00:00
val flow = kronScheduler.asFlowWithoutDelays()
runTest {
val testsCount = 10
2023-03-18 06:23:53 +00:00
val failJob = createFailJob((testsCount * 2) * 1000L)
val mustBeCollected = 10
val answers = (0 until testsCount).map { _ ->
2023-03-18 06:23:53 +00:00
async {
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
collected
}
}.awaitAll()
failJob.cancel()
answers.forEach {
assertEquals(mustBeCollected, it)
}
}
}
2020-01-13 04:15:01 +00:00
}