|
| 1 | +package io.quarkus.quartz.test.timezone; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.time.Instant; |
| 8 | +import java.time.ZoneId; |
| 9 | +import java.time.ZonedDateTime; |
| 10 | +import java.util.concurrent.CountDownLatch; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import jakarta.inject.Inject; |
| 14 | + |
| 15 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 16 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 19 | + |
| 20 | +import io.quarkus.scheduler.Scheduled; |
| 21 | +import io.quarkus.scheduler.Scheduler; |
| 22 | +import io.quarkus.scheduler.Trigger; |
| 23 | +import io.quarkus.test.QuarkusUnitTest; |
| 24 | + |
| 25 | +public class TriggerPrevFireTimeZoneTest { |
| 26 | + |
| 27 | + @RegisterExtension |
| 28 | + static final QuarkusUnitTest test = new QuarkusUnitTest() |
| 29 | + .withApplicationRoot(root -> { |
| 30 | + ZonedDateTime now = ZonedDateTime.now(); |
| 31 | + ZonedDateTime prague = now.withZoneSameInstant(ZoneId.of("Europe/Prague")); |
| 32 | + ZonedDateTime istanbul = now.withZoneSameInstant(ZoneId.of("Europe/Istanbul")); |
| 33 | + // For example, the current date-time is 2024-07-09 10:08:00; |
| 34 | + // the default time zone is Europe/London |
| 35 | + // then the config should look like: |
| 36 | + // simpleJobs1.cron=0/1 * 11 * * ? |
| 37 | + // simpleJobs2.cron=0/1 * 12 * * ? |
| 38 | + String properties = String.format( |
| 39 | + "simpleJobs1.cron=0/1 * %s * * ?\n" |
| 40 | + + "simpleJobs1.hour=%s\n" |
| 41 | + + "simpleJobs2.cron=0/1 * %s * * ?\n" |
| 42 | + + "simpleJobs2.hour=%s", |
| 43 | + prague.getHour(), prague.getHour(), istanbul.getHour(), istanbul.getHour()); |
| 44 | + root.addClasses(Jobs.class) |
| 45 | + .addAsResource( |
| 46 | + new StringAsset(properties), |
| 47 | + "application.properties"); |
| 48 | + }); |
| 49 | + |
| 50 | + @ConfigProperty(name = "simpleJobs1.hour") |
| 51 | + int pragueHour; |
| 52 | + |
| 53 | + @ConfigProperty(name = "simpleJobs2.hour") |
| 54 | + int istanbulHour; |
| 55 | + |
| 56 | + @Inject |
| 57 | + Scheduler scheduler; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testScheduledJobs() throws InterruptedException { |
| 61 | + assertTrue(Jobs.PRAGUE_LATCH.await(5, TimeUnit.SECONDS)); |
| 62 | + assertTrue(Jobs.ISTANBUL_LATCH.await(5, TimeUnit.SECONDS)); |
| 63 | + Trigger prague = scheduler.getScheduledJob("prague"); |
| 64 | + Trigger istanbul = scheduler.getScheduledJob("istanbul"); |
| 65 | + assertNotNull(prague); |
| 66 | + assertNotNull(istanbul); |
| 67 | + Instant praguePrev = prague.getPreviousFireTime(); |
| 68 | + Instant istanbulPrev = istanbul.getPreviousFireTime(); |
| 69 | + assertNotNull(praguePrev); |
| 70 | + assertNotNull(istanbulPrev); |
| 71 | + assertEquals(praguePrev, istanbulPrev); |
| 72 | + assertEquals(pragueHour, praguePrev.atZone(ZoneId.of("Europe/Prague")).getHour()); |
| 73 | + assertEquals(istanbulHour, istanbulPrev.atZone(ZoneId.of("Europe/Istanbul")).getHour()); |
| 74 | + } |
| 75 | + |
| 76 | + static class Jobs { |
| 77 | + |
| 78 | + static final CountDownLatch PRAGUE_LATCH = new CountDownLatch(1); |
| 79 | + static final CountDownLatch ISTANBUL_LATCH = new CountDownLatch(1); |
| 80 | + |
| 81 | + @Scheduled(identity = "prague", cron = "{simpleJobs1.cron}", timeZone = "Europe/Prague") |
| 82 | + void withPragueTimezone() { |
| 83 | + PRAGUE_LATCH.countDown(); |
| 84 | + } |
| 85 | + |
| 86 | + @Scheduled(identity = "istanbul", cron = "{simpleJobs2.cron}", timeZone = "Europe/Istanbul") |
| 87 | + void withIstanbulTimezone() { |
| 88 | + ISTANBUL_LATCH.countDown(); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments