Search in sources :

Example 11 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class SharedSessionParallelTest method testCountdownBean2.

@Test(timeout = 20000)
public void testCountdownBean2() 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 List<String> list = Collections.synchronizedList(new ArrayList<>());
    final BeanA[] beans = new BeanA[threadCount];
    final TestExecutor exec = counter -> {
        final BeanA bean = new BeanA(seed);
        beans[counter] = bean;
        try {
            kieSession.setGlobal("list", list);
            kieSession.insert(bean);
            kieSession.fireAllRules();
            return true;
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(0, seed, list, seed * threadCount);
    for (final BeanA bean : beans) {
        Assertions.assertThat(bean).hasFieldOrPropertyWithValue("seed", 0);
    }
}
Also used : BeanA(org.drools.compiler.integrationtests.facts.BeanA) CountDownLatch(java.util.concurrent.CountDownLatch) Arrays(java.util.Arrays) List(java.util.List) CyclicBarrier(java.util.concurrent.CyclicBarrier) BeanA(org.drools.compiler.integrationtests.facts.BeanA) RunWith(org.junit.runner.RunWith) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) KieSession(org.kie.api.runtime.KieSession) Collections(java.util.Collections) Parameterized(org.junit.runners.Parameterized) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 List (java.util.List)11 Assertions (org.assertj.core.api.Assertions)11 BeanA (org.drools.compiler.integrationtests.facts.BeanA)11 Test (org.junit.Test)11 RunWith (org.junit.runner.RunWith)11 Parameterized (org.junit.runners.Parameterized)11 KieSession (org.kie.api.runtime.KieSession)11 Collections (java.util.Collections)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 CyclicBarrier (java.util.concurrent.CyclicBarrier)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BeanB (org.drools.compiler.integrationtests.facts.BeanB)2 KieBase (org.kie.api.KieBase)2 QueryResults (org.kie.api.runtime.rule.QueryResults)2 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)2