new buildSchedule function and tests for string parsing

This commit is contained in:
InsanusMokrassar 2020-06-03 22:04:05 +06:00
parent c9243d7968
commit d8ed07da98
3 changed files with 73 additions and 1 deletions

View File

@ -13,7 +13,11 @@
* Kotlin `1.3.70` -> `1.3.72` * Kotlin `1.3.70` -> `1.3.72`
* Coroutines `1.3.5` -> `1.3.7` * Coroutines `1.3.5` -> `1.3.7`
* Klock `1.10.0` -> `1.11.3` * Klock `1.10.0` -> `1.11.3`
* A lot of KDocs added and fixed
* `EverySecondScheduler` changed its building logic - now it is lazy with builder using * `EverySecondScheduler` changed its building logic - now it is lazy with builder using
* `KronScheduler#doOnce` was optimized: now it will be explicitly called once and return result of its calculations
* `KronScheduler#doWhile` was rewritten to use `KronScheduler#doOnce` for calculations of `block` result
* New `buildSchedule(String)` function as a shortcut for `createSimpleScheduler(String)`
### 0.2.2 ### 0.2.2

View File

@ -51,3 +51,8 @@ fun createSimpleScheduler(incoming: String): KronScheduler {
secondsParsed, minutesParsed, hoursParsed, dayOfMonthParsed, monthParsed secondsParsed, minutesParsed, hoursParsed, dayOfMonthParsed, monthParsed
) )
} }
/**
*
*/
fun buildSchedule(incoming: String): KronScheduler = createSimpleScheduler(incoming)

View File

@ -0,0 +1,63 @@
package com.insanusmokrassar.krontab.utils
import com.insanusmokrassar.krontab.buildSchedule
import com.insanusmokrassar.krontab.builder.buildSchedule
import com.insanusmokrassar.krontab.createSimpleScheduler
import com.soywiz.klock.DateTime
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile
import kotlin.test.Test
import kotlin.test.assertEquals
@ExperimentalCoroutinesApi
@FlowPreview
class StringParseTest {
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnString() {
val kronScheduler = buildSchedule("*/1 * * * *")
val flow = kronScheduler.asFlow()
runTest {
val mustBeCollected = 10
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
assertEquals(mustBeCollected, collected)
}
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondWithMuchOfEmittersBuiltOnString() {
val kronScheduler = buildSchedule("*/1 * * * *")
val flow = kronScheduler.asFlow()
runTest {
val testsCount = 10
val failJob = it.createFailJob((testsCount * 2) * 1000L)
val mustBeCollected = 10
val answers = (0 until testsCount).map { _ ->
it.async {
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
collected
}
}.awaitAll()
failJob.cancel()
answers.forEach {
assertEquals(mustBeCollected, it)
}
}
}
}