From 2efd7e615768308fa706eb34e87828ba974ab4d5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 Mar 2024 15:56:45 +0600 Subject: [PATCH] fixes in built-in schedulers --- CHANGELOG.md | 1 + src/commonMain/kotlin/SchedulerShortcuts.kt | 35 ++++++++++++++--- .../kotlin/builder/SchedulerBuilder.kt | 29 +++++++++----- src/commonTest/kotlin/BuildersTest.kt | 38 +++++++++++++++++++ 4 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 src/commonTest/kotlin/BuildersTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index dfba9ba..9638cd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * `Kotlin`: `1.9.23` * `Serialization`: `1.6.3` * `Klock`: `5.3.2` +* Fixes in build-in schedulers ## 2.2.8 diff --git a/src/commonMain/kotlin/SchedulerShortcuts.kt b/src/commonMain/kotlin/SchedulerShortcuts.kt index ad88b58..7070b9a 100644 --- a/src/commonMain/kotlin/SchedulerShortcuts.kt +++ b/src/commonMain/kotlin/SchedulerShortcuts.kt @@ -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 } + } } /** diff --git a/src/commonMain/kotlin/builder/SchedulerBuilder.kt b/src/commonMain/kotlin/builder/SchedulerBuilder.kt index 681f2c6..d7d299f 100644 --- a/src/commonMain/kotlin/builder/SchedulerBuilder.kt +++ b/src/commonMain/kotlin/builder/SchedulerBuilder.kt @@ -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, minutes, hours, dayOfMonth, month, year, dayOfWeek, milliseconds ?: millisecondsArrayDefault) + } ?: createKronScheduler( + seconds = seconds, + minutes = minutes, + hours = hours, + dayOfMonth = dayOfMonth, + month = month, + years = year, + weekDays = dayOfWeek, + milliseconds = milliseconds ?: millisecondsArrayDefault + ) } diff --git a/src/commonTest/kotlin/BuildersTest.kt b/src/commonTest/kotlin/BuildersTest.kt new file mode 100644 index 0000000..abb82cd --- /dev/null +++ b/src/commonTest/kotlin/BuildersTest.kt @@ -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 + } + } + } + } +} \ No newline at end of file