|
| 1 | +package io.quarkus.quartz.test; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | + |
| 5 | +import java.util.concurrent.CountDownLatch; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | + |
| 9 | +import jakarta.inject.Inject; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 13 | +import org.quartz.DisallowConcurrentExecution; |
| 14 | +import org.quartz.Job; |
| 15 | +import org.quartz.JobBuilder; |
| 16 | +import org.quartz.JobDetail; |
| 17 | +import org.quartz.JobExecutionContext; |
| 18 | +import org.quartz.JobExecutionException; |
| 19 | +import org.quartz.SchedulerException; |
| 20 | +import org.quartz.SimpleScheduleBuilder; |
| 21 | +import org.quartz.Trigger; |
| 22 | +import org.quartz.TriggerBuilder; |
| 23 | + |
| 24 | +import io.quarkus.quartz.QuartzScheduler; |
| 25 | +import io.quarkus.scheduler.Scheduled; |
| 26 | +import io.quarkus.scheduler.Scheduler; |
| 27 | +import io.quarkus.test.QuarkusUnitTest; |
| 28 | + |
| 29 | +public class NonconcurrentProgrammaticTest { |
| 30 | + |
| 31 | + @RegisterExtension |
| 32 | + static final QuarkusUnitTest test = new QuarkusUnitTest() |
| 33 | + .withApplicationRoot(root -> root |
| 34 | + .addClasses(Jobs.class)) |
| 35 | + .overrideConfigKey("quarkus.scheduler.start-mode", "halted"); |
| 36 | + |
| 37 | + @Inject |
| 38 | + QuartzScheduler scheduler; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testExecution() throws SchedulerException, InterruptedException { |
| 42 | + JobDetail job = JobBuilder.newJob(Jobs.class) |
| 43 | + .withIdentity("foo", Scheduler.class.getName()) |
| 44 | + .build(); |
| 45 | + Trigger trigger = TriggerBuilder.newTrigger() |
| 46 | + .withIdentity("foo", Scheduler.class.getName()) |
| 47 | + .startNow() |
| 48 | + .withSchedule(SimpleScheduleBuilder.simpleSchedule() |
| 49 | + .withIntervalInSeconds(1) |
| 50 | + .repeatForever()) |
| 51 | + .build(); |
| 52 | + scheduler.getScheduler().scheduleJob(job, trigger); |
| 53 | + |
| 54 | + scheduler.resume(); |
| 55 | + |
| 56 | + assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), |
| 57 | + String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); |
| 58 | + } |
| 59 | + |
| 60 | + @DisallowConcurrentExecution |
| 61 | + static class Jobs implements Job { |
| 62 | + |
| 63 | + static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); |
| 64 | + static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); |
| 65 | + |
| 66 | + static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); |
| 67 | + |
| 68 | + @Scheduled(identity = "bar", every = "1s") |
| 69 | + void concurrent() throws InterruptedException { |
| 70 | + CONCURRENT_LATCH.countDown(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void execute(JobExecutionContext context) throws JobExecutionException { |
| 75 | + Jobs.NONCONCURRENT_COUNTER.incrementAndGet(); |
| 76 | + try { |
| 77 | + if (!Jobs.CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { |
| 78 | + throw new IllegalStateException("nonconcurrent() execution blocked too long..."); |
| 79 | + } |
| 80 | + } catch (InterruptedException e) { |
| 81 | + throw new IllegalStateException(e); |
| 82 | + } |
| 83 | + if (Jobs.NONCONCURRENT_COUNTER.get() == 1) { |
| 84 | + // concurrent() executed >= 5x and nonconcurrent() 1x |
| 85 | + Jobs.NONCONCURRENT_LATCH.countDown(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments