[jboss-svn-commits] JBL Code SVN: r18555 - in labs/jbossrules/branches/4.0.x/drools-core/src: test/java/org/drools/base/extractors and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Feb 21 13:16:59 EST 2008


Author: tirelli
Date: 2008-02-21 13:16:59 -0500 (Thu, 21 Feb 2008)
New Revision: 18555

Modified:
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/base/extractors/MVELClassFieldExtractor.java
   labs/jbossrules/branches/4.0.x/drools-core/src/test/java/org/drools/base/extractors/MVELClassFieldExtractorTest.java
Log:
JBRULES-1480: fixing multithread issue on MVEL class field extractor

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/base/extractors/MVELClassFieldExtractor.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/base/extractors/MVELClassFieldExtractor.java	2008-02-21 17:01:50 UTC (rev 18554)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/base/extractors/MVELClassFieldExtractor.java	2008-02-21 18:16:59 UTC (rev 18555)
@@ -43,7 +43,6 @@
 
     private CompiledExpression mvelExpression = null;
     private Map extractors = null;
-    private Map variables = null; 
 
     public MVELClassFieldExtractor(Class clazz,
                                    String fieldName,
@@ -52,7 +51,6 @@
                Object.class, // fieldType
                ValueType.determineValueType( Object.class ) ); // value type
         this.extractors = new HashMap();
-        this.variables = new HashMap();
 
         ExpressionCompiler compiler = new ExpressionCompiler( fieldName );
         this.mvelExpression = compiler.compile();
@@ -70,14 +68,15 @@
      * @see org.drools.base.extractors.BaseObjectClassFieldExtractor#getValue(java.lang.Object)
      */
     public Object getValue(InternalWorkingMemory workingMemory, Object object) {
+        Map variables = new HashMap();
         for( Iterator it = this.extractors.entrySet().iterator(); it.hasNext(); ) {
             Map.Entry entry = (Map.Entry) it.next();
             String var = (String) entry.getKey();
             FieldExtractor extr = (FieldExtractor) entry.getValue();
             
-            this.variables.put( var, extr.getValue( workingMemory, object ));
+            variables.put( var, extr.getValue( workingMemory, object ));
         }
-        return MVEL.executeExpression( mvelExpression, this.variables );
+        return MVEL.executeExpression( mvelExpression, variables );
     }
 
 }

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/test/java/org/drools/base/extractors/MVELClassFieldExtractorTest.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/test/java/org/drools/base/extractors/MVELClassFieldExtractorTest.java	2008-02-21 17:01:50 UTC (rev 18554)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/test/java/org/drools/base/extractors/MVELClassFieldExtractorTest.java	2008-02-21 18:16:59 UTC (rev 18555)
@@ -1,5 +1,7 @@
 package org.drools.base.extractors;
 
+import java.util.Vector;
+
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
@@ -10,31 +12,47 @@
 
 public class MVELClassFieldExtractorTest extends TestCase {
 
-    Extractor extractor = ClassFieldExtractorCache.getInstance().getExtractor( Person.class,
-                                                                               "addresses['home'].street",
-                                                                               getClass().getClassLoader() );
-    Person    person    = null;
+    Extractor               extractor = ClassFieldExtractorCache.getInstance().getExtractor( Person.class,
+                                                                                             "addresses['home'].street",
+                                                                                             getClass().getClassLoader() );
+    private final Person[]  person    = new Person[2];
+    private final Address[] business  = new Address[2];
+    private final Address[] home      = new Address[2];
 
     protected void setUp() throws Exception {
         super.setUp();
-        person = new Person( "bob",
-                             30 );
-        Address business = new Address( "Business Street",
-                                        "999",
-                                        null );
-        Address home = new Address( "Home Street",
-                                    "555",
-                                    "55555555" );
-        person.getAddresses().put( "business",
-                                   business );
-        person.getAddresses().put( "home",
-                                   home );
+        person[0] = new Person( "bob",
+                                30 );
+        business[0] = new Address( "Business Street",
+                                   "999",
+                                   null );
+        home[0] = new Address( "Home Street",
+                               "555",
+                               "55555555" );
+        person[0].getAddresses().put( "business",
+                                      business[0] );
+        person[0].getAddresses().put( "home",
+                                      home[0] );
+
+        person[1] = new Person( "mark",
+                                35 );
+        business[1] = new Address( "Another Business Street",
+                                   "999",
+                                   null );
+        home[1] = new Address( "Another Home Street",
+                               "555",
+                               "55555555" );
+        person[1].getAddresses().put( "business",
+                                      business[1] );
+        person[1].getAddresses().put( "home",
+                                      home[1] );
+
     }
 
     public void testGetBooleanValue() {
         try {
             this.extractor.getBooleanValue( null,
-                                            this.person );
+                                            this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -44,7 +62,7 @@
     public void testGetByteValue() {
         try {
             this.extractor.getByteValue( null,
-                                         this.person );
+                                         this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -54,7 +72,7 @@
     public void testGetCharValue() {
         try {
             this.extractor.getCharValue( null,
-                                         this.person );
+                                         this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -64,7 +82,7 @@
     public void testGetDoubleValue() {
         try {
             this.extractor.getDoubleValue( null,
-                                           this.person );
+                                           this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -74,7 +92,7 @@
     public void testGetFloatValue() {
         try {
             this.extractor.getFloatValue( null,
-                                          this.person );
+                                          this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -84,7 +102,7 @@
     public void testGetIntValue() {
         try {
             this.extractor.getIntValue( null,
-                                        this.person );
+                                        this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -94,7 +112,7 @@
     public void testGetLongValue() {
         try {
             this.extractor.getLongValue( null,
-                                         this.person );
+                                         this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -104,7 +122,7 @@
     public void testGetShortValue() {
         try {
             this.extractor.getShortValue( null,
-                                          this.person );
+                                          this.person[0] );
             fail( "Should have throw an exception" );
         } catch ( final Exception e ) {
             // success
@@ -113,11 +131,11 @@
 
     public void testGetValue() {
         try {
-            Assert.assertEquals( "Home Street",
+            Assert.assertEquals( home[0].getStreet(),
                                  this.extractor.getValue( null,
-                                                          this.person ) );
+                                                          this.person[0] ) );
             Assert.assertTrue( this.extractor.getValue( null,
-                                                        this.person ) instanceof String );
+                                                        this.person[0] ) instanceof String );
         } catch ( final Exception e ) {
             fail( "Should not throw an exception" );
         }
@@ -126,16 +144,60 @@
     public void testIsNullValue() {
         try {
             Assert.assertFalse( this.extractor.isNullValue( null,
-                                                            this.person ) );
+                                                            this.person[0] ) );
 
             Extractor nullExtractor = ClassFieldExtractorCache.getInstance().getExtractor( Person.class,
                                                                                            "addresses['business'].phone",
                                                                                            getClass().getClassLoader() );
             Assert.assertTrue( nullExtractor.isNullValue( null,
-                                                          this.person ) );
+                                                          this.person[0] ) );
         } catch ( final Exception e ) {
             fail( "Should not throw an exception" );
         }
     }
 
+    public void testMultithreads() {
+        final int THREAD_COUNT = 30;
+
+        try {
+            final Vector errors = new Vector();
+
+            final Thread t[] = new Thread[THREAD_COUNT];
+            for ( int j = 0; j < 10; j++ ) {
+                for ( int i = 0; i < t.length; i++ ) {
+                    final int ID = i;
+                    t[i] = new Thread() {
+                        public void run() {
+                            try {
+                                final int ITERATIONS = 300;
+                                for ( int k = 0; k < ITERATIONS; k++ ) {
+                                    String value = (String) extractor.getValue( null,
+                                                                                person[ID % 2] );
+                                    if ( !home[ID % 2].getStreet().equals( value ) ) {
+                                        errors.add( "THREAD(" + ID + "): Wrong value at iteration " + k + ". Value='" + value + "'\n" );
+                                    }
+                                }
+                            } catch ( Exception ex ) {
+                                ex.printStackTrace();
+                                errors.add( ex );
+                            }
+                        }
+
+                    };
+                    t[i].start();
+                }
+                for ( int i = 0; i < t.length; i++ ) {
+                    t[i].join();
+                }
+            }
+            if ( !errors.isEmpty() ) {
+                System.out.println(errors.toString());
+                fail( " Errors occured during execution " );
+            }
+        } catch ( InterruptedException e ) {
+            e.printStackTrace();
+            fail( "Unexpected exception running test: " + e.getMessage() );
+        }
+    }
+
 }




More information about the jboss-svn-commits mailing list