Search in sources :

Example 1 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA 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);
    }
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA 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);
}
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) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 3 with BeanA

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

the class SharedSessionParallelTest method testLongRunningRule.

@Test(timeout = 20000)
public void testLongRunningRule() throws InterruptedException {
    final int threadCount = 100;
    final int seed = threadCount + 200;
    final int objectCount = 1000;
    final String longRunningDrl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule longRunning " + "when " + "    $bean : BeanA($n : seed, seed > " + threadCount + ") " + "then " + "    modify($bean) { setSeed($n-1) };" + "    list.add(\"\" + $bean.getSeed());" + "end";
    final String listDrl = "global java.util.List list2;\n" + "rule listRule " + "when " + "    BeanA($n : seed, seed < " + threadCount + ") " + "then " + "    list2.add(\"\" + $n);" + "end";
    final KieSession kieSession = getKieBase(longRunningDrl, listDrl).newKieSession();
    final CyclicBarrier barrier = new CyclicBarrier(threadCount);
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final List<String> list2 = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        try {
            if (counter == 0) {
                kieSession.setGlobal("list", list);
                kieSession.setGlobal("list2", list2);
                kieSession.insert(new BeanA(seed));
                barrier.await();
                kieSession.fireAllRules();
                return true;
            } else {
                barrier.await();
                for (int i = 0; i < objectCount; i++) {
                    kieSession.insert(new BeanA(counter));
                }
                kieSession.fireAllRules();
                return true;
            }
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(threadCount, seed, list);
    checkList(1, threadCount, list2, (threadCount - 1) * objectCount);
}
Also used : 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) BeanA(org.drools.compiler.integrationtests.facts.BeanA) KieSession(org.kie.api.runtime.KieSession) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 4 with BeanA

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

the class SharedSessionParallelTest method testCorrectFirings2.

@Test(timeout = 20000)
public void testCorrectFirings2() throws InterruptedException {
    final int threadCount = 100;
    final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 " + "when " + "    BeanA($n : seed, seed == 0) " + "then " + "    list.add(\"\" + $n);" + "end";
    final KieSession kieSession = getKieBase(drl).newKieSession();
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        kieSession.setGlobal("list", list);
        kieSession.insert(new BeanA(counter % 2));
        kieSession.fireAllRules();
        return true;
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    Assertions.assertThat(list).contains("" + 0);
    Assertions.assertThat(list).doesNotContain("" + 1);
    final int expectedListSize = ((threadCount - 1) / 2) + 1;
    Assertions.assertThat(list).hasSize(expectedListSize);
}
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)

Example 5 with BeanA

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

the class SharedSessionParallelTest method testOneRulePerThread.

@Test(timeout = 20000)
public void testOneRulePerThread() throws InterruptedException {
    final int threadCount = 1000;
    final String[] drls = new String[threadCount];
    for (int i = 0; i < threadCount; i++) {
        drls[i] = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R" + i + " " + "when " + "    $bean : BeanA( seed == " + i + " ) " + "then " + "    list.add(\"" + i + "\");" + "end";
    }
    final KieSession kieSession = getKieBase(drls).newKieSession();
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        kieSession.setGlobal("list", list);
        kieSession.insert(new BeanA(counter));
        kieSession.fireAllRules();
        return true;
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(threadCount, list);
}
Also used : 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) BeanA(org.drools.compiler.integrationtests.facts.BeanA) 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