Search in sources :

Example 1 with QuartzJobSchedule

use of org.apache.gobblin.runtime.scheduler.QuartzJobSpecScheduler.QuartzJobSchedule in project incubator-gobblin by apache.

the class TestQuartzJobSpecScheduler method testSchedule.

@Test
public void testSchedule() throws Exception {
    final Logger log = LoggerFactory.getLogger(getClass().getName() + ".testSchedule");
    Config quartzCfg = ConfigFactory.parseMap(ImmutableMap.<String, Object>builder().put("org.quartz.scheduler.instanceName", "TestQuartzJobSpecScheduler.testSchedule").put("org.quartz.threadPool.threadCount", "10").put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore").build());
    QuartzJobSpecScheduler scheduler = new QuartzJobSpecScheduler(log, quartzCfg);
    scheduler.startAsync();
    scheduler.awaitRunning(10, TimeUnit.SECONDS);
    Assert.assertTrue(scheduler._scheduler.getScheduler().isStarted());
    final ArrayBlockingQueue<JobSpec> expectedCalls = new ArrayBlockingQueue<>(100);
    try {
        Config jobCfg1 = ConfigFactory.parseMap(ImmutableMap.<String, Object>builder().put(ConfigurationKeys.JOB_SCHEDULE_KEY, "0/5 * * * * ?").build());
        Config jobCfg2 = ConfigFactory.parseMap(ImmutableMap.<String, Object>builder().put(ConfigurationKeys.JOB_SCHEDULE_KEY, "3/5 * * * * ?").build());
        final JobSpec js1 = JobSpec.builder("test.job1").withConfig(jobCfg1).build();
        final JobSpec js2 = JobSpec.builder("test.job2").withConfig(jobCfg2).build();
        final JobSpec js1_2 = JobSpec.builder("test.job1").withConfig(jobCfg1).withVersion("2").build();
        JobSpecSchedule jss1 = scheduler.scheduleJob(js1, new Runnable() {

            @Override
            public void run() {
                expectedCalls.offer(js1);
            }
        });
        Assert.assertEquals(scheduler.getSchedules().size(), 1);
        Assert.assertEquals(jss1.getJobSpec(), js1);
        Assert.assertTrue(jss1 instanceof QuartzJobSchedule);
        QuartzJobSchedule qjss1 = (QuartzJobSchedule) jss1;
        Assert.assertNotNull(scheduler._scheduler.getScheduler().getJobDetail(qjss1.getQuartzTrigger().getJobKey()));
        Assert.assertNotNull(scheduler._scheduler.getScheduler().getTrigger(qjss1.getQuartzTrigger().getKey()));
        Assert.assertTrue(qjss1.getQuartzTrigger().mayFireAgain());
        // Wait for the next run
        JobSpec expJs1 = expectedCalls.poll(6000, TimeUnit.MILLISECONDS);
        Assert.assertEquals(expJs1, js1);
        // Wait for the next run
        expJs1 = expectedCalls.poll(6000, TimeUnit.MILLISECONDS);
        Assert.assertEquals(expJs1, js1);
        // Schedule another job
        JobSpecSchedule jss2 = scheduler.scheduleJob(js2, new Runnable() {

            @Override
            public void run() {
                expectedCalls.offer(js2);
            }
        });
        Assert.assertEquals(scheduler.getSchedules().size(), 2);
        Assert.assertEquals(jss2.getJobSpec(), js2);
        // Wait for the next run -- we should get js2
        JobSpec expJs2 = expectedCalls.poll(6000, TimeUnit.MILLISECONDS);
        Assert.assertEquals(expJs2, js2);
        // Wait for the next run -- we should get js1
        expJs1 = expectedCalls.poll(6000, TimeUnit.MILLISECONDS);
        Assert.assertEquals(expJs1, js1);
        // Wait for the next run -- we should get js2
        expJs2 = expectedCalls.poll(6000, TimeUnit.MILLISECONDS);
        log.info("Found call: " + expJs2);
        Assert.assertEquals(expJs2, js2);
        // Update the first job
        QuartzJobSchedule qjss1_2 = (QuartzJobSchedule) scheduler.scheduleJob(js1_2, new Runnable() {

            @Override
            public void run() {
                expectedCalls.offer(js1_2);
            }
        });
        Assert.assertEquals(scheduler.getSchedules().size(), 2);
        // Wait for 5 seconds -- we should see at least 2 runs of js1_2 and js2
        Thread.sleep(15000);
        int js1_2_cnt = 0;
        int js2_cnt = 0;
        for (JobSpec nextJs : expectedCalls) {
            log.info("Found call: " + nextJs);
            if (js1_2.equals(nextJs)) {
                ++js1_2_cnt;
            } else if (js2.equals(nextJs)) {
                ++js2_cnt;
            } else {
                Assert.fail("Unexpected job spec: " + nextJs);
            }
        }
        Assert.assertTrue(js1_2_cnt >= 2, "js1_2_cnt=" + js1_2_cnt);
        Assert.assertTrue(js2_cnt >= 2, "js2_cnt=" + js2_cnt);
        scheduler.unscheduleJob(js1_2.getUri());
        Assert.assertEquals(scheduler.getSchedules().size(), 1);
        Assert.assertFalse(scheduler._scheduler.getScheduler().checkExists(qjss1_2.getQuartzTrigger().getJobKey()));
        // Flush calls
        Thread.sleep(1000);
        expectedCalls.clear();
        // All subsequent calls should be for js2
        for (int i = 0; i < 2; ++i) {
            JobSpec nextJs = expectedCalls.poll(12000, TimeUnit.MILLISECONDS);
            Assert.assertEquals(nextJs, js2);
        }
    } finally {
        scheduler.stopAsync();
        scheduler.awaitTerminated(10, TimeUnit.SECONDS);
    }
    // we may have to drain at most one call due to race conditions
    if (null != expectedCalls.poll(2100, TimeUnit.MILLISECONDS)) {
        Assert.assertNull(expectedCalls.poll(3000, TimeUnit.MILLISECONDS));
    }
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Config(com.typesafe.config.Config) QuartzJobSchedule(org.apache.gobblin.runtime.scheduler.QuartzJobSpecScheduler.QuartzJobSchedule) JobSpecSchedule(org.apache.gobblin.runtime.api.JobSpecSchedule) JobSpec(org.apache.gobblin.runtime.api.JobSpec) Logger(org.slf4j.Logger) Test(org.testng.annotations.Test)

Aggregations

Config (com.typesafe.config.Config)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 JobSpec (org.apache.gobblin.runtime.api.JobSpec)1 JobSpecSchedule (org.apache.gobblin.runtime.api.JobSpecSchedule)1 QuartzJobSchedule (org.apache.gobblin.runtime.scheduler.QuartzJobSpecScheduler.QuartzJobSchedule)1 Logger (org.slf4j.Logger)1 Test (org.testng.annotations.Test)1