Skip to content

Commit

Permalink
Attempt to fix flaky DependentBeanJobTest
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Feb 20, 2024
1 parent 85c8a77 commit a6f36e1
Showing 1 changed file with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.quartz.test;

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

import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -45,10 +44,10 @@ public class DependentBeanJobTest {

@Test
public void testDependentBeanJobDestroyed() throws SchedulerException, InterruptedException {
assertEquals(0, MyJob.timesConstructed);
assertEquals(0, MyJob.timesDestroyed);
// prepare latch, schedule 10 one-off jobs, assert
CountDownLatch latch = service.initializeLatch(10);
// prepare latches, schedule 10 one-off jobs, assert
CountDownLatch execLatch = service.initExecuteLatch(10);
CountDownLatch constructLatch = service.initConstructLatch(10);
CountDownLatch destroyedLatch = service.initDestroyedLatch(10);
for (int i = 0; i < 10; i++) {
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger" + i, "myGroup")
Expand All @@ -59,12 +58,14 @@ public void testDependentBeanJobDestroyed() throws SchedulerException, Interrupt
.build();
quartz.scheduleJob(job, trigger);
}
assertTrue(latch.await(5, TimeUnit.SECONDS), "Latch count: " + latch.getCount());
assertEquals(10, MyJob.timesConstructed);
assertEquals(10, MyJob.timesDestroyed);
assertTrue(execLatch.await(2, TimeUnit.SECONDS), "Latch count: " + execLatch.getCount());
assertTrue(constructLatch.await(2, TimeUnit.SECONDS), "Latch count: " + constructLatch.getCount());
assertTrue(destroyedLatch.await(2, TimeUnit.SECONDS), "Latch count: " + destroyedLatch.getCount());

// now try the same with repeating job triggering three times
latch = service.initializeLatch(3);
execLatch = service.initExecuteLatch(3);
constructLatch = service.initConstructLatch(3);
destroyedLatch = service.initDestroyedLatch(3);
JobDetail job = JobBuilder.newJob(MyJob.class)
.withIdentity("myRepeatingJob", "myGroup")
.build();
Expand All @@ -78,44 +79,61 @@ public void testDependentBeanJobDestroyed() throws SchedulerException, Interrupt
.build();
quartz.scheduleJob(job, trigger);

assertTrue(latch.await(2, TimeUnit.SECONDS), "Latch count: " + latch.getCount());
assertEquals(13, MyJob.timesConstructed);
assertEquals(13, MyJob.timesDestroyed);
assertTrue(execLatch.await(2, TimeUnit.SECONDS), "Latch count: " + execLatch.getCount());
assertTrue(constructLatch.await(2, TimeUnit.SECONDS), "Latch count: " + constructLatch.getCount());
assertTrue(destroyedLatch.await(2, TimeUnit.SECONDS), "Latch count: " + destroyedLatch.getCount());
}

@ApplicationScoped
public static class Service {

volatile CountDownLatch latch;
volatile CountDownLatch executeLatch;
volatile CountDownLatch constructedLatch;
volatile CountDownLatch destroyedLatch;

public CountDownLatch initializeLatch(int latchCountdown) {
this.latch = new CountDownLatch(latchCountdown);
return latch;
public CountDownLatch initExecuteLatch(int latchCountdown) {
this.executeLatch = new CountDownLatch(latchCountdown);
return executeLatch;
}

public CountDownLatch initConstructLatch(int latchCountdown) {
this.constructedLatch = new CountDownLatch(latchCountdown);
return constructedLatch;
}

public CountDownLatch initDestroyedLatch(int latchCountdown) {
this.destroyedLatch = new CountDownLatch(latchCountdown);
return destroyedLatch;
}

public void execute() {
latch.countDown();
executeLatch.countDown();
}

public void constructedLatch() {
constructedLatch.countDown();
}

public void destroyedLatch() {
destroyedLatch.countDown();
}

}

@Dependent
static class MyJob implements Job {

public static volatile int timesConstructed = 0;
public static volatile int timesDestroyed = 0;

@Inject
Service service;

@PostConstruct
void postConstruct() {
timesConstructed++;
service.constructedLatch();
}

@PreDestroy
void preDestroy() {
timesDestroyed++;
service.destroyedLatch();
}

@Override
Expand Down

0 comments on commit a6f36e1

Please sign in to comment.