mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 08:13:54 +00:00
fixes in built-in schedulers
This commit is contained in:
parent
6845537afd
commit
2efd7e6157
@ -6,6 +6,7 @@
|
||||
* `Kotlin`: `1.9.23`
|
||||
* `Serialization`: `1.6.3`
|
||||
* `Klock`: `5.3.2`
|
||||
* Fixes in build-in schedulers
|
||||
|
||||
## 2.2.8
|
||||
|
||||
|
@ -34,35 +34,60 @@ val EverySecondScheduler: KronScheduler by lazy {
|
||||
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one minute
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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 {
|
||||
createKronSchedulerWithOffset(
|
||||
seconds,
|
||||
minutes,
|
||||
hours,
|
||||
dayOfMonth,
|
||||
month,
|
||||
year,
|
||||
dayOfWeek,
|
||||
TimezoneOffset(it.minutes),
|
||||
milliseconds ?: millisecondsArrayDefault
|
||||
seconds = seconds,
|
||||
minutes = minutes,
|
||||
hours = hours,
|
||||
dayOfMonth = dayOfMonth,
|
||||
month = month,
|
||||
years = year,
|
||||
weekDays = dayOfWeek,
|
||||
offset = TimezoneOffset(it.minutes),
|
||||
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