Skip to content

Commit

Permalink
Add two additional tests for CleanupExecutionErrorCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
MarianMacik authored and mswiderski committed Apr 25, 2017
1 parent b3fd66e commit 75019bf
Showing 1 changed file with 194 additions and 60 deletions.
Expand Up @@ -63,21 +63,22 @@
public class CleanupExecutionErrorCommandWithProcessTest extends AbstractExecutorBaseTest {

private PoolingDataSource pds;
private UserGroupCallback userGroupCallback;
private UserGroupCallback userGroupCallback;
private RuntimeManager manager;
private ExecutorService executorService;
private EntityManagerFactory emf = null;

@Before
public void setup() {
ExecutorTestUtil.cleanupSingletonSessionId();
pds = ExecutorTestUtil.setupPoolingDataSource();
Properties properties= new Properties();
Properties properties = new Properties();
properties.setProperty("mary", "HR");
properties.setProperty("john", "HR");
userGroupCallback = new JBossUserGroupCallbackImpl(properties);
executorService = buildExecutorService();
}

@After
public void teardown() {
executorService.destroy();
Expand All @@ -86,57 +87,42 @@ public void teardown() {
manager.close();
}
if (emf != null) {
emf.close();
emf.close();
}
pds.close();
}

protected CountDownAsyncJobListener configureListener(int threads) {
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(threads);
((ExecutorServiceImpl) executorService).addAsyncJobListener(countDownListener);

return countDownListener;
}

@Test(timeout=30000)
public void testRunProcessWithAsyncHandler() throws Exception {
@Test(timeout = 30000)
public void testRunProcessWithAsyncHandlerDeleteUsingOlderThan() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(1);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
.userGroupCallback(userGroupCallback)
.entityManagerFactory(emf)
.addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
.registerableItemsFactory(new DefaultRegisterableItemsFactory() {
RuntimeEnvironment environment = configureEnvironment();

@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {

Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.ThrowExceptionCommand"));
return handlers;
}

})
.get();

manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);

ExecutionErrorStorage errorStorage = ((AbstractRuntimeManager) manager).getExecutionErrorManager().getStorage();

RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
Date startDate = new Date();
assertNotNull(ksession);

Date startDate = new Date();
ProcessInstance processInstance = ksession.startProcess("ScriptTask");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
long processInstanceId = processInstance.getId();
countDownListener.waitTillCompleted();

List<ExecutionError> errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());
scheduleLogCleanup(startDate, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd", manager.getIdentifier());

scheduleLogCleanup(startDate, null, true, null, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();
System.out.println("Aborting process instance " + processInstance.getId());
Expand All @@ -145,47 +131,195 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
// should not delete any errors as the process instance is still active
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

runtime.getKieSession().abortProcessInstance(processInstance.getId());

processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNull(processInstance);

Thread.sleep(1000);
scheduleLogCleanup(new Date(), "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());

scheduleLogCleanup(new Date(), null, true, null, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();

errors = errorStorage.list(0, 10);
assertEquals(0, errors.size());

}

private ExecutorService buildExecutorService() {

}

@Test(timeout = 30000)
public void testRunProcessWithAsyncHandlerDeleteUsingOlderThanPeriod() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(1);
RuntimeEnvironment environment = configureEnvironment();

manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);

ExecutionErrorStorage errorStorage = ((AbstractRuntimeManager) manager).getExecutionErrorManager().getStorage();

RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);

ProcessInstance processInstance = ksession.startProcess("ScriptTask");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
long processInstanceId = processInstance.getId();
countDownListener.waitTillCompleted();

List<ExecutionError> errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

// advance time 1 second
Thread.sleep(1000);

// delete errors which happened 1s or more ago
scheduleLogCleanup(null, "1s", true, null, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();
System.out.println("Aborting process instance " + processInstance.getId());
processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
// should not delete any errors as the process instance is still active
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

runtime.getKieSession().abortProcessInstance(processInstance.getId());

processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNull(processInstance);

// delete errors which happened 5s or more ago
scheduleLogCleanup(null, "5s", true, null, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();

// should not delete any errors as we wanted to delete errors from 5s ago
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

// delete errors which happened 1s or more ago
scheduleLogCleanup(null, "1s", true, null, "ScriptTask", String.valueOf(processInstanceId), "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();

errors = errorStorage.list(0, 10);
assertEquals(0, errors.size());

}

@Test(timeout = 30000)
public void testRunProcessWithAsyncHandlerDeleteUsingReoccurring() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(1);
RuntimeEnvironment environment = configureEnvironment();

manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);

ExecutionErrorStorage errorStorage = ((AbstractRuntimeManager) manager).getExecutionErrorManager().getStorage();

RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);

// schedule log cleanup every 3 seconds for processes with processId ScriptTask
scheduleLogCleanup(null, null, false, "3s", "ScriptTask", null, "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
// wait for the first cleanup
countDownListener.waitTillCompleted();

List<ExecutionError> errors = errorStorage.list(0, 10);
assertEquals(0, errors.size());

System.out.println("Process starting...");
ProcessInstance processInstance = ksession.startProcess("ScriptTask");
System.out.println("Process started...");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
// wait for the process instance to generate an error
countDownListener.reset(1);
System.out.println("Waiting to generate an error...");
countDownListener.waitTillCompleted();

errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

// wait for another log cleanup
countDownListener.reset(1);
countDownListener.waitTillCompleted();
System.out.println("Aborting process instance " + processInstance.getId());
processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
// should not delete any errors as the process instance is still active
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());

runtime.getKieSession().abortProcessInstance(processInstance.getId());

processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNull(processInstance);

// wait for another log cleanup
countDownListener.reset(1);
countDownListener.waitTillCompleted();

errors = errorStorage.list(0, 10);
assertEquals(0, errors.size());

}

