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.
- fixes quarkusio#44048
- Loading branch information
Showing
16 changed files
with
474 additions
and
80 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
69 changes: 69 additions & 0 deletions
69
...uartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentJobDefinitionTest.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,69 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.QuartzScheduler; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentJobDefinitionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.scheduler.start-mode", "forced") | ||
.overrideConfigKey("quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread", | ||
"true"); | ||
|
||
@Inject | ||
QuartzScheduler scheduler; | ||
|
||
@Test | ||
public void testExecution() throws InterruptedException { | ||
scheduler.newJob("foo") | ||
.setTask(se -> { | ||
Jobs.NONCONCURRENT_COUNTER.incrementAndGet(); | ||
try { | ||
if (!Jobs.CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
if (Jobs.NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
Jobs.NONCONCURRENT_LATCH.countDown(); | ||
} | ||
}) | ||
.setInterval("1s") | ||
.setNonconcurrent() | ||
.schedule(); | ||
|
||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...quartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentProgrammaticTest.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,91 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.quartz.DisallowConcurrentExecution; | ||
import org.quartz.Job; | ||
import org.quartz.JobBuilder; | ||
import org.quartz.JobDetail; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.SimpleScheduleBuilder; | ||
import org.quartz.Trigger; | ||
import org.quartz.TriggerBuilder; | ||
|
||
import io.quarkus.quartz.QuartzScheduler; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.Scheduler; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentProgrammaticTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.scheduler.start-mode", "halted"); | ||
|
||
@Inject | ||
QuartzScheduler scheduler; | ||
|
||
@Test | ||
public void testExecution() throws SchedulerException, InterruptedException { | ||
JobDetail job = JobBuilder.newJob(Jobs.class) | ||
.withIdentity("foo", Scheduler.class.getName()) | ||
.build(); | ||
Trigger trigger = TriggerBuilder.newTrigger() | ||
.withIdentity("foo", Scheduler.class.getName()) | ||
.startNow() | ||
.withSchedule(SimpleScheduleBuilder.simpleSchedule() | ||
.withIntervalInSeconds(1) | ||
.repeatForever()) | ||
.build(); | ||
scheduler.getScheduler().scheduleJob(job, trigger); | ||
|
||
scheduler.resume(); | ||
|
||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
@DisallowConcurrentExecution | ||
static class Jobs implements Job { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
@Override | ||
public void execute(JobExecutionContext context) throws JobExecutionException { | ||
Jobs.NONCONCURRENT_COUNTER.incrementAndGet(); | ||
try { | ||
if (!Jobs.CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
if (Jobs.NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
Jobs.NONCONCURRENT_LATCH.countDown(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
extensions/quartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentTest.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,56 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.Nonconcurrent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread", | ||
"true"); | ||
|
||
@Test | ||
public void testExecution() throws InterruptedException { | ||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Nonconcurrent | ||
@Scheduled(identity = "foo", every = "1s") | ||
void nonconcurrent() throws InterruptedException { | ||
NONCONCURRENT_COUNTER.incrementAndGet(); | ||
if (!CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
if (NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
NONCONCURRENT_LATCH.countDown(); | ||
} | ||
} | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...z/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentValidationFailureTest.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,35 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.Nonconcurrent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentValidationFailureTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(Jobs.class)) | ||
.setExpectedException(IllegalStateException.class, true) | ||
.overrideConfigKey("quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread", | ||
"false"); | ||
|
||
@Test | ||
public void test() throws InterruptedException { | ||
fail(); | ||
} | ||
|
||
static class Jobs { | ||
|
||
@Nonconcurrent | ||
@Scheduled(identity = "foo", every = "1s") | ||
void nonconcurrent() throws InterruptedException { | ||
} | ||
|
||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
extensions/quartz/runtime/src/main/java/io/quarkus/quartz/Nonconcurrent.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,31 @@ | ||
package io.quarkus.quartz; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import org.quartz.DisallowConcurrentExecution; | ||
import org.quartz.Job; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.SkippedExecution; | ||
|
||
/** | ||
* Annotated scheduled method may not be executed concurrently. The behavior is identical to a {@link Job} class annotated with | ||
* {@link DisallowConcurrentExecution}. | ||
* <p> | ||
* This annotation can be only used if {@code quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread} is set to | ||
* {@code true}, otherwise the application startup fails. | ||
* <p> | ||
* Unlike with {@link Scheduled.ConcurrentExecution#SKIP} the {@link SkippedExecution} event is never fired if a method | ||
* execution is skipped by Quartz. | ||
* | ||
* @see DisallowConcurrentExecution | ||
*/ | ||
@Target(METHOD) | ||
@Retention(RUNTIME) | ||
public @interface Nonconcurrent { | ||
|
||
} |
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
Oops, something went wrong.