Skip to content

Commit

Permalink
fix: update kotlin tests for scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgriffing committed Feb 24, 2024
1 parent dd96d19 commit 36876d8
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 61 deletions.
24 changes: 9 additions & 15 deletions clients/kotlin/src/main/kotlin/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object Scheduler {

fun isScheduleActiveWithNow(schedule: Schedule, scheduleType: ScheduleType, now: Instant): Boolean {

val nowMillis = now.toEpochMilliseconds()

when (scheduleType) {
ScheduleType.EMPTY -> return true
ScheduleType.GLOBAL,
Expand Down Expand Up @@ -46,27 +48,19 @@ object Scheduler {
when (schedule.timeType) {
ScheduleTimeType.NONE -> return true
ScheduleTimeType.START_END -> {
val startDateTime = LocalDateTime(
startDate.toLocalDateTime(TimeZone.UTC).date,
LocalTime(
startTime.hour,
startTime.minute,
startTime.second,
startTime.nanosecond
)
).toInstant(TimeZone.UTC)

val endDateTime = LocalDateTime(
val startOfEndDate = LocalDateTime(
endDate.toLocalDateTime(TimeZone.UTC).date,
LocalTime(
endTime.hour,
endTime.minute,
endTime.second,
endTime.nanosecond
0, 0, 0, 0
)
).toInstant(TimeZone.UTC)

return (startDateTime..endDateTime).contains(now)
val startDateTimestampWithStartTime =
startOfStartDate.toEpochMilliseconds() + schedule.startTime
val endDateTimestampWithEndTime = startOfEndDate.toEpochMilliseconds() + schedule.endTime

return nowMillis in startDateTimestampWithStartTime..endDateTimestampWithEndTime
}

ScheduleTimeType.DAILY -> {
Expand Down
279 changes: 233 additions & 46 deletions clients/kotlin/src/test/kotlin/SchedulerTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dev.vexilla.*
import kotlinx.datetime.*
import org.junit.jupiter.api.Test
//import java.time.Duration
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.days
import kotlin.test.*


Expand All @@ -24,53 +24,130 @@ class SchedulerTest {


@Test
fun `should handle start_end schedules`() {
fun `should handle start_end single day schedules`() {
val now = Clock.System.now()
val zeroDay = Instant.fromEpochMilliseconds(0)

val beforeSchedule = Schedule(
start = now.plus(1, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
end = now.plus(3, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = 0,
endTime = 0
)
for (hour in 0..23) {

val isBeforeScheduleActive = Scheduler.isScheduleActive(beforeSchedule, ScheduleType.GLOBAL)
val mockedNow =
LocalDateTime(
now.toLocalDateTime(TimeZone.UTC).date,
LocalTime(hour, 0, 0, 0)
).toInstant(
TimeZone.UTC
)

assertFalse(isBeforeScheduleActive)
val zeroDayWithMockedTime = Instant.fromEpochMilliseconds(0).plus(hour.hours)

val duringSchedule = Schedule(
start = now.minus(1, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
end = now.plus(1, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = 0,
endTime = 0
)
val beforeSchedule = Schedule(
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isDuringScheduleActive = Scheduler.isScheduleActive(duringSchedule, ScheduleType.GLOBAL)
val isBeforeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeSchedule, ScheduleType.GLOBAL, mockedNow)

assertTrue(isDuringScheduleActive)
assertFalse(isBeforeScheduleActive, "Hour: ${hour}: Before test failed")

val duringSchedule = Schedule(
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds()
)

val afterSchedule = Schedule(
start = now.minus(3, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
end = now.minus(1, DateTimeUnit.DAY, TimeZone.UTC).epochSeconds * 1000,
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = 0,
endTime = 0
)
val isDuringScheduleActive =
Scheduler.isScheduleActiveWithNow(duringSchedule, ScheduleType.GLOBAL, mockedNow)

assertTrue(isDuringScheduleActive, "Hour: ${hour}: During test failed")


val afterSchedule = Schedule(
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterScheduleActive = Scheduler.isScheduleActive(afterSchedule, ScheduleType.GLOBAL)
val isAfterScheduleActive = Scheduler.isScheduleActiveWithNow(afterSchedule, ScheduleType.GLOBAL, mockedNow)

assertFalse(isAfterScheduleActive)
assertFalse(isAfterScheduleActive, "Hour: ${hour}: During test failed")
}
}

@Test
fun `should handle start_end multi day schedules`() {
val now = Clock.System.now()
val zeroDay = Instant.fromEpochMilliseconds(0)

for (hour in 0..23) {

val mockedNow =
LocalDateTime(
now.toLocalDateTime(TimeZone.UTC).date,
LocalTime(hour, 0, 0, 0)
).toInstant(
TimeZone.UTC
)

val zeroDayWithMockedTime = Instant.fromEpochMilliseconds(0).plus(hour.hours)

val beforeSchedule = Schedule(
start = mockedNow.plus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(3.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isBeforeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeSchedule, ScheduleType.GLOBAL, mockedNow)

assertFalse(isBeforeScheduleActive)

val duringSchedule = Schedule(
start = mockedNow.minus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds()
)

val isDuringScheduleActive =
Scheduler.isScheduleActiveWithNow(duringSchedule, ScheduleType.GLOBAL, mockedNow)

assertTrue(isDuringScheduleActive)


val afterSchedule = Schedule(
start = mockedNow.minus(3.days).toEpochMilliseconds(),
end = mockedNow.minus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.START_END,
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterScheduleActive = Scheduler.isScheduleActiveWithNow(afterSchedule, ScheduleType.GLOBAL, mockedNow)

assertFalse(isAfterScheduleActive)
}
}


@Test
fun `should handle daily schedules`() {
fun `should handle daily single day schedules`() {
for (hour in 0..23) {
val now = Clock.System.now().toLocalDateTime(TimeZone.UTC)

Expand All @@ -82,43 +159,153 @@ class SchedulerTest {
TimeZone.UTC
)

val zeroDayWithMockedTime = Instant.fromEpochMilliseconds(0).plus(hour.hours)

val beforeWholeSchedule = Schedule(
start = mockedNow.plus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isBeforeWholeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeWholeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isBeforeWholeScheduleActive, "Hour: ${hour}: Before whole test failed")

val beforeSchedule = Schedule(
start = mockedNow.minus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
end = mockedNow.plus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = mockedNow.plus(1, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds(),
endTime = mockedNow.plus(3, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds()
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isBeforeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isBeforeScheduleActive, "Hour: ${hour}: Before test failed")

val duringSchedule = Schedule(
start = mockedNow.minus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
end = mockedNow.plus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = mockedNow.minus(1, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds(),
endTime = mockedNow.plus(1, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds()
startTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds()
)

val isDuringScheduleActive =
Scheduler.isScheduleActiveWithNow(duringSchedule, ScheduleType.GLOBAL, mockedNow)
assertTrue(isDuringScheduleActive, "Hour: ${hour}: During test failed")

val afterSchedule = Schedule(
start = mockedNow.minus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
end = mockedNow.plus(1, DateTimeUnit.DAY, TimeZone.UTC).toEpochMilliseconds(),
start = mockedNow.toEpochMilliseconds(),
end = mockedNow.toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = mockedNow.minus(3, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds(),
endTime = mockedNow.minus(1, DateTimeUnit.HOUR, TimeZone.UTC).toEpochMilliseconds()
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterScheduleActive = Scheduler.isScheduleActiveWithNow(afterSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isAfterScheduleActive, "Hour: ${hour}: After test failed")

val afterWholeSchedule = Schedule(
start = mockedNow.minus(1.days).toEpochMilliseconds(),
end = mockedNow.minus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterWholeScheduleActive =
Scheduler.isScheduleActiveWithNow(afterWholeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isAfterWholeScheduleActive, "Hour: ${hour}: After whole test failed")
}
}


@Test
fun `should handle daily multi day schedules`() {
for (hour in 0..23) {
val now = Clock.System.now().toLocalDateTime(TimeZone.UTC)

val mockedNow =
LocalDateTime(
now.date,
LocalTime(hour, 0)
).toInstant(
TimeZone.UTC
)

val zeroDayWithMockedTime = Instant.fromEpochMilliseconds(0).plus(hour.hours)

val beforeWholeSchedule = Schedule(
start = mockedNow.plus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(3.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isBeforeWholeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeWholeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isBeforeWholeScheduleActive, "Hour: ${hour}: Before whole test failed")

val beforeSchedule = Schedule(
start = mockedNow.minus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(3.hours).toEpochMilliseconds()
)

val isBeforeScheduleActive =
Scheduler.isScheduleActiveWithNow(beforeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isBeforeScheduleActive, "Hour: ${hour}: Before test failed")

val duringSchedule = Schedule(
start = mockedNow.minus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.plus(1.hours).toEpochMilliseconds()
)

val isDuringScheduleActive =
Scheduler.isScheduleActiveWithNow(duringSchedule, ScheduleType.GLOBAL, mockedNow)
assertTrue(isDuringScheduleActive, "Hour: ${hour}: During test failed")

val afterSchedule = Schedule(
start = mockedNow.minus(1.days).toEpochMilliseconds(),
end = mockedNow.plus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterScheduleActive = Scheduler.isScheduleActiveWithNow(afterSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isAfterScheduleActive, "Hour: ${hour}: After test failed")

val afterWholeSchedule = Schedule(
start = mockedNow.minus(3.days).toEpochMilliseconds(),
end = mockedNow.minus(1.days).toEpochMilliseconds(),
timezone = "UTC",
timeType = ScheduleTimeType.DAILY,
startTime = zeroDayWithMockedTime.minus(3.hours).toEpochMilliseconds(),
endTime = zeroDayWithMockedTime.minus(1.hours).toEpochMilliseconds()
)

val isAfterWholeScheduleActive =
Scheduler.isScheduleActiveWithNow(afterWholeSchedule, ScheduleType.GLOBAL, mockedNow)
assertFalse(isAfterWholeScheduleActive, "Hour: ${hour}: After whole test failed")
}
}
}

0 comments on commit 36876d8

Please sign in to comment.