use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.
the class SessionsPoolTest method testStatelessKieSessionsPool.
@Test
public void testStatelessKieSessionsPool() {
KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
StatelessKieSession session = pool.newStatelessKieSession();
List<String> list = new ArrayList<>();
session.setGlobal("list", list);
session.execute("test");
assertEquals(1, list.size());
list.clear();
session.execute("test");
assertEquals(1, list.size());
}
use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.
the class SessionsPoolTest method testListenersReset.
@Test
public void testListenersReset() {
final KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
KieSession ksession = pool.newKieSession();
try {
ksession.addEventListener(new DefaultAgendaEventListener());
ksession.addEventListener(new DefaultRuleRuntimeEventListener());
((RuleEventManager) ksession).addEventListener(new RuleEventListener() {
});
} finally {
ksession.dispose();
}
ksession = pool.newKieSession();
try {
Assertions.assertThat(ksession.getAgendaEventListeners()).hasSize(0);
Assertions.assertThat(ksession.getRuleRuntimeEventListeners()).hasSize(0);
Assertions.assertThat(((EventSupport) ksession).getRuleEventSupport().getEventListeners()).hasSize(0);
} finally {
ksession.dispose();
}
}
use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.
the class SessionsPoolTest method testKieSessionsPoolInMultithreadEnv.
@Test
public void testKieSessionsPoolInMultithreadEnv() throws InterruptedException, ExecutionException {
KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(4);
final int THREAD_NR = 10;
ExecutorService executor = Executors.newFixedThreadPool(THREAD_NR, r -> {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
});
try {
CompletionService<Boolean> ecs = new ExecutorCompletionService<>(executor);
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(() -> {
try {
KieSession ksession = pool.newKieSession();
try {
checkKieSession(ksession);
} finally {
ksession.dispose();
}
return true;
} catch (final Exception e) {
return false;
}
});
}
boolean success = true;
for (int i = 0; i < THREAD_NR; i++) {
success = ecs.take().get() && success;
}
assertTrue(success);
} finally {
executor.shutdown();
}
pool.shutdown();
try {
pool.newKieSession();
fail("after pool shutdown it shouldn't be possible to get sessions from it");
} catch (IllegalStateException e) {
}
}
use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.
the class SessionsPoolTest method testKieSessionsPool.
@Test
public void testKieSessionsPool() {
KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
KieSession ksession = pool.newKieSession();
try {
checkKieSession(ksession);
} finally {
ksession.dispose();
}
try {
ksession.insert("test2");
fail("it shouldn't be possible to operate on a disposed session even if created from a pool");
} catch (Exception e) {
}
KieSession ksession2 = pool.newKieSession();
// using a pool with only one session so it should return the same one as before
assertSame(ksession, ksession2);
assertNull(ksession2.getGlobal("list"));
checkKieSession(ksession2);
pool.shutdown();
try {
ksession.insert("test3");
fail("after pool shutdown all sessions created from it should be disposed");
} catch (IllegalStateException e) {
}
try {
pool.newKieSession();
fail("after pool shutdown it shouldn't be possible to get sessions from it");
} catch (IllegalStateException e) {
}
}
Aggregations