use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class MultithreadTest method testConcurrentFireAndDispose.
@Test(timeout = 10000)
public void testConcurrentFireAndDispose() 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);
final Thread t1 = new Thread() {
@Override
public void run() {
LOG.info("before: sleep, dispose().");
try {
Thread.sleep(100);
} catch (final InterruptedException _e) {
}
LOG.info("before: dispose().");
ksession.dispose();
LOG.info("after: dispose().");
}
};
t1.setDaemon(true);
t1.start();
try {
int i = 0;
LOG.info("before: while.");
while (true) {
ksession.insert("" + i++);
ksession.fireAllRules();
}
} catch (final IllegalStateException e) {
// java.lang.IllegalStateException: Illegal method call. This session was previously disposed.
// ignore and exit
LOG.info("after: while.");
} catch (final java.util.concurrent.RejectedExecutionException e) {
e.printStackTrace();
Assertions.fail("java.util.concurrent.RejectedExecutionException should not happen");
}
LOG.info("last line of test.");
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class MultithreadTest method testJittingShortComparison.
@Test(timeout = 20000)
public void testJittingShortComparison() {
// DROOLS-1633
final String drl = "import " + BeanA.class.getCanonicalName() + "\n;" + "global java.util.List list;" + "rule R when\n" + " $a1: BeanA($sv1 : shortValue)\n" + " $b2: BeanA(shortValue != $sv1)\n" + "then\n" + " list.add(\"FIRED\");\n" + "end";
final List<String> list = Collections.synchronizedList(new ArrayList());
final RuleBaseConfiguration rbc = (RuleBaseConfiguration) KieServices.Factory.get().newKieBaseConfiguration();
rbc.setJittingThreshold(0);
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(rbc);
final int threadNr = 1000;
final Thread[] threads = new Thread[threadNr];
for (int i = 0; i < threadNr; i++) {
threads[i] = new Thread(new SessionRunner(kbase, list));
}
for (int i = 0; i < threadNr; i++) {
threads[i].start();
}
for (int i = 0; i < threadNr; i++) {
try {
threads[i].join();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(list).hasSize(0);
}
use of org.kie.internal.utils.KieHelper 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.internal.utils.KieHelper 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.internal.utils.KieHelper in project drools by kiegroup.
the class MarshallingTest method testMarshallWithTimedRule.
@Test
public void testMarshallWithTimedRule() {
// DROOLS-795
String drl = "rule \"Rule A Timeout\"\n" + "when\n" + " String( this == \"ATrigger\" )\n" + "then\n" + " insert (new String( \"A-Timer\") );\n" + "end\n" + "\n" + "rule \"Timer For rule A Timeout\"\n" + " timer ( int: 5s )\n" + "when\n" + " String( this == \"A-Timer\")\n" + "then\n" + " delete ( \"A-Timer\" );\n" + " delete ( \"ATrigger\" );\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(EqualityBehaviorOption.EQUALITY, DeclarativeAgendaOption.ENABLED, EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(sessionConfig, null);
ksession.insert("ATrigger");
assertEquals(1, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(2, ksession.getFactCount());
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(4, TimeUnit.SECONDS);
assertEquals(2, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(2, ksession.getFactCount());
ksession = marshallAndUnmarshall(kbase, ksession, sessionConfig);
clock = ksession.getSessionClock();
clock.advanceTime(4, TimeUnit.SECONDS);
assertEquals(2, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(0, ksession.getFactCount());
}
Aggregations