use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class TimerTest method testTimer.
@Test
@Disabled
public void testTimer() {
KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {
private static final long serialVersionUID = 510l;
public void signalEvent(String type, Object event) {
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
logger.info("Timer {} triggered", timer.getId());
counter++;
}
}
};
processInstance.setKnowledgeRuntime(((InternalWorkingMemory) kruntime.getKieSession()).getKnowledgeRuntime());
processInstance.setId("1234");
InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) kruntime.getKieSession()).getProcessRuntime());
processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
new Thread(() -> kruntime.getKieSession().fireUntilHalt()).start();
JobsService jobService = new LegacyInMemoryJobService(kruntime, new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
ProcessInstanceJobDescription desc = ProcessInstanceJobDescription.of(ExactExpirationTime.now(), processInstance.getStringId(), "test");
String jobId = jobService.scheduleProcessInstanceJob(desc);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
counter = 0;
desc = ProcessInstanceJobDescription.of(DurationExpirationTime.after(500), processInstance.getStringId(), "test");
jobId = jobService.scheduleProcessInstanceJob(desc);
assertEquals(0, counter);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
counter = 0;
desc = ProcessInstanceJobDescription.of(DurationExpirationTime.repeat(500, 300L), processInstance.getStringId(), "test");
jobId = jobService.scheduleProcessInstanceJob(desc);
assertEquals(0, counter);
try {
Thread.sleep(700);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
// we can't know exactly how many times this will fire as timers are not precise, but should be at least 4
assertTrue(counter >= 4);
jobService.cancelJob(jobId);
int lastCount = counter;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(lastCount, counter);
}
Aggregations