[jboss-svn-commits] JBL Code SVN: r34258 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/reteoo and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jul 28 10:47:41 EDT 2010


Author: tirelli
Date: 2010-07-28 10:47:40 -0400 (Wed, 28 Jul 2010)
New Revision: 34258

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooWorkingMemoryTest.java
Log:
JBRULES-2603: fixing reentrant problem on working memory action queue

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2010-07-28 13:13:47 UTC (rev 34257)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2010-07-28 14:47:40 UTC (rev 34258)
@@ -1462,21 +1462,24 @@
         try {
             startOperation();
             if( evaluatingActionQueue.compareAndSet( false, true ) ) {
-                if ( !this.actionQueue.isEmpty() ) {
-                    WorkingMemoryAction action = null;
+                try {
+                    if ( !this.actionQueue.isEmpty() ) {
+                        WorkingMemoryAction action = null;
 
-                    while ( (action = actionQueue.poll()) != null ) {
-                        try {
-                            action.execute( this );
-                        } catch ( Exception e ) {
-                            throw new RuntimeDroolsException( "Unexpected exception executing action " + action.toString(),
-                                                              e );
+                        while ( (action = actionQueue.poll()) != null ) {
+                            try {
+                                action.execute( this );
+                            } catch ( Exception e ) {
+                                throw new RuntimeDroolsException( "Unexpected exception executing action " + action.toString(),
+                                                                  e );
+                            }
                         }
                     }
+                } finally {
+                    evaluatingActionQueue.compareAndSet( true, false );
                 }
             }
         } finally {
-            evaluatingActionQueue.compareAndSet( true, false );
             endOperation();
         }
     }

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooWorkingMemoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooWorkingMemoryTest.java	2010-07-28 13:13:47 UTC (rev 34257)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooWorkingMemoryTest.java	2010-07-28 14:47:40 UTC (rev 34258)
@@ -16,13 +16,18 @@
 
 package org.drools.reteoo;
 
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import junit.framework.TestCase;
 
 import org.drools.Cheese;
+import org.drools.FactHandle;
 import org.drools.Person;
 import org.drools.RuleBase;
 import org.drools.RuleBaseFactory;
@@ -31,7 +36,8 @@
 import org.drools.common.EqualityKey;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.common.TruthMaintenanceSystem;
-import org.drools.FactHandle;
+import org.drools.common.WorkingMemoryAction;
+import org.drools.marshalling.impl.MarshallerWriteContext;
 import org.drools.spi.GlobalResolver;
 
 public class ReteooWorkingMemoryTest extends TestCase {
@@ -133,4 +139,50 @@
             }
         }               
     }
+    
+    public void testExecuteQueueActions() {
+        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        final ReteooWorkingMemory wm = (ReteooWorkingMemory) ruleBase.newStatefulSession();
+        final ReentrantAction action = new ReentrantAction();
+        wm.queueWorkingMemoryAction( action );
+        wm.executeQueuedActions();
+        assertEquals( 2, action.counter.get() );
+    }
+
+    private static class ReentrantAction implements WorkingMemoryAction {
+        // I am using AtomicInteger just as an int wrapper... nothing to do with concurrency here
+        public AtomicInteger counter = new AtomicInteger(0);
+        public void writeExternal(ObjectOutput out) throws IOException {}
+        public void readExternal(ObjectInput in) throws IOException,
+                                                ClassNotFoundException {}
+        public void write(MarshallerWriteContext context) throws IOException {}
+        public void execute(InternalWorkingMemory workingMemory) {
+            // the reentrant action must be executed completely
+            // before any of the final actions is executed
+            assertEquals( 0, counter.get() );
+            workingMemory.queueWorkingMemoryAction( new FinalAction( counter ) );
+            assertEquals( 0, counter.get() );
+            workingMemory.queueWorkingMemoryAction( new FinalAction( counter ) );
+            assertEquals( 0, counter.get() );
+            workingMemory.executeQueuedActions();
+            assertEquals( 0, counter.get() );
+            workingMemory.executeQueuedActions();
+            assertEquals( 0, counter.get() );
+        }
+    }
+    
+    private static class FinalAction extends ReentrantAction {
+        public AtomicInteger counter;
+        public FinalAction( AtomicInteger counter ) {
+            this.counter = counter;
+        }
+        public void execute(InternalWorkingMemory workingMemory) {
+            counter.incrementAndGet();
+            workingMemory.executeQueuedActions();
+            workingMemory.executeQueuedActions();
+        }
+    }
+    
+
+
 }



More information about the jboss-svn-commits mailing list