Search in sources :

Example 76 with MotechEvent

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

the class TestEventListener method shouldNotScheduleRunOnceJobInThePast.

@Test(expected = MotechSchedulerException.class)
public void shouldNotScheduleRunOnceJobInThePast() throws SchedulerException {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        schedulerService.scheduleRunOnceJob(new RunOnceSchedulableJob(new MotechEvent("test_event", params), newDateTime(2020, 6, 15, 12, 0, 0)));
    } finally {
        stopFakingTime();
    }
}
Also used : HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Test(org.junit.Test)

Example 77 with MotechEvent

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

the class TestEventListener method shouldNotIgnoreFiresInPastWhenSchedulingCronJob.

@Test
public // See http://quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigJobStoreTX
void shouldNotIgnoreFiresInPastWhenSchedulingCronJob() throws InterruptedException, SchedulerException {
    final String eventSubject = id("eve");
    try {
        TestEventListener listener = new TestEventListener();
        eventRegistry.registerListener(listener, eventSubject);
        DateTime now = findSuitableTimeToScheduleWithSafeBufferFromTriggerTime();
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        DateTime jobStartTimeInPast = now.minusMinutes(3);
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent(eventSubject, params), "0 0/1 * 1/1 * ? *", jobStartTimeInPast, null, false));
        synchronized (listener.getReceivedEvents()) {
            listener.getReceivedEvents().wait(5000);
        }
        assertTrue("Listener didn't receive misfired events.", listener.getReceivedEvents().size() > 0);
    } finally {
        eventRegistry.clearListenersForBean(eventSubject);
        schedulerService.unscheduleAllJobs(eventSubject + "-job_id");
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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)

Example 78 with MotechEvent

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

the class TestEventListener method shouldRescheduleCronJob.

@Test
public void shouldRescheduleCronJob() throws SchedulerException {
    try {
        fakeNow(new DateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        final String jobId = id("jobId");
        params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 10 * * ?"));
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 14 * * ?"));
        List<DateTime> first3FireTimes = getFireTimes("test_event-" + jobId).subList(0, 3);
        assertEquals(asList(newDateTime(2020, 7, 15, 14, 0, 0), newDateTime(2020, 7, 16, 14, 0, 0), newDateTime(2020, 7, 17, 14, 0, 0)), first3FireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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)

Example 79 with MotechEvent

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

the class TestEventListener method shouldScheduleInterveningDayOfWeekJob.

@Test
public void shouldScheduleInterveningDayOfWeekJob() throws SchedulerException {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        schedulerService.scheduleDayOfWeekJob(new DayOfWeekSchedulableJob(new MotechEvent("test_event", params), // friday
        newDateTime(2020, 7, 10), newDateTime(2020, 7, 22), asList(DayOfWeek.Monday, DayOfWeek.Thursday), new Time(10, 10), true));
        List<DateTime> fireTimes = getFireTimes("test_event-job_id");
        assertEquals(asList(newDateTime(2020, 7, 16, 10, 10, 0), newDateTime(2020, 7, 20, 10, 10, 0)), fireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : HashMap(java.util.HashMap) DayOfWeekSchedulableJob(org.motechproject.scheduler.contract.DayOfWeekSchedulableJob) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) Time(org.motechproject.commons.date.model.Time) DateTime(org.joda.time.DateTime) TimeFaker.stopFakingTime(org.motechproject.testing.utils.TimeFaker.stopFakingTime) MotechEvent(org.motechproject.event.MotechEvent) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 80 with MotechEvent

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

the class TestEventListener method shouldRescheduleCronJobWithNewSchedule.

@Test
public void shouldRescheduleCronJobWithNewSchedule() throws SchedulerException {
    try {
        fakeNow(new DateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 10 * * ?"));
        schedulerService.rescheduleJob("test_event", "job_id", "0 0 14 * * ?");
        List<DateTime> first3FireTimes = getFireTimes("test_event-job_id").subList(0, 3);
        assertEquals(asList(newDateTime(2020, 7, 15, 14, 0, 0), newDateTime(2020, 7, 16, 14, 0, 0), newDateTime(2020, 7, 17, 14, 0, 0)), first3FireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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