Search in sources :

Example 11 with Clock

use of org.activiti.engine.runtime.Clock in project Activiti by Activiti.

the class DurationHelperTest method daylightSaving25HourDayEurope.

@Test
public void daylightSaving25HourDayEurope() throws Exception {
    Clock testingClock = new DefaultClockImpl();
    testingClock.setCurrentCalendar(parseCalendar("20131027-00:00:00", TimeZone.getTimeZone("Europe/Amsterdam")));
    DurationHelper dh = new DurationHelper("R2/2013-10-27T00:00:00/P1D", testingClock);
    assertEquals(parseCalendar("20131028-00:00:00", TimeZone.getTimeZone("Europe/Amsterdam")), dh.getCalendarAfter(testingClock.getCurrentCalendar()));
}
Also used : DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) DurationHelper(org.activiti.engine.impl.calendar.DurationHelper) Clock(org.activiti.engine.runtime.Clock) Test(org.junit.Test)

Example 12 with Clock

use of org.activiti.engine.runtime.Clock in project Activiti by Activiti.

the class DurationHelperTest method daylightSaving23HourDayEurope.

@Test
public void daylightSaving23HourDayEurope() throws Exception {
    Clock testingClock = new DefaultClockImpl();
    testingClock.setCurrentCalendar(parseCalendar("20140330-00:00:00", TimeZone.getTimeZone("Europe/Amsterdam")));
    DurationHelper dh = new DurationHelper("R2/2014-03-30T00:00:00/P1D", testingClock);
    assertEquals(parseCalendar("20140331-00:00:00", TimeZone.getTimeZone("Europe/Amsterdam")), dh.getCalendarAfter(testingClock.getCurrentCalendar()));
}
Also used : DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) DurationHelper(org.activiti.engine.impl.calendar.DurationHelper) Clock(org.activiti.engine.runtime.Clock) Test(org.junit.Test)

Example 13 with Clock

use of org.activiti.engine.runtime.Clock in project Activiti by Activiti.

the class JobEventsTest method testJobCanceledEventOnBoundaryEvent.

@Deployment
public void testJobCanceledEventOnBoundaryEvent() throws Exception {
    Clock testClock = new DefaultClockImpl();
    processEngineConfiguration.setClock(testClock);
    testClock.setCurrentTime(new Date());
    runtimeService.startProcessInstanceByKey("testTimerCancelledEvent");
    listener.clearEventsReceived();
    Task task = taskService.createTaskQuery().singleResult();
    taskService.complete(task.getId());
    checkEventCount(1, ActivitiEventType.JOB_CANCELED);
}
Also used : Task(org.activiti.engine.task.Task) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) Clock(org.activiti.engine.runtime.Clock) Date(java.util.Date) Deployment(org.activiti.engine.test.Deployment)

Example 14 with Clock

use of org.activiti.engine.runtime.Clock in project Activiti by Activiti.

the class StartTimerEventRepeatWithoutEndDateTest method testCycleDateStartTimerEvent.

/**
   * Timer repetition
   */