private ExecutorService buildExecutorService() {
emf = EntityManagerFactoryManager.get().getOrCreate("org.jbpm.persistence.complete");

executorService = ExecutorServiceFactory.newExecutorService(emf);
executorService.setInterval(1);
executorService.setRetries(0);
executorService.init();

return executorService;
}

private void scheduleLogCleanup(Date olderThan,
String forProcess, String forProcessInstance, String dateFormat, String identifier) {
CommandContext commandContext = new CommandContext();
commandContext.setData("EmfName", "org.jbpm.persistence.complete");
commandContext.setData("SingleRun", "true");
if (olderThan != null) {
commandContext.setData("OlderThan", new SimpleDateFormat(dateFormat).format(olderThan));
}
commandContext.setData("DateFormat", dateFormat);
commandContext.setData("ForDeployment", identifier);
commandContext.setData("ForProcess", forProcess);
commandContext.setData("ForProcessInstance", forProcessInstance);
executorService.scheduleRequest("org.jbpm.executor.commands.ExecutionErrorCleanupCommand", commandContext);
}


private void scheduleLogCleanup(Date olderThan, String olderThanPeriod, boolean singleRun, String nextRun,
String forProcess, String forProcessInstance, String dateFormat, String identifier) {
CommandContext commandContext = new CommandContext();
commandContext.setData("EmfName", "org.jbpm.persistence.complete");
commandContext.setData("SingleRun", Boolean.toString(singleRun));

if (nextRun != null) {
commandContext.setData("NextRun", nextRun);
}

if (olderThan != null) {
commandContext.setData("OlderThan", new SimpleDateFormat(dateFormat).format(olderThan));
}

if (olderThanPeriod != null) {
commandContext.setData("OlderThanPeriod", olderThanPeriod);
}
commandContext.setData("DateFormat", dateFormat);
commandContext.setData("ForDeployment", identifier);
commandContext.setData("ForProcess", forProcess);
if (forProcessInstance != null) {
commandContext.setData("ForProcessInstance", forProcessInstance);
}
executorService.scheduleRequest("org.jbpm.executor.commands.ExecutionErrorCleanupCommand", commandContext);
}

private RuntimeEnvironment configureEnvironment() {
return RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
.userGroupCallback(userGroupCallback)
.entityManagerFactory(emf)
.addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
.registerableItemsFactory(new DefaultRegisterableItemsFactory() {

@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {

Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.ThrowExceptionCommand"));
return handlers;
}

})
.get();
}

}

0 comments on commit 75019bf

Please sign in to comment.