forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#18060 from mkouba/scheduler-conditional-…
…exec Scheduler - introduce Scheduled#skipExecutionIf()
- Loading branch information
Showing
13 changed files
with
428 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ions/quartz/deployment/src/test/java/io/quarkus/quartz/test/ConditionalExecutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.inject.Singleton; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
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.ScheduledExecution; | ||
import io.quarkus.scheduler.SkippedExecution; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConditionalExecutionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Jobs.class)); | ||
|
||
@Test | ||
public void testExecution() { | ||
try { | ||
// Wait until Jobs#doSomething() is executed at least 1x and skipped 1x | ||
if (IsDisabled.SKIPPED_LATCH.await(10, TimeUnit.SECONDS)) { | ||
assertEquals(1, Jobs.COUNTER.getCount()); | ||
IsDisabled.DISABLED.set(false); | ||
} else { | ||
fail("Job#foo not skipped in 10 seconds!"); | ||
} | ||
if (!Jobs.COUNTER.await(10, TimeUnit.SECONDS)) { | ||
fail("Job#foo not executed in 10 seconds!"); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch COUNTER = new CountDownLatch(1); | ||
|
||
@Scheduled(identity = "foo", every = "1s", skipExecutionIf = IsDisabled.class) | ||
void doSomething() throws InterruptedException { | ||
COUNTER.countDown(); | ||
} | ||
|
||
} | ||
|
||
@Singleton | ||
public static class IsDisabled implements Scheduled.SkipPredicate { | ||
|
||
static final CountDownLatch SKIPPED_LATCH = new CountDownLatch(1); | ||
|
||
static final AtomicBoolean DISABLED = new AtomicBoolean(true); | ||
|
||
@Override | ||
public boolean test(ScheduledExecution execution) { | ||
return DISABLED.get(); | ||
} | ||
|
||
void onSkip(@Observes SkippedExecution event) { | ||
if (event.triggerId.equals("foo")) { | ||
System.out.println(event); | ||
SKIPPED_LATCH.countDown(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...cheduler/deployment/src/test/java/io/quarkus/scheduler/test/ConditionalExecutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkus.scheduler.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.inject.Singleton; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
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.ScheduledExecution; | ||
import io.quarkus.scheduler.SkippedExecution; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConditionalExecutionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Jobs.class)); | ||
|
||
@Test | ||
public void testExecution() { | ||
try { | ||
// Wait until Jobs#doSomething() is executed at least 1x and skipped 1x | ||
if (IsDisabled.SKIPPED_LATCH.await(10, TimeUnit.SECONDS)) { | ||
assertEquals(1, Jobs.COUNTER.getCount()); | ||
IsDisabled.DISABLED.set(false); | ||
} else { | ||
fail("Job#foo not skipped in 10 seconds!"); | ||
} | ||
if (!Jobs.COUNTER.await(10, TimeUnit.SECONDS)) { | ||
fail("Job#foo not executed in 10 seconds!"); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch COUNTER = new CountDownLatch(1); | ||
|
||
@Scheduled(identity = "foo", every = "1s", skipExecutionIf = IsDisabled.class) | ||
void doSomething() throws InterruptedException { | ||
COUNTER.countDown(); | ||
} | ||
|
||
} | ||
|
||
@Singleton | ||
public static class IsDisabled implements Scheduled.SkipPredicate { | ||
|
||
static final CountDownLatch SKIPPED_LATCH = new CountDownLatch(1); | ||
|
||
static final AtomicBoolean DISABLED = new AtomicBoolean(true); | ||
|
||
@Override | ||
public boolean test(ScheduledExecution execution) { | ||
return DISABLED.get(); | ||
} | ||
|
||
void onSkip(@Observes SkippedExecution event) { | ||
if (event.triggerId.equals("foo")) { | ||
|
||
SKIPPED_LATCH.countDown(); | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.