Skip to content

Commit

Permalink
Merge pull request #2 from cagizoz/fix-tests-for-timezone-scheduling
Browse files Browse the repository at this point in the history
Fix tests for timezone scheduling
  • Loading branch information
jkobejs authored Nov 15, 2022
2 parents 753724f + 5f6f476 commit 746857f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
40 changes: 36 additions & 4 deletions zio1/src/test/scala/io/github/jkobej/cron/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ object Tests extends DefaultRunnableSpec {
either <- fork.join
} yield assert(either)(Assertion.isLeft(Assertion.equalTo(error)))
},
testM("runs at specific time within other zone") {
testM("runs at specific time within Europe/Berlin timezone") {
val set = (1 to 12).map(dateTime.withMonth).toSet
val expected = set.map(_.atZone(ZoneId.of("Europe/Berlin")).withZoneSameInstant(ZoneId.systemDefault())).map(_.toLocalDateTime)

val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(6))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
Expand All @@ -84,11 +87,40 @@ object Tests extends DefaultRunnableSpec {
} yield ()
_ <- TestClock.setTime(Duration.between(Instant.EPOCH, startInstant))
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/London")).fork
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/Berlin")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == (1 to 12).map(dateTime.withMonth).toSet)
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == expected)
},
testM("runs at specific time within UTC timezone") {
val set = (1 to 12).map(dateTime.withMonth).toSet
val expected = set.map(_.atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault())).map(_.toLocalDateTime)

val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(6))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
.withYear(FieldExpressionFactory.on(2022))
.instance()

for {
datesRef <- Ref.make[List[LocalDateTime]](Nil)
eff = for {
date <- zio.clock.localDateTime.orDie
_ <- datesRef.update(dates => date :: dates)
} yield ()
_ <- TestClock.setTime(Duration.between(Instant.EPOCH, startInstant))
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("UTC")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == expected)
}
)
}
40 changes: 36 additions & 4 deletions zio2/src/test/scala/io/github/jkobej/cron/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ object Tests extends ZIOSpecDefault {
either <- fork.join
} yield assert(either)(Assertion.isLeft(Assertion.equalTo(error)))
},
test("runs at specific time within timezone") {
test("runs at specific time within UTC timezone") {
val set = (1 to 12).map(dateTime.withMonth).toSet
val expected = set.map(_.atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault())).map(_.toLocalDateTime)

val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(6))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
Expand All @@ -83,11 +86,40 @@ object Tests extends ZIOSpecDefault {
} yield ()
_ <- TestClock.setTime(startInstant)
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/London")).fork
fork <- eff.repeatWithCron(cron, ZoneId.of("UTC")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == (1 to 12).map(dateTime.withMonth).toSet)
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == expected)
},
test("runs at specific time within Europe/Berlin timezone") {
val set = (1 to 12).map(dateTime.withMonth).toSet
val expected = set.map(_.atZone(ZoneId.of("Europe/Berlin")).withZoneSameInstant(ZoneId.systemDefault())).map(_.toLocalDateTime)

val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(6))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
.withYear(FieldExpressionFactory.on(2022))
.instance()

for {
datesRef <- Ref.make[List[LocalDateTime]](Nil)
eff = for {
date <- zio.Clock.localDateTime
_ <- datesRef.update(dates => date :: dates)
} yield ()
_ <- TestClock.setTime(startInstant)
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/Berlin")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == expected)
}
)
}

0 comments on commit 746857f

Please sign in to comment.