[jboss-svn-commits] JBL Code SVN: r18323 - in labs/jbossrules/trunk/drools-compiler/src/test: java/org/drools/integrationtests and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 5 14:37:03 EST 2008


Author: tirelli
Date: 2008-02-05 14:37:03 -0500 (Tue, 05 Feb 2008)
New Revision: 18323

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MultithreadTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultithreadRulebaseSharing.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Child.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Parent.java
Log:
JBRULES-1392: adding multi-thread test case

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Child.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Child.java	2008-02-05 19:26:39 UTC (rev 18322)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Child.java	2008-02-05 19:37:03 UTC (rev 18323)
@@ -2,6 +2,8 @@
 
 public class Child extends Parent {
 
+    private Parent parent;
+    
     public Child() {
     }
 
@@ -9,4 +11,18 @@
         super( name );
     }
 
+    /**
+     * @return the parent
+     */
+    public Parent getParent() {
+        return parent;
+    }
+
+    /**
+     * @param parent the parent to set
+     */
+    public void setParent(Parent parent) {
+        this.parent = parent;
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Parent.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Parent.java	2008-02-05 19:26:39 UTC (rev 18322)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Parent.java	2008-02-05 19:37:03 UTC (rev 18323)
@@ -21,6 +21,8 @@
  *
  */
 public class Parent extends GrandParent {
+    
+    private GrandParent grandParent;
 
     public Parent() {
     }
@@ -29,4 +31,18 @@
         super( name );
     }
 
+    /**
+     * @return the parent
+     */
+    public GrandParent getGrandParent() {
+        return grandParent;
+    }
+
+    /**
+     * @param parent the parent to set
+     */
+    public void setGrandParent(GrandParent parent) {
+        this.grandParent = parent;
+    }
+
 }

Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MultithreadTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MultithreadTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MultithreadTest.java	2008-02-05 19:37:03 UTC (rev 18323)
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2008 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Feb 5, 2008
+ */
+
+package org.drools.integrationtests;
+
+import java.io.InputStreamReader;
+
+import junit.framework.TestCase;
+
+import org.drools.Child;
+import org.drools.GrandParent;
+import org.drools.Parent;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.StatefulSession;
+import org.drools.compiler.PackageBuilder;
+
+/**
+ * This is a test case for multi-thred issues
+ * 
+ * @author etirelli
+ */
+public class MultithreadTest extends TestCase {
+
+    /**
+     * @inheritDoc
+     *
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    /**
+     * @inheritDoc
+     *
+     * @see junit.framework.TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testRuleBaseConcurrentCompilation() {
+        final int THREAD_COUNT = 30;
+        try {
+            boolean success = true;
+            final PackageBuilder builder = new PackageBuilder();
+            builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_MultithreadRulebaseSharing.drl" ) ) );
+            RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+            ruleBase.addPackage( builder.getPackage() );
+            final Thread[] t = new Thread[THREAD_COUNT];
+            final RulebaseRunner[] r = new RulebaseRunner[THREAD_COUNT];
+            for ( int i = 0; i < t.length; i++ ) {
+                r[i] = new RulebaseRunner( i,
+                                           ruleBase );
+                t[i] = new Thread( r[i],
+                                   "thread-" + i );
+                t[i].start();
+            }
+            for ( int i = 0; i < t.length; i++ ) {
+                t[i].join();
+                if ( r[i].getStatus() == RulebaseRunner.Status.FAIL ) {
+                    success = false;
+                }
+            }
+            if ( !success ) {
+                fail( "Multithread test failed. Look at the stack traces for details. " );
+            }
+        } catch ( Exception e ) {
+            e.printStackTrace();
+            fail( "Should not raise any exception: " + e.getMessage() );
+        }
+    }
+
+    public static class RulebaseRunner
+        implements
+        Runnable {
+
+        private static final int ITERATIONS = 300;
+        private final int        id;
+        private final RuleBase   rulebase;
+        private Status           status;
+
+        public RulebaseRunner(final int id,
+                              final RuleBase rulebase) {
+            this.id = id;
+            this.rulebase = rulebase;
+            this.status = Status.SUCCESS;
+        }
+
+        public void run() {
+            try {
+                StatefulSession session2 = this.rulebase.newStatefulSession();
+
+                for ( int k = 0; k < ITERATIONS; k++ ) {
+                    GrandParent gp = new GrandParent( "bob" );
+                    Parent parent = new Parent( "mark" );
+                    parent.setGrandParent( gp );
+
+                    Child child = new Child( "mike" );
+                    child.setParent( parent );
+
+                    session2.insert( gp );
+                    session2.insert( parent );
+                    session2.insert( child );
+                }
+
+                session2.fireAllRules();
+                session2.dispose();
+
+            } catch ( Exception e ) {
+                this.status = Status.FAIL;
+                System.out.println( Thread.currentThread().getName() + " failed: " + e.getMessage() );
+                e.printStackTrace();
+            }
+        }
+
+        public static enum Status {
+            SUCCESS, FAIL
+        }
+
+        /**
+         * @return the id
+         */
+        public int getId() {
+            return id;
+        }
+
+        /**
+         * @return the status
+         */
+        public Status getStatus() {
+            return status;
+        }
+
+    }
+
+}

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultithreadRulebaseSharing.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultithreadRulebaseSharing.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultithreadRulebaseSharing.drl	2008-02-05 19:37:03 UTC (rev 18323)
@@ -0,0 +1,9 @@
+package org.drools;
+
+rule "test multi-thread rulebase sharing"
+when
+    $gp : GrandParent()
+    $ch : Child( parent.grandParent == $gp )
+then
+    // do something
+end
\ No newline at end of file




More information about the jboss-svn-commits mailing list