From 5bbc400a2adf835ca8fdcc533077683a684f546e Mon Sep 17 00:00:00 2001 From: Rustam Sultansoy Date: Sat, 28 Nov 2020 01:56:03 +0200 Subject: [PATCH] Fix SimpleScheduler pause functionality. Add return in SimpleScheduler::checkTriggers method if !running. Unit test for SimpleScheduler::pause functionality Pause and check isRunning returns false and scheduler doesn't trigger CountDownLatch::countDown --- .../scheduler/test/PausedSchedulerTest.java | 48 +++++++++++++++++++ .../scheduler/runtime/SimpleScheduler.java | 1 + 2 files changed, 49 insertions(+) create mode 100644 extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/PausedSchedulerTest.java diff --git a/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/PausedSchedulerTest.java b/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/PausedSchedulerTest.java new file mode 100644 index 0000000000000..0a2fd629bdb3d --- /dev/null +++ b/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/PausedSchedulerTest.java @@ -0,0 +1,48 @@ +package io.quarkus.scheduler.test; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import javax.inject.Inject; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.scheduler.Scheduled; +import io.quarkus.scheduler.Scheduler; +import io.quarkus.test.QuarkusUnitTest; + +public class PausedSchedulerTest { + + @RegisterExtension + static final QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClasses(PausedSchedulerTest.Jobs.class) + .addAsResource(new StringAsset("quarkus.scheduler.enabled=true"), + "application.properties")); + + @Inject + Scheduler scheduler; + + @Test + public void testSchedulerPauseMethod() throws InterruptedException { + scheduler.pause(); + assertFalse(scheduler.isRunning()); + assertFalse(Jobs.LATCH.await(3, TimeUnit.SECONDS)); + } + + static class Jobs { + static final CountDownLatch LATCH = new CountDownLatch(2); + + @Scheduled(every = "1s") + void countDownSecond() { + LATCH.countDown(); + } + } + +} diff --git a/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java b/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java index d5c1aea3e6d8c..d80c32208f48f 100644 --- a/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java +++ b/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java @@ -122,6 +122,7 @@ void stop() { void checkTriggers() { if (!running) { LOGGER.trace("Skip all triggers - scheduler paused"); + return; } ZonedDateTime now = ZonedDateTime.now(); for (ScheduledTask task : scheduledTasks) {