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

139 lines
4.5 KiB
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab.utils
2021-04-10 09:29:55 +00:00
import com.soywiz.klock.*
2021-04-09 18:28:08 +00:00
import dev.inmo.krontab.KronSchedulerTz
2020-11-21 08:58:19 +00:00
import dev.inmo.krontab.buildSchedule
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.takeWhile
2023-03-18 06:23:53 +00:00
import kotlinx.coroutines.test.runTest
2022-04-29 15:57:10 +00:00
import kotlin.test.*
@ExperimentalCoroutinesApi
@FlowPreview
class StringParseTest {
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnString() {
val kronScheduler = buildSchedule("*/1 * * * *")
2023-03-18 06:23:53 +00:00
val flow = kronScheduler.asFlowWithoutDelays()
runTest {
val mustBeCollected = 10
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
assertEquals(mustBeCollected, collected)
}
}
2022-04-29 15:57:10 +00:00
@Test
fun testThatFlowIsCorrectlyWorkEverySecondWhenMillisIsHalfOfSecondBuiltOnString() {
val kronScheduler = buildSchedule("*/1 * * * * 500ms")
2023-03-18 06:23:53 +00:00
val flow = kronScheduler.asFlowWithoutDelays()
2022-04-29 15:57:10 +00:00
runTest {
val mustBeCollected = 10
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
assertEquals(mustBeCollected, collected)
}
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondWithMuchOfEmittersBuiltOnString() {
val kronScheduler = buildSchedule("*/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-07-24 07:16:56 +00:00
@Test
fun testThatFlowIsCorrectlyWorkEverySeveralSecondsRangeBuiltOnString() {
val rangesEnds = listOf(0 to 5, 30 to 35)
val kronScheduler = buildSchedule("${rangesEnds.joinToString(",") { "${it.first}-${it.second}" }} * * * *")
2023-03-18 06:23:53 +00:00
val flow = kronScheduler.asFlowWithoutDelays()
2020-07-24 07:16:56 +00:00
runTest {
2021-05-30 12:53:44 +00:00
val ranges = rangesEnds.map { it.first .. it.second }.flatten().distinct().toMutableList()
val expectedCollects = ranges.size
2020-07-24 07:16:56 +00:00
var collected = 0
flow.takeWhile { ranges.isNotEmpty() }.collect {
ranges.remove(it.seconds)
collected++
2023-03-18 06:23:53 +00:00
assertTrue(
collected <= expectedCollects,
"Expected value should be less than $expectedCollects, but was $collected. Ranges state: $ranges"
)
2022-04-29 15:57:10 +00:00
}
assertEquals(expectedCollects, collected)
}
}
@Test
fun testNextIsCorrectlyWorkEverySeveralMillisecondsRangeBuiltOnString() {
val rangesEnds = listOf(0, 200, 500, 750)
val kronScheduler = buildSchedule("* * * * * ${rangesEnds.joinToString(",") { "$it" }}ms")
runTest {
val ranges = rangesEnds.toMutableList()
val expectedCollects = ranges.size
var collected = 0
var currentTime = DateTime.now()
while (ranges.isNotEmpty()) {
val nextTrigger = kronScheduler.next(currentTime) ?: error("Strangely unable to get next time")
ranges.remove(nextTrigger.milliseconds)
collected++
currentTime = nextTrigger + 1.milliseconds
2020-07-24 07:16:56 +00:00
}
assertEquals(expectedCollects, collected)
}
}
2021-03-30 08:07:39 +00:00
@Test
fun testThatTimezoneCorrectlyDeserialized() {
2021-06-03 06:37:13 +00:00
val now = DateTime.now().copy(milliseconds = 0).local
2021-03-30 08:07:39 +00:00
runTest {
2021-04-10 09:29:55 +00:00
for (i in 0 .. 1339) {
val expectedInCurrentOffset = now.toOffset(TimezoneOffset(i.minutes)) + 1.hours
val kronScheduler = buildSchedule(
"${expectedInCurrentOffset.seconds} ${expectedInCurrentOffset.minutes} ${expectedInCurrentOffset.hours} * * ${i}o"
) as KronSchedulerTz
2021-03-30 08:07:39 +00:00
val next = kronScheduler.next(now)
2021-04-10 09:29:55 +00:00
assertEquals(expectedInCurrentOffset.toOffset(now.offset), next)
2021-03-30 08:07:39 +00:00
}
}
}
}