use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class MultithreadTest method testFireUntilHaltAndDispose.
@Test(timeout = 10000)
public void testFireUntilHaltAndDispose() throws InterruptedException {
// DROOLS-1103
final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + " String()\n" + "then\n" + "end";
final KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
final KieBase kbase = helper.build(EventProcessingOption.STREAM);
final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption(TimedRuleExecutionOption.YES);
final KieSession ksession = kbase.newKieSession(ksconf, null);
new Thread() {
@Override
public void run() {
ksession.fireUntilHalt();
}
}.start();
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// do nothing
}
ksession.insert("xxx");
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// do nothing
}
ksession.dispose();
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class MultithreadTest method testConcurrentDelete.
@Test(timeout = 20000)
public void testConcurrentDelete() {
final String drl = "import " + SlowBean.class.getCanonicalName() + ";\n" + "rule R when\n" + " $sb1: SlowBean() \n" + " $sb2: SlowBean( id > $sb1.id ) \n" + "then " + " System.out.println($sb2 + \" > \"+ $sb1);" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final int BEAN_NR = 4;
for (int step = 0; step < 2; step++) {
final FactHandle[] fhs = new FactHandle[BEAN_NR];
for (int i = 0; i < BEAN_NR; i++) {
fhs[i] = ksession.insert(new SlowBean(i + step * BEAN_NR));
}
final CyclicBarrier barrier = new CyclicBarrier(2);
new Thread(new Runnable() {
@Override
public void run() {
ksession.fireAllRules();
try {
barrier.await();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}).start();
try {
Thread.sleep(15L);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < BEAN_NR; i++) {
if (i % 2 == 1) {
ksession.delete(fhs[i]);
}
}
try {
barrier.await();
} catch (final Exception e) {
throw new RuntimeException(e);
}
System.out.println("Done step " + step);
}
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class SharedSessionParallelTest method testCheckOneThreadOnly.
@Test(timeout = 20000)
public void testCheckOneThreadOnly() throws InterruptedException {
final int threadCount = 100;
final List<String> list = Collections.synchronizedList(new ArrayList<>());
final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 " + "when " + " BeanA($n : seed) " + "then " + " list.add(\"\" + $n);" + "end";
final KieSession kieSession = getKieBase(drl).newKieSession();
final CountDownLatch latch = new CountDownLatch(threadCount);
final TestExecutor exec = counter -> {
kieSession.setGlobal("list", list);
kieSession.insert(new BeanA(counter));
latch.countDown();
if (counter == 0) {
try {
latch.await();
} catch (final InterruptedException e) {
e.printStackTrace();
return false;
}
return kieSession.fireAllRules() == threadCount;
}
return true;
};
parallelTest(threadCount, exec);
kieSession.dispose();
Assertions.assertThat(list).hasSize(threadCount);
for (int i = 0; i < threadCount; i++) {
Assertions.assertThat(list).contains("" + i);
}
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class SharedSessionParallelTest method testNoExceptions.
@Test(timeout = 20000)
public void testNoExceptions() throws InterruptedException {
final String drl = "rule R1 when String() then end";
final int repetitions = 100;
final int numberOfObjects = 1000;
final int countOfThreads = 100;
for (int i = 0; i < repetitions; i++) {
final KieSession kieSession = getKieBase(drl).newKieSession();
parallelTest(countOfThreads, counter -> {
try {
for (int j = 0; j < numberOfObjects; j++) {
kieSession.insert("test_" + numberOfObjects);
}
kieSession.fireAllRules();
return true;
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
});
kieSession.dispose();
}
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class SharedSessionParallelTest method testCountdownBean.
@Test(timeout = 20000)
public void testCountdownBean() throws InterruptedException {
final int threadCount = 100;
final int seed = 1000;
final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule countdown " + "when " + " $bean : BeanA($n : seed, seed > 0 ) " + "then " + " modify($bean) { setSeed($n-1) };" + " list.add(\"\" + $bean.getSeed());" + "end";
final KieSession kieSession = getKieBase(drl).newKieSession();
final CyclicBarrier barrier = new CyclicBarrier(threadCount);
final List<String> list = Collections.synchronizedList(new ArrayList<>());
final BeanA bean = new BeanA(seed);
final TestExecutor exec = counter -> {
try {
if (counter == 0) {
kieSession.setGlobal("list", list);
kieSession.insert(bean);
}
barrier.await();
kieSession.fireAllRules();
return true;
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
};
parallelTest(threadCount, exec);
kieSession.dispose();
checkList(seed, list);
Assertions.assertThat(bean).hasFieldOrPropertyWithValue("seed", 0);
}
Aggregations