public void testCycleDateStartTimerEvent() throws Exception {
    Clock previousClock = processEngineConfiguration.getClock();
    Clock testClock = new DefaultClockImpl();
    processEngineConfiguration.setClock(testClock);
    Calendar calendar = Calendar.getInstance();
    calendar.set(2025, Calendar.DECEMBER, 10, 0, 0, 0);
    testClock.setCurrentTime(calendar.getTime());
    //deploy the process
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/event/timer/StartTimerEventRepeatWithoutEndDateTest.testCycleDateStartTimerEvent.bpmn20.xml").deploy();
    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    //AFTER DEPLOYMENT
    //when the process is deployed there will be created a timerStartEvent job which will wait to be executed.
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //dueDate should be after 24 hours from the process deployment
    Calendar dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 11, 0, 0, 0);
    //check the due date is inside the 2 seconds range
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    //No process instances
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //No tasks
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    // ADVANCE THE CLOCK
    // advance the clock after 9 days from starting the process ->
    // the system will execute the pending job and will create a new one (day by day)
    moveByMinutes(9 * 60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(10000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    // After time advanced 9 days  there should be 9 process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(9, processInstances.size());
    // 9 task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(9, tasks.size());
    // one new job will be created (and the old one will be deleted after execution)
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //check if the last job to be executed has the dueDate set correctly
    // (10'th repeat after 10 dec. => dueDate must have DueDate = 20 dec.)
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 20, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO that all 10 repeats to be executed
    // (last execution)
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the maximum number of repeats is reached it will not be executed other jobs");
    }
    // After the 10nth startEvent Execution should have 10 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(10, processInstances.size());
    // the current job will be deleted after execution and a new one will not be created. (all 10 has already executed)
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 10 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(10, tasks.size());
    //FINAL CHECK
    // count "timer fired" events
    int timerFiredCount = 0;
    List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.TIMER_FIRED.equals(eventReceived.getType())) {
            timerFiredCount++;
        }
    }
    //count "entity created" events
    int eventCreatedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_CREATED.equals(eventReceived.getType())) {
            eventCreatedCount++;
        }
    }
    // count "entity deleted" events
    int eventDeletedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_DELETED.equals(eventReceived.getType())) {
            eventDeletedCount++;
        }
    }
    //10 timers fired
    assertEquals(10, timerFiredCount);
    //10 jobs created
    assertEquals(10, eventCreatedCount);
    //10 jobs deleted
    assertEquals(10, eventDeletedCount);
    // let's complete the userTasks where the process is hanging in order to complete the processes.
    for (ProcessInstance processInstance : processInstances) {
        tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();
        Task task = tasks.get(0);
        assertEquals("Task A", task.getName());
        assertEquals(1, tasks.size());
        taskService.complete(task.getId());
    }
    //now All the process instances should be completed
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //no jobs
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    //no tasks
    tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    listener.clearEventsReceived();
    processEngineConfiguration.setClock(previousClock);
    repositoryService.deleteDeployment(repositoryService.createDeploymentQuery().singleResult().getId(), true);
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) Clock(org.activiti.engine.runtime.Clock) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job)

Example 15 with Clock

use of org.activiti.engine.runtime.Clock in project Activiti by Activiti.

the class SimpleSimulationRunTest method recordEvents.

private void recordEvents() {
    Clock clock = new DefaultClockImpl();
    clock.setCurrentTime(new Date(0));
    ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
    config.setClock(clock);
    ProcessEngine processEngine = (new RecordableProcessEngineFactory(config, listener)).getObject();
    processEngine.getRepositoryService().createDeployment().addClasspathResource(USERTASK_PROCESS).deploy();
    EventRecorderTestUtils.increaseTime(clock);
    TaskService taskService = processEngine.getTaskService();
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put(TEST_VARIABLE, TEST_VALUE);
    processEngine.getRuntimeService().startProcessInstanceByKey("oneTaskProcess", "oneTaskProcessBusinessKey", variables);
    EventRecorderTestUtils.increaseTime(clock);
    Task task = taskService.createTaskQuery().taskDefinitionKey("userTask").singleResult();
    taskService.complete(task.getId());
    checkStatus(processEngine.getHistoryService());
    EventRecorderTestUtils.closeProcessEngine(processEngine, listener);
    ProcessEngines.destroy();
}
Also used : Task(org.activiti.engine.task.Task) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) ThreadLocalClock(org.activiti.crystalball.simulator.impl.clock.ThreadLocalClock) Clock(org.activiti.engine.runtime.Clock) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)

Aggregations

Clock (org.activiti.engine.runtime.Clock)30 DefaultClockImpl (org.activiti.engine.impl.util.DefaultClockImpl)26 Test (org.junit.Test)15 DurationHelper (org.activiti.engine.impl.calendar.DurationHelper)14 Calendar (java.util.Calendar)9 Task (org.activiti.engine.task.Task)7 Date (java.util.Date)6 GregorianCalendar (java.util.GregorianCalendar)6 ThreadLocalClock (org.activiti.crystalball.simulator.impl.clock.ThreadLocalClock)6 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)6 ActivitiEvent (org.activiti.engine.delegate.event.ActivitiEvent)5 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)5 Job (org.activiti.engine.runtime.Job)5 SimpleDateFormat (java.text.SimpleDateFormat)3 DefaultClockFactory (org.activiti.crystalball.simulator.impl.clock.DefaultClockFactory)3 Deployment (org.activiti.engine.test.Deployment)3 HashMap (java.util.HashMap)2 ProcessEngineImpl (org.activiti.engine.impl.ProcessEngineImpl)2 CycleBusinessCalendar (org.activiti.engine.impl.calendar.CycleBusinessCalendar)2 NoExecutionVariableScope (org.activiti.engine.impl.el.NoExecutionVariableScope)2