Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maallen committed Aug 24, 2023
1 parent c88f249 commit d2996d2
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.box.l10n.mojito.quartz.multi;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.box.l10n.mojito.quartz.QuartzConfig;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
classes = {
QuartzMultiSchedulerConfigProperties.class,
QuartzMultiSchedulerFactory.class,
QuartzMultiSchedulerManager.class,
QuartzConfig.class,
QuartzMultiSchedulerManagerTest.class
},
properties = {
"l10n.org.multi-quartz.enabled=true",
"l10n.org.multi-quartz.scheduler.defaultScheduler.quartz.threadPool.threadCount=10",
"l10n.org.multi-quartz.scheduler.scheduler2.quartz.threadPool.threadCount=5"
})
@EnableConfigurationProperties(QuartzMultiSchedulerConfigProperties.class)
public class QuartzMultiSchedulerManagerTest {

@Autowired QuartzMultiSchedulerManager quartzMultiSchedulerManager;

@Test
public void testSchedulerGeneratedFromProperties() throws SchedulerException {
Scheduler defaultScheduler = quartzMultiSchedulerManager.getScheduler("defaultScheduler");
Scheduler scheduler2 = quartzMultiSchedulerManager.getScheduler("scheduler2");

Assert.assertEquals("defaultScheduler", defaultScheduler.getSchedulerName());
Assert.assertEquals(10, defaultScheduler.getMetaData().getThreadPoolSize());
Assert.assertEquals("scheduler2", scheduler2.getSchedulerName());
Assert.assertEquals(5, scheduler2.getMetaData().getThreadPoolSize());
await()
.atMost(Duration.of(10, ChronoUnit.SECONDS))
.until(() -> defaultScheduler.isStarted() && scheduler2.isStarted());
}

@Test(expected = QuartzMultiSchedulerException.class)
public void testExceptionThrownIfNoDefaultSchedulerConfigured() throws SchedulerException {
List<Scheduler> schedulers = new ArrayList<>();
Scheduler schedulerMock = mock(Scheduler.class);
when(schedulerMock.getSchedulerName()).thenReturn("scheduler1");
schedulers.add(schedulerMock);

QuartzMultiSchedulerManager quartzMultiSchedulerManager = new QuartzMultiSchedulerManager();
quartzMultiSchedulerManager.schedulers = schedulers;
quartzMultiSchedulerManager.init();
}

@Test(expected = QuartzMultiSchedulerException.class)
public void testExceptionThrownIfDuplicateSchedulerConfigured() throws SchedulerException {
List<Scheduler> schedulers = new ArrayList<>();
Scheduler schedulerMock = mock(Scheduler.class);
Scheduler schedulerMock2 = mock(Scheduler.class);
when(schedulerMock.getSchedulerName()).thenReturn("scheduler1");
when(schedulerMock2.getSchedulerName()).thenReturn("scheduler1");
schedulers.add(schedulerMock);
schedulers.add(schedulerMock2);

QuartzMultiSchedulerManager quartzMultiSchedulerManager = new QuartzMultiSchedulerManager();
quartzMultiSchedulerManager.schedulers = schedulers;
quartzMultiSchedulerManager.init();
}

@Test
public void testDefaultSchedulerReturnedIfRequestedDoesntExist() throws SchedulerException {
Assert.assertEquals(
"defaultScheduler",
quartzMultiSchedulerManager.getScheduler("schedulerNotExist").getSchedulerName());
}

@Test
public void testScheduleJobOnSeparateSchedulers()
throws SchedulerException, InterruptedException {
Scheduler defaultScheduler = quartzMultiSchedulerManager.getScheduler("defaultScheduler");
Scheduler scheduler2 = quartzMultiSchedulerManager.getScheduler("scheduler2");

await()
.atMost(Duration.of(10, ChronoUnit.SECONDS))
.until(() -> defaultScheduler.isStarted() && scheduler2.isStarted());

JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity("job1", "DYNAMIC").build();
Trigger trigger =
TriggerBuilder.newTrigger().withIdentity("trigger1", "DYNAMIC").startNow().build();
defaultScheduler.scheduleJob(jobDetail, trigger);

Assert.assertTrue(defaultScheduler.checkExists(jobDetail.getKey()));

Assert.assertFalse(scheduler2.checkExists(jobDetail.getKey()));

await()
.atMost(Duration.of(10, ChronoUnit.SECONDS))
.until(() -> jobDetail.getJobDataMap() != null);
Assert.assertEquals("defaultScheduler", TestJob.getExecutingScheduler());

JobDetail jobDetail2 = JobBuilder.newJob(TestJob.class).withIdentity("job2", "DYNAMIC").build();
Trigger trigger2 =
TriggerBuilder.newTrigger().withIdentity("trigger2", "DYNAMIC").startNow().build();
scheduler2.scheduleJob(jobDetail2, trigger2);

Assert.assertTrue(scheduler2.checkExists(jobDetail2.getKey()));
Assert.assertFalse(defaultScheduler.checkExists(jobDetail2.getKey()));

await()
.atMost(Duration.of(10, ChronoUnit.SECONDS))
.until(() -> jobDetail2.getJobDataMap() != null);
Assert.assertEquals("scheduler2", TestJob.getExecutingScheduler());
}
}
28 changes: 28 additions & 0 deletions webapp/src/test/java/com/box/l10n/mojito/quartz/multi/TestJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.box.l10n.mojito.quartz.multi;

import org.quartz.Job;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestJob implements Job {

private static volatile String executingScheduler;

Logger logger = LoggerFactory.getLogger(TestJob.class);

@Override
public void execute(org.quartz.JobExecutionContext context)
throws org.quartz.JobExecutionException {
logger.info("Executing test job");
try {
executingScheduler = context.getScheduler().getSchedulerName();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}

public static String getExecutingScheduler() {
return executingScheduler;
}
}

0 comments on commit d2996d2

Please sign in to comment.