Search in sources :

Example 21 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestListener method scheduleJobs.

private void scheduleJobs(MotechSchedulerFactoryBean schedulerFactoryBean, SettingsFacade settings) throws InterruptedException, SchedulerException {
    Scheduler scheduler = schedulerFactoryBean.getQuartzScheduler();
    scheduler.clear();
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("foo", "bar");
        params.put("fii", "bor");
        params.put("fuu", "baz");
        String eventSubject = "test_event";
        MotechEvent motechEvent = new MotechEvent(eventSubject, params);
        int maxJobs = 1;
        int repeatCount = 1;
        TestListener listener = new TestListener("test_listener", maxJobs, repeatCount);
        eventListenerRegistryService.registerListener(listener, eventSubject);
        DateTime startTime = now().plusSeconds(0);
        Integer repeatInterval = 1;
        System.out.println("startTime: " + startTime);
        NanoStopWatch timeToSchedule = new NanoStopWatch().start();
        for (int i = 0; i < maxJobs; i++) {
            params.put(MotechSchedulerService.JOB_ID_KEY, "test_job_" + String.valueOf(i));
            RepeatingSchedulableJob repeatingJob = new RepeatingSchedulableJob(motechEvent, repeatCount, repeatInterval, startTime, null, false);
            repeatingJob.setUseOriginalFireTimeAfterMisfire(false);
            schedulerService.scheduleRepeatingJob(repeatingJob);
        }
        System.out.println(format("Time to schedule : %dms", timeToSchedule.duration()));
        scheduler.start();
        synchronized (listener) {
            while (scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains("default")).size() > 0) {
                listener.wait(5000);
            }
        }
        System.out.println(listener.getReport());
        assertEquals(maxJobs * (repeatCount + 1), listener.getCount());
    } finally {
        eventListenerRegistryService.clearListenersForBean("test_listener");
        schedulerFactoryBean.getQuartzScheduler().standby();
    }
}
Also used : RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) NanoStopWatch(org.motechproject.commons.api.NanoStopWatch) HashMap(java.util.HashMap) Scheduler(org.quartz.Scheduler) MotechEvent(org.motechproject.event.MotechEvent) DateTime(org.joda.time.DateTime)

Example 22 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class JobIdTest method initializeUsingMotechEvent.

@Test
public void initializeUsingMotechEvent() {
    MotechEvent motechEvent = new MotechEvent(SUBJECT_VALUE);
    motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, JOB_ID_VALUE);
    JobId jobId = new CronJobId(motechEvent);
    assertEquals(String.format("%s-%s", SUBJECT_VALUE, JOB_ID_VALUE), jobId.value());
    jobId = new RepeatingJobId(motechEvent);
    assertEquals(String.format("%s-%s%s", SUBJECT_VALUE, JOB_ID_VALUE, RepeatingJobId.SUFFIX_REPEATJOBID), jobId.value());
}
Also used : CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechEvent(org.motechproject.event.MotechEvent) JobId(org.motechproject.scheduler.contract.JobId) CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) Test(org.junit.Test)

Example 23 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestMotechScheduledEvent method equalsTest.

@Test
public void equalsTest() throws Exception {
    MotechEvent motechEvent = new MotechEvent("testEvent", null);
    MotechEvent motechEventSame = new MotechEvent("testEvent", null);
    MotechEvent motechEventDifferentJobId = new MotechEvent("testEvent", null);
    MotechEvent scheduledEventDifferentEventType = new MotechEvent("testEvent2", null);
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("One", 1);
    MotechEvent nonNullParams = new MotechEvent("testEvent", hashMap);
    MotechEvent nonNullParams2 = new MotechEvent("testEvent", hashMap);
    assertTrue(motechEvent.equals(motechEvent));
    assertTrue(motechEvent.equals(motechEventSame));
    assertTrue(nonNullParams.equals(nonNullParams2));
    assertFalse(motechEvent.equals(null));
    assertFalse(motechEvent.equals(uuidStr));
    assertFalse(motechEvent.equals(scheduledEventDifferentEventType));
    assertFalse(motechEvent.equals(nonNullParams));
    assertFalse(nonNullParams.equals(motechEvent));
}
Also used : HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 24 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestMotechScheduledEvent method testGetParameters.

@Test
public void testGetParameters() {
    MotechEvent motechEvent = new MotechEvent("testEvent", null);
    Map<String, Object> params = motechEvent.getParameters();
    assertNotNull("Expecting param object", params);
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("One", 1);
    MotechEvent nonNullParams = new MotechEvent("testEvent", hashMap);
    params = nonNullParams.getParameters();
    assertTrue(params.equals(hashMap));
// assertFalse(params == hashMap);  // not wrapped collection anymore.
}
Also used : HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 25 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestEventListener method shouldGetPreviousFireTime.

@Test
public void shouldGetPreviousFireTime() throws InterruptedException {
    try {
        fakeNow(new DateTime());
        Map<String, Object> params = new HashMap<>();
        MotechEvent event = new MotechEvent("test_event", params);
        final String jobId = id("jobId");
        params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
        DateTime now = new DateTime();
        StringBuilder cron = new StringBuilder();
        cron.append(now.getSecondOfMinute()).append(" ").append(now.getMinuteOfHour()).append(" ");
        cron.append(now.getHourOfDay()).append(" * * ?");
        schedulerService.scheduleJob(new CronSchedulableJob(event, cron.toString()));
        Thread.sleep(1000);
        DateTime dateTime = schedulerService.getPreviousFireDate(new CronJobId(event));
        assertEquals(dateTime.getHourOfDay(), now.getHourOfDay());
        assertEquals(dateTime.getMinuteOfHour(), now.getMinuteOfHour());
        assertEquals(dateTime.getSecondOfMinute(), now.getSecondOfMinute());
    } finally {
        stopFakingTime();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) CronJobId(org.motechproject.scheduler.contract.CronJobId) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

MotechEvent (org.motechproject.event.MotechEvent)138 Test (org.junit.Test)87 HashMap (java.util.HashMap)79 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)28 DateTime (org.joda.time.DateTime)25 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)20 ArrayList (java.util.ArrayList)14 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)13 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)11 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 Matchers.anyString (org.mockito.Matchers.anyString)7 EventListener (org.motechproject.event.listener.EventListener)7 CronJobId (org.motechproject.scheduler.contract.CronJobId)7 Task (org.motechproject.tasks.domain.mds.task.Task)7 JobId (org.motechproject.scheduler.contract.JobId)6 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)6 Time (org.motechproject.commons.date.model.Time)5 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)5 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)5