Search in sources :

Example 96 with NodeLeftCountDownProcessEventListener

use of org.jbpm.test.listener.NodeLeftCountDownProcessEventListener in project jbpm by kiegroup.

the class StartEventTest method testTimerStartCron.

@Test(timeout = 15000)
public void testTimerStartCron() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 5);
    KieBase kbase = createKnowledgeBase("BPMN2-TimerStartCron.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    final List<Long> list = new ArrayList<Long>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        public void afterProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    // Timer in the process takes 1s, so after 5 seconds, there should be 5 process IDs in the list.
    countDownListener.waitTillCompleted();
    assertThat(getNumberOfProcessInstances("Minimal")).isEqualTo(5);
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) Test(org.junit.Test)

Example 97 with NodeLeftCountDownProcessEventListener

use of org.jbpm.test.listener.NodeLeftCountDownProcessEventListener in project jbpm by kiegroup.

the class StartEventTest method testTimerStartDateISO.

@Test(timeout = 10000)
public void testTimerStartDateISO() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 1);
    byte[] content = IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/BPMN2-TimerStartDate.bpmn2"));
    String processContent = new String(content, "UTF-8");
    OffsetDateTime plusTwoSeconds = OffsetDateTime.now().plusSeconds(2);
    processContent = processContent.replaceFirst("#\\{date\\}", plusTwoSeconds.toString());
    Resource resource = ResourceFactory.newReaderResource(new StringReader(processContent));
    resource.setSourcePath("/BPMN2-TimerStartDate.bpmn2");
    resource.setTargetPath("/BPMN2-TimerStartDate.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    final List<Long> list = new ArrayList<Long>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        public void beforeProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    assertThat(list.size()).isEqualTo(0);
    countDownListener.waitTillCompleted();
    assertThat(list.size()).isEqualTo(1);
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) OffsetDateTime(java.time.OffsetDateTime) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) Test(org.junit.Test)

Example 98 with NodeLeftCountDownProcessEventListener

use of org.jbpm.test.listener.NodeLeftCountDownProcessEventListener in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromDisc.

@Test(timeout = 20000)
public void testStartTimerCycleFromDisc() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
    KieBase kbase = createKnowledgeBaseFromDisc("BPMN2-StartTimerCycle.bpmn2");
    try {
        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
        ksession.addEventListener(countDownListener);
        assertEquals(0, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        long sessionId = ksession.getIdentifier();
        Environment env = ksession.getEnvironment();
        final List<Long> list = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(2, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        logger.info("dispose");
        ksession.dispose();
        countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
        ksession.addEventListener(countDownListener);
        AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
        final List<Long> list2 = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list2.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(4, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        abortProcessInstances(ksession);
        ksession.dispose();
    } finally {
        ksession = createKnowledgeSession(kbase);
        abortProcessInstances(ksession);
        ksession.dispose();
    }
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) Environment(org.kie.api.runtime.Environment) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Test(org.junit.Test)

Example 99 with NodeLeftCountDownProcessEventListener

use of org.jbpm.test.listener.NodeLeftCountDownProcessEventListener in project jbpm by kiegroup.

the class UnmarshallingOverdueTimersTest method startDisposeAndReloadTimerProcess.

@Test(timeout = 10000)
public void startDisposeAndReloadTimerProcess() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 1);
    if (debug) {
        String shellVar = "TEST";
        String shellVarVal = System.getenv(shellVar);
        if (shellVarVal != null) {
            debug = false;
        }
    }
    String sessionPropName = "KSESSION_ID";
    String sessionPropVal = System.getenv(sessionPropName);
    String processPropName = "PROCESS_ID";
    String processPropVal = System.getenv(sessionPropName);
    if (sessionPropVal == null || debug) {
        KieBase kbase = loadKnowledgeBase(bpmn2FileName);
        KieSession ksession = createStatefulKnowledgeSession(kbase);
        ksession.addEventListener(countDownListener);
        // setup parameters
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("time", seconds + timeUnit);
        // note process start time
        Calendar cal = GregorianCalendar.getInstance();
        // start process
        ProcessInstance processInstance = ksession.startProcess("interruptedTimer", params);
        long processId = processInstance.getId();
        // print info for next test
        if (debug) {
            processPropVal = Long.toString(processId);
        } else {
            logger.info("export {}={}", processPropName, processId);
        }
        // dispose of session
        KieSessionConfiguration config = ksession.getSessionConfiguration();
        long ksessionId = knowledgeSessionDispose(ksession);
        // print info for next test
        if (debug) {
            sessionPropVal = Long.toString(ksessionId);
        } else {
            logger.info("export {}={}", sessionPropName, ksessionId);
        }
        if (!debug) {
            cal.add(Calendar.SECOND, (int) seconds);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
            logger.info("Please wait at least {} [{}]", (seconds + timeUnit), sdf.format(cal.getTime()));
        }
    }
    if (sessionPropVal != null || debug) {
        // reload session
        int ksessionId = Integer.parseInt(sessionPropVal);
        KieSession ksession = reloadStatefulKnowledgeSession(bpmn2FileName, ksessionId);
        ksession.addEventListener(countDownListener);
        long processInstanceId = Integer.parseInt(processPropVal);
        logger.debug("! waiting 5 seconds for timer to fire");
        countDownListener.waitTillCompleted();
        ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
        if (processInstance != null) {
            assertTrue("Process has not terminated.", processInstance.getState() == ProcessInstance.STATE_COMPLETED);
        }
    }
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) SimpleDateFormat(java.text.SimpleDateFormat) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) MultipleProcessesPerThreadTest(org.jbpm.bpmn2.concurrency.MultipleProcessesPerThreadTest) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 100 with NodeLeftCountDownProcessEventListener

use of org.jbpm.test.listener.NodeLeftCountDownProcessEventListener in project jbpm by kiegroup.

the class AsyncContinuationSupportTest method testAsyncScriptTask.

@Test(timeout = 10000)
public void testAsyncScriptTask() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Hello", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-AsyncScriptTask.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new SystemOutWorkItemHandler());
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance processInstance = ksession.startProcess("AsyncScriptTask");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstanceId);
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(8, logs.size());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Aggregations

NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)132 Test (org.junit.Test)127 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)101 KieSession (org.kie.api.runtime.KieSession)66 KieBase (org.kie.api.KieBase)61 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)58 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)46 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)42 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)42 HashMap (java.util.HashMap)40 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)39 ArrayList (java.util.ArrayList)35 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)30 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)28 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)26 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)23 DoNothingWorkItemHandler (org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler)20 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)19 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)19 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)18