Skip to content

Commit

Permalink
Create MultiSchedulerIntegrationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jlinn committed Dec 11, 2016
1 parent df510b3 commit e076935
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 21 deletions.
24 changes: 18 additions & 6 deletions src/test/java/net/joelinn/quartz/BaseIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Before;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.simpl.PropertySettingJobFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
Expand All @@ -30,27 +31,29 @@ public abstract class BaseIntegrationTest {
protected Scheduler scheduler;
protected Pool<Jedis> jedisPool;

protected int port;
protected static final String HOST = "localhost";


@Before
public void setUp() throws Exception {
final int port = getPort();
final String host = "localhost";
port = getPort();
redisServer = RedisServer.builder()
.port(port)
.build();
redisServer.start();

jedisPool = new JedisPool(host, port);
jedisPool = new JedisPool(HOST, port);


scheduler = new StdSchedulerFactory(schedulerConfig(host, port)).getScheduler();
scheduler = new StdSchedulerFactory(schedulerConfig(HOST, port)).getScheduler();
scheduler.start();
}


protected Properties schedulerConfig(String host, int port) {
Properties config = new Properties();
config.setProperty("org.quartz.jobStore.class", RedisJobStore.class.getName());
config.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, RedisJobStore.class.getName());
config.setProperty("org.quartz.jobStore.host", host);
config.setProperty("org.quartz.jobStore.port", String.valueOf(port));
config.setProperty("org.quartz.threadPool.threadCount", "1");
Expand All @@ -70,7 +73,7 @@ public void tearDown() throws Exception {


public static class DataJob implements Job {
private Pool<Jedis> jedisPool;
protected Pool<Jedis> jedisPool;

public void setJedisPool(Pool<Jedis> jedisPool) {
this.jedisPool = jedisPool;
Expand Down Expand Up @@ -192,4 +195,13 @@ public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigge

}
}


protected class RedisJobFactory extends PropertySettingJobFactory {
@Override
protected void setBeanProps(Object obj, JobDataMap data) throws SchedulerException {
data.put("jedisPool", jedisPool);
super.setBeanProps(obj, data);
}
}
}
121 changes: 121 additions & 0 deletions src/test/java/net/joelinn/quartz/MultiSchedulerIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package net.joelinn.quartz;

import net.jodah.concurrentunit.Waiter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.NameMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.Properties;

import static junit.framework.TestCase.fail;
import static net.joelinn.quartz.TestUtils.createCronTrigger;
import static net.joelinn.quartz.TestUtils.createJob;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

/**
* @author Joe Linn
* 12/10/2016
*/
public class MultiSchedulerIntegrationTest extends BaseIntegrationTest {
private static final Logger log = LoggerFactory.getLogger(MultiSchedulerIntegrationTest.class);

private static final String KEY_ID = "id";

private Scheduler scheduler2;


@Before
@Override
public void setUp() throws Exception {
super.setUp();
Properties props = schedulerConfig(HOST, port);
props.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "second");
scheduler2 = new StdSchedulerFactory(props).getScheduler();
}


@After
@Override
public void tearDown() throws Exception {
scheduler2.shutdown();
super.tearDown();
}


@Override
protected Properties schedulerConfig(String host, int port) {
Properties config = super.schedulerConfig(host, port);
config.setProperty("org.quartz.threadPool.threadCount", "2");
config.setProperty("org.quartz.scheduler.instanceId", "AUTO");
config.setProperty("org.quartz.scheduler.instanceIdGenerator.class", "org.quartz.simpl.SimpleInstanceIdGenerator");
return config;
}

@Test
public void testMultipleSchedulers() throws Exception {
scheduler.setJobFactory(new RedisJobFactory());
scheduler2.setJobFactory(new RedisJobFactory());

assertThat(scheduler.getSchedulerInstanceId(), notNullValue());
assertThat(scheduler2.getSchedulerInstanceId(), notNullValue());
assertThat(scheduler.getSchedulerInstanceId(), not(equalTo(scheduler2.getSchedulerInstanceId())));

JobDetail job = createJob(SchedulerIDCheckingJob.class, "testJob", "group");
final String triggerName = "trigger";
CronTrigger trigger = createCronTrigger(triggerName, "group", "* * * * * ?");

Waiter waiter = new Waiter();
scheduler.getListenerManager().addTriggerListener(new CompleteListener(waiter), NameMatcher.triggerNameEquals(triggerName));
scheduler.scheduleJob(job, trigger);

waiter.await(1500);

try (Jedis jedis = jedisPool.getResource()) {
assertThat(jedis.get(KEY_ID), equalTo(scheduler.getSchedulerInstanceId()));
}

scheduler.shutdown();
scheduler2.getListenerManager().addTriggerListener(new CompleteListener(waiter), NameMatcher.triggerNameEquals(triggerName));
scheduler2.start();

waiter.await(1500);

try (Jedis jedis = jedisPool.getResource()) {
assertThat(jedis.get(KEY_ID), equalTo(scheduler2.getSchedulerInstanceId()));
}
}


@DisallowConcurrentExecution
public static class SchedulerIDCheckingJob extends DataJob {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
final String schedulerID = context.getScheduler().getSchedulerInstanceId();
try (Jedis jedis = jedisPool.getResource()) {
if (jedis.setnx(KEY_ID, schedulerID) == 0) {
// we already have an ID stored
final String storedID = jedis.get(KEY_ID);
if (storedID.equals(schedulerID)) {
fail("The same schedule executed the job twice.");
} else {
jedis.set(KEY_ID, schedulerID);
}
}
}
} catch (SchedulerException e) {
log.error("Unable to obtain scheduler instance ID.", e);
fail("Failed to obtain scheduler instance ID.");
}
}
}
}
15 changes: 0 additions & 15 deletions src/test/java/net/joelinn/quartz/MultiThreadedIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import net.jodah.concurrentunit.Waiter;
import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.NameMatcher;
import org.quartz.simpl.PropertySettingJobFactory;
import redis.clients.jedis.Jedis;

import java.util.Properties;
Expand Down Expand Up @@ -90,16 +87,4 @@ public void testDisallowConcurrent() throws Exception {

assertThat(SingletonSleepJob.concurrentExecutions.get(), equalTo(0));
}


private class RedisJobFactory extends PropertySettingJobFactory {
@Override
protected void setBeanProps(Object obj, JobDataMap data) throws SchedulerException {
data.put("jedisPool", jedisPool);
super.setBeanProps(obj, data);
}
}



}

0 comments on commit e076935

Please sign in to comment.