diff --git a/zio1/src/test/scala/io/github/jkobej/cron/Tests.scala b/zio1/src/test/scala/io/github/jkobej/cron/Tests.scala index 6730bb9..f6e914e 100644 --- a/zio1/src/test/scala/io/github/jkobej/cron/Tests.scala +++ b/zio1/src/test/scala/io/github/jkobej/cron/Tests.scala @@ -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()) @@ -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) } ) } diff --git a/zio2/src/test/scala/io/github/jkobej/cron/Tests.scala b/zio2/src/test/scala/io/github/jkobej/cron/Tests.scala index 852b1a2..910afc6 100644 --- a/zio2/src/test/scala/io/github/jkobej/cron/Tests.scala +++ b/zio2/src/test/scala/io/github/jkobej/cron/Tests.scala @@ -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()) @@ -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) } ) }