Skip to content

Commit

Permalink
Make Quarz test more reliable (#1144)
Browse files Browse the repository at this point in the history
closes #1107
  • Loading branch information
felixbarny authored Apr 17, 2020
1 parent 135c52e commit 110da2a
Showing 1 changed file with 18 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.TracerInternalApiUtils;
import co.elastic.apm.agent.impl.transaction.Transaction;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -45,42 +47,39 @@
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;


class JobTransactionNameInstrumentationTest extends AbstractInstrumentationTest {

private static Scheduler scheduler;
private static CompletableFuture<Boolean> responseFuture;
private Scheduler scheduler;

@BeforeAll
private static void setup() throws SchedulerException {
@BeforeEach
private void prepare() throws SchedulerException {
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
}

@AfterAll
private static void cleanup() throws SchedulerException {
@AfterEach
private void cleanup() throws SchedulerException {
scheduler.shutdown();
reporter.assertRecycledAfterDecrementingReferences();
}

@BeforeEach
private void prepare() throws SchedulerException {
responseFuture = new CompletableFuture<>();
}

void verifyJobDetails(JobDetail job) throws InterruptedException {
reporter.getFirstTransaction(150);
assertThat(reporter.getTransactions().size()).isEqualTo(1);
void verifyJobDetails(JobDetail job) {
await().untilAsserted(() -> assertThat(reporter.getTransactions().size()).isEqualTo(1));
assertThat(reporter.getTransactions().get(0).getType()).isEqualToIgnoringCase("scheduled");
assertThat(reporter.getTransactions().get(0).getNameAsString())
.isEqualToIgnoringCase(String.format("%s.%s", job.getKey().getGroup(), job.getKey().getName()));
}

@Test
void testJobWithGroup() throws SchedulerException, InterruptedException, ExecutionException {
void testJobWithGroup() throws SchedulerException {
JobDetail job = JobBuilder.newJob(TestJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
Expand All @@ -90,14 +89,11 @@ void testJobWithGroup() throws SchedulerException, InterruptedException, Executi
SimpleScheduleBuilder.repeatSecondlyForTotalCount(1, 1))
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
responseFuture.get();
scheduler.deleteJob(job.getKey());
verifyJobDetails(job);
}

@Test
void testJobWithoutGroup() throws SchedulerException, InterruptedException, ExecutionException {
void testJobWithoutGroup() throws SchedulerException {
JobDetail job = JobBuilder.newJob(TestJob.class)
.withIdentity("dummyJobName").build();
Trigger trigger = TriggerBuilder
Expand All @@ -107,9 +103,6 @@ void testJobWithoutGroup() throws SchedulerException, InterruptedException, Exec
SimpleScheduleBuilder.repeatSecondlyForTotalCount(1, 1))
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
responseFuture.get();
scheduler.deleteJob(job.getKey());
verifyJobDetails(job);
}

Expand All @@ -124,7 +117,7 @@ void testJobManualCall() throws SchedulerException, InterruptedException {
}

@Test
public void testAgentPaused() throws SchedulerException, InterruptedException, ExecutionException {
public void testAgentPaused() throws SchedulerException {
TracerInternalApiUtils.pauseTracer(tracer);
int transactionCount = objectPoolFactory.getTransactionPool().getRequestedObjectCount();
int spanCount = objectPoolFactory.getSpanPool().getRequestedObjectCount();
Expand All @@ -138,7 +131,7 @@ public void testAgentPaused() throws SchedulerException, InterruptedException, E
}

@Test
void testSpringJob() throws SchedulerException, InterruptedException, ExecutionException {
void testSpringJob() throws SchedulerException {
JobDetail job = JobBuilder.newJob(TestSpringJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
Expand All @@ -148,14 +141,11 @@ void testSpringJob() throws SchedulerException, InterruptedException, ExecutionE
SimpleScheduleBuilder.repeatSecondlyForTotalCount(1, 1))
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
responseFuture.get();
scheduler.deleteJob(job.getKey());
verifyJobDetails(job);
}

@Test
void testJobWithResult() throws SchedulerException, InterruptedException, ExecutionException {
void testJobWithResult() throws SchedulerException {
JobDetail job = JobBuilder.newJob(TestJobWithResult.class)
.withIdentity("dummyJobName").build();
Trigger trigger = TriggerBuilder
Expand All @@ -165,17 +155,13 @@ void testJobWithResult() throws SchedulerException, InterruptedException, Execut
SimpleScheduleBuilder.repeatSecondlyForTotalCount(1, 1))
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
responseFuture.get();
scheduler.deleteJob(job.getKey());
verifyJobDetails(job);
assertThat(reporter.getTransactions().get(0).getResult()).isEqualTo("this is the result");
}

public static class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
responseFuture.complete(Boolean.TRUE);
}
}

Expand All @@ -198,23 +184,20 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
assertThat(transaction).isNull();
assertThat(tracer.getActive()).isNull();
}
responseFuture.complete(Boolean.TRUE);
}
}

public static class TestJobWithResult implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
context.setResult("this is the result");
responseFuture.complete(Boolean.TRUE);
}
}

public static class TestSpringJob extends QuartzJobBean {

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
responseFuture.complete(Boolean.TRUE);
}

}
Expand Down

0 comments on commit 110da2a

Please sign in to comment.