Skip to content

Commit

Permalink
Merge pull request quarkusio#37421 from mkouba/issue-37417
Browse files Browse the repository at this point in the history
Scheduler: register ApplicationNotRunning as bean even if quartz is used
  • Loading branch information
gsmet authored Dec 1, 2023
2 parents 31b92a0 + aa401f1 commit b748934
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.quartz.test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import jakarta.enterprise.event.Observes;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.runtime.StartupEvent;
import io.quarkus.scheduler.FailedExecution;
import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.SuccessfulExecution;
import io.quarkus.test.QuarkusUnitTest;

public class ApplicationNotRunningPredicateTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest().withApplicationRoot((jar) -> jar.addClasses(Jobs.class));

static final CountDownLatch SUCCESS_LATCH = new CountDownLatch(1);
static volatile FailedExecution failedExecution;

@Test
public void testTriggerErrorStatus() throws InterruptedException {
assertTrue(SUCCESS_LATCH.await(5, TimeUnit.SECONDS));
assertNull(failedExecution);
}

void observeSuccessfulExecution(@Observes SuccessfulExecution successfulExecution) {
SUCCESS_LATCH.countDown();
}

void observeFailedExecution(@Observes FailedExecution failedExecution) {
ApplicationNotRunningPredicateTest.failedExecution = failedExecution;
}

static class Jobs {

volatile boolean started;

void started(@Observes StartupEvent event) {
started = true;
}

@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class)
void scheduleAfterStarted() {
if (!started) {
throw new IllegalStateException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public class SchedulerProcessor {

@BuildStep
void beans(Capabilities capabilities, BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(Scheduled.ApplicationNotRunning.class));
if (capabilities.isMissing(Capability.QUARTZ)) {
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class, Scheduled.ApplicationNotRunning.class));
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Observes;
import jakarta.interceptor.Interceptor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -43,15 +41,15 @@ void observeFailedExecution(@Observes FailedExecution failedExecution) {

static class Jobs {

volatile boolean preStart;
volatile boolean started;

void started(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent event) {
preStart = true;
void started(@Observes StartupEvent event) {
started = true;
}

@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class)
void scheduleAfterStarted() {
if (!preStart) {
if (!started) {
throw new IllegalStateException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class OpenTelemetryQuartzTest {
@Test
public void quartzSpanTest() {
// ensure that scheduled job is called
assertCounter("/scheduler/count", 1, Duration.ofSeconds(1));
assertCounter("/scheduler/count", 1, Duration.ofSeconds(3));
// assert programmatically scheduled job is called
assertCounter("/scheduler/count/manual", 1, Duration.ofSeconds(1));
assertCounter("/scheduler/count/manual", 1, Duration.ofSeconds(3));
// assert JobDefinition type scheduler
assertCounter("/scheduler/count/job-definition", 1, Duration.ofSeconds(1));

Expand All @@ -53,6 +53,7 @@ public void quartzSpanTest() {

private void assertCounter(String counterPath, int expectedCount, Duration timeout) {
await().atMost(timeout)
.pollInterval(Duration.ofMillis(500))
.until(() -> {
Response response = given().when().get(counterPath);
int code = response.statusCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class OpenTelemetrySchedulerTest {
@Test
public void schedulerSpanTest() {
// ensure that scheduled job is called
assertCounter("/scheduler/count", 1, Duration.ofSeconds(1));
assertCounter("/scheduler/count", 1, Duration.ofSeconds(3));
// assert JobDefinition type scheduler
assertCounter("/scheduler/count/job-definition", 1, Duration.ofSeconds(1));
assertCounter("/scheduler/count/job-definition", 1, Duration.ofSeconds(3));

// ------- SPAN ASSERTS -------
List<Map<String, Object>> spans = getSpans();
Expand All @@ -48,6 +48,7 @@ public void schedulerSpanTest() {

private void assertCounter(String counterPath, int expectedCount, Duration timeout) {
await().atMost(timeout)
.pollInterval(Duration.ofMillis(500))
.until(() -> {
Response response = given().when().get(counterPath);
int code = response.statusCode();
Expand Down

0 comments on commit b748934

Please sign in to comment.