Search in sources :

Example 1 with Job

use of org.drools.core.time.Job in project drools by kiegroup.

the class PseudoClockSchedulerTest method timerIsResetWhenJobThrowsExceptions.

@Test
public void timerIsResetWhenJobThrowsExceptions() {
    final Date triggerTime = new Date(1000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime, triggerTime, triggerTime, null);
    when(mockTrigger_1.nextFireTime()).thenReturn(triggerTime);
    Job job = new Job() {

        public void execute(JobContext ctx) {
            assertThat(scheduler.getCurrentTime(), is(1000L));
            throw new RuntimeException("for test");
        }
    };
    scheduler.scheduleJob(job, this.mockContext_1, mockTrigger_1);
    scheduler.advanceTime(5000, TimeUnit.MILLISECONDS);
    // The time must be advanced correctly even when the job throws an exception
    assertThat(scheduler.getCurrentTime(), is(5000L));
    verify(mockTrigger_1, atLeast(2)).hasNextFireTime();
    verify(mockTrigger_1, times(1)).nextFireTime();
}
Also used : JobContext(org.drools.core.time.JobContext) Job(org.drools.core.time.Job) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Job

use of org.drools.core.time.Job in project drools by kiegroup.

the class PseudoClockSchedulerTest method timerIsSetToJobTriggerTimeForExecution.

@Test
public void timerIsSetToJobTriggerTimeForExecution() {
    final Date triggerTime = new Date(1000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime, triggerTime, triggerTime, null);
    when(mockTrigger_1.nextFireTime()).thenReturn(triggerTime);
    Job job = new Job() {

        public void execute(JobContext ctx) {
            // Even though the clock has been advanced to 5000, the job should run
            // with the time set its trigger time.
            assertThat(scheduler.getCurrentTime(), is(1000L));
        }
    };
    scheduler.scheduleJob(job, this.mockContext_1, mockTrigger_1);
    scheduler.advanceTime(5000, TimeUnit.MILLISECONDS);
    // Now, after the job has been executed the time should be what it was advanced to
    assertThat(scheduler.getCurrentTime(), is(5000L));
    verify(mockTrigger_1, atLeast(2)).hasNextFireTime();
    verify(mockTrigger_1, times(1)).nextFireTime();
}
Also used : JobContext(org.drools.core.time.JobContext) Job(org.drools.core.time.Job) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)2 Job (org.drools.core.time.Job)2 JobContext (org.drools.core.time.JobContext)2 Test (org.junit.Test)2