use of org.drools.mvel.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHaltWithExpiration.
@Test(timeout = 40000L)
public void testFireUntilHaltWithExpiration() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
sb.append("declare MyEvent @role( event ) @expires( 20ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 5; i++) {
sb.append(getRuleWithEventForExpiration(i));
}
// See DROOLS-6352 : To avoid bias in CompositePartitionAwareObjectSinkAdapter.partitionedPropagators by artificial repetition of rule pattern
sb.append("rule R_ex1\n when MyEvent(id == 100)\n then\n end\n");
for (int i = 5; i < 10; i++) {
sb.append(getRuleWithEventForExpiration(i));
}
KieSessionConfiguration sessionConfig = RuleBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", streamConfig, sb.toString());
final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, streamConfig, MultithreadEvaluationOption.YES);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
DebugList<Integer> list = new DebugList<Integer>();
CountDownLatch done1 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 10) {
done1.countDown();
}
});
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
new Thread(ksession::fireUntilHalt).start();
try {
try {
done1.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(10, list.size());
list.clear();
CountDownLatch done2 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done2.countDown();
}
});
ksession.insert(1);
sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
try {
done2.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
list.clear();
CountDownLatch done3 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done3.countDown();
}
});
sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
try {
done3.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
} finally {
ksession.halt();
ksession.dispose();
}
}
use of org.drools.mvel.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method getMultipleParallelKieSessionsFireUntilHaltCallable.
private Callable<Void> getMultipleParallelKieSessionsFireUntilHaltCallable(KieBase kBase, boolean asyncInsert) {
return () -> {
KieSession ksession = kBase.newKieSession();
assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).isTrue();
CountDownLatch done = new CountDownLatch(1);
DebugList<Integer> list = new DebugList<Integer>();
list.onItemAdded = (l -> {
if (l.size() == 10) {
ksession.halt();
done.countDown();
}
});
ksession.setGlobal("list", list);
new Thread(ksession::fireUntilHalt).start();
if (asyncInsert) {
StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
for (int i = 0; i < 10; i++) {
session.insertAsync(i);
session.insertAsync("" + String.valueOf(i));
}
} else {
insertFacts(ksession, 10);
}
try {
done.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertThat(list.size()).isEqualTo(10);
return null;
};
}
Aggregations