mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-26 03:58:50 +00:00
fixes in built-in schedulers
This commit is contained in:
parent
6845537afd
commit
2efd7e6157
@ -6,6 +6,7 @@
|
|||||||
* `Kotlin`: `1.9.23`
|
* `Kotlin`: `1.9.23`
|
||||||
* `Serialization`: `1.6.3`
|
* `Serialization`: `1.6.3`
|
||||||
* `Klock`: `5.3.2`
|
* `Klock`: `5.3.2`
|
||||||
|
* Fixes in build-in schedulers
|
||||||
|
|
||||||
## 2.2.8
|
## 2.2.8
|
||||||
|
|
||||||
|
@ -34,35 +34,60 @@ val EverySecondScheduler: KronScheduler by lazy {
|
|||||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one minute
|
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one minute
|
||||||
*/
|
*/
|
||||||
val EveryMinuteScheduler: KronScheduler by lazy {
|
val EveryMinuteScheduler: KronScheduler by lazy {
|
||||||
buildSchedule { minutes { 0 every 1 } }
|
buildSchedule {
|
||||||
|
seconds { at(0) }
|
||||||
|
minutes { 0 every 1 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one hour
|
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one hour
|
||||||
*/
|
*/
|
||||||
val EveryHourScheduler: KronScheduler by lazy {
|
val EveryHourScheduler: KronScheduler by lazy {
|
||||||
buildSchedule { hours { 0 every 1 } }
|
buildSchedule {
|
||||||
|
seconds { at(0) }
|
||||||
|
minutes { at(0) }
|
||||||
|
hours { 0 every 1 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one day
|
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one day
|
||||||
*/
|
*/
|
||||||
val EveryDayOfMonthScheduler: KronScheduler by lazy {
|
val EveryDayOfMonthScheduler: KronScheduler by lazy {
|
||||||
buildSchedule { dayOfMonth { 0 every 1 } }
|
buildSchedule {
|
||||||
|
seconds { at(0) }
|
||||||
|
minutes { at(0) }
|
||||||
|
hours { at(0) }
|
||||||
|
dayOfMonth { 0 every 1 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one month
|
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one month
|
||||||
*/
|
*/
|
||||||
val EveryMonthScheduler: KronScheduler by lazy {
|
val EveryMonthScheduler: KronScheduler by lazy {
|
||||||
buildSchedule { months { 0 every 1 } }
|
buildSchedule {
|
||||||
|
seconds { at(0) }
|
||||||
|
minutes { at(0) }
|
||||||
|
hours { at(0) }
|
||||||
|
dayOfMonth { at(0) }
|
||||||
|
months { 0 every 1 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one year
|
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one year
|
||||||
*/
|
*/
|
||||||
val EveryYearScheduler: KronScheduler by lazy {
|
val EveryYearScheduler: KronScheduler by lazy {
|
||||||
buildSchedule { years { 0 every 1 } }
|
buildSchedule {
|
||||||
|
seconds { at(0) }
|
||||||
|
minutes { at(0) }
|
||||||
|
hours { at(0) }
|
||||||
|
dayOfMonth { at(0) }
|
||||||
|
months { at(0) }
|
||||||
|
years { 0 every 1 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,15 +182,24 @@ class SchedulerBuilder(
|
|||||||
*/
|
*/
|
||||||
fun build(): KronScheduler = offset ?.let {
|
fun build(): KronScheduler = offset ?.let {
|
||||||
createKronSchedulerWithOffset(
|
createKronSchedulerWithOffset(
|
||||||
seconds,
|
seconds = seconds,
|
||||||
minutes,
|
minutes = minutes,
|
||||||
hours,
|
hours = hours,
|
||||||
dayOfMonth,
|
dayOfMonth = dayOfMonth,
|
||||||
month,
|
month = month,
|
||||||
year,
|
years = year,
|
||||||
dayOfWeek,
|
weekDays = dayOfWeek,
|
||||||
TimezoneOffset(it.minutes),
|
offset = TimezoneOffset(it.minutes),
|
||||||
milliseconds ?: millisecondsArrayDefault
|
milliseconds = milliseconds ?: millisecondsArrayDefault
|
||||||
|
)
|
||||||
|
} ?: createKronScheduler(
|
||||||
|
seconds = seconds,
|
||||||
|
minutes = minutes,
|
||||||
|
hours = hours,
|
||||||
|
dayOfMonth = dayOfMonth,
|
||||||
|
month = month,
|
||||||
|
years = year,
|
||||||
|
weekDays = dayOfWeek,
|
||||||
|
milliseconds = milliseconds ?: millisecondsArrayDefault
|
||||||
)
|
)
|
||||||
} ?: createKronScheduler(seconds, minutes, hours, dayOfMonth, month, year, dayOfWeek, milliseconds ?: millisecondsArrayDefault)
|
|
||||||
}
|
}
|
||||||
|
38
src/commonTest/kotlin/BuildersTest.kt
Normal file
38
src/commonTest/kotlin/BuildersTest.kt
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package dev.inmo.krontab.utils
|
||||||
|
|
||||||
|
import dev.inmo.krontab.*
|
||||||
|
import korlibs.time.*
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
|
class BuildersTest {
|
||||||
|
@Test
|
||||||
|
fun presetsWorksCorrectly() {
|
||||||
|
val data = mapOf(
|
||||||
|
EverySecondScheduler to { it: DateTime -> if (it.milliseconds > 0 ) it + 1.seconds - it.milliseconds.milliseconds else it },
|
||||||
|
EveryMinuteScheduler to { it: DateTime -> if (it.seconds > 0 || it.milliseconds > 0 ) it + 1.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
|
||||||
|
EveryHourScheduler to { it: DateTime -> if (it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) it + 1.hours - it.minutes.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
|
||||||
|
EveryDayOfMonthScheduler to { it: DateTime -> if (it.hours > 0 || it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) it + 1.days - it.hours.hours - it.minutes.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
|
||||||
|
EveryMonthScheduler to { it: DateTime -> if (it.dayOfMonth > 1 || it.hours > 0 || it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) (it + 1.months).copy(dayOfMonth = 1, hour = 0, minute = 0, second = 0, milliseconds = 0) else it },
|
||||||
|
)
|
||||||
|
val samples = 10000
|
||||||
|
|
||||||
|
runTest {
|
||||||
|
var now = DateTime.now()
|
||||||
|
for (i in 0 until samples) {
|
||||||
|
data.forEach { (scheduler, expectCalculator) ->
|
||||||
|
val expectValue = expectCalculator(now)
|
||||||
|
val newNow = scheduler.nextOrRelative(now)
|
||||||
|
|
||||||
|
assertEquals(expectValue, newNow, "For time ${now.toStringDefault()} calculated wrong value: ${newNow.toStringDefault()} is not equal to ${expectValue.toStringDefault()}")
|
||||||
|
|
||||||
|
now = newNow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user