Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler: register ApplicationNotRunning as bean even if quartz is used #37421

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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