openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r911194 - /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java
Date Wed, 17 Feb 2010 22:17:02 GMT
Author: curtisr7
Date: Wed Feb 17 22:17:02 2010
New Revision: 911194

URL: http://svn.apache.org/viewvc?rev=911194&view=rev
Log:
OPENJPA-1517: Reduce synchronization in LifecycleEventManager.

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java?rev=911194&r1=911193&r2=911194&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java
Wed Feb 17 22:17:02 2010
@@ -19,10 +19,12 @@
 package org.apache.openjpa.persistence.event;
 
 
-import org.apache.openjpa.persistence.event.common.apps.RuntimeTest1;
-import org.apache.openjpa.persistence.event.common.apps.RuntimeTest2;
-import org.apache.openjpa.persistence.event.common.apps.RuntimeTest4;
-import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+import java.lang.Thread.UncaughtExceptionHandler;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.openjpa.event.LifecycleEvent;
 import org.apache.openjpa.event.LifecycleEventManager;
 import org.apache.openjpa.event.LoadListener;
@@ -30,7 +32,10 @@
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.MetaDataRepository;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
-import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+import org.apache.openjpa.persistence.event.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.event.common.apps.RuntimeTest2;
+import org.apache.openjpa.persistence.event.common.apps.RuntimeTest4;
 
 /**
  * <p>Test the {@link LifecycleEventManager}.</p>
@@ -38,18 +43,14 @@
  * @author Abe White
  */
 public class TestLifecycleEventManager
-    extends AbstractTestCase {
+    extends AbstractTestCase implements UncaughtExceptionHandler{
 
     public TestLifecycleEventManager(String s) {
         super(s, "eventcactusapp");
     }
 
-    public void testAllClassListener() {
-        MetaDataRepository repos =
-            ((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(
-                OpenJPAPersistence.createEntityManagerFactory("TestConv2", "")))
-                .
-                    getConfiguration().getMetaDataRepositoryInstance();
+    public void atestAllClassListener() {
+        MetaDataRepository repos = getMDR();
         ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true);
         LifecycleEventManager mgr = new LifecycleEventManager();
         RuntimeTest2 pc = new RuntimeTest2();
@@ -118,12 +119,8 @@
         assertEquals(2, listener.store);
     }
 
-    public void testBaseClassListener() {
-        MetaDataRepository repos =
-            ((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(
-                OpenJPAPersistence.createEntityManagerFactory("TestConv2", "")))
-                .
-                    getConfiguration().getMetaDataRepositoryInstance();
+    public void atestBaseClassListener() {
+        MetaDataRepository repos = getMDR();
         ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true);
 
         LifecycleEventManager mgr = new LifecycleEventManager();
@@ -194,6 +191,62 @@
         assertEquals(3, listener.load);
         assertEquals(1, listener.store);
     }
+    
+    public void testMultiThreaded() throws Exception{
+
+        for(int z = 0; z < 1000; z++){
+            
+            final LifecycleEventManager mgr = new LifecycleEventManager();
+            final List<Listener> listeners = new ArrayList<Listener>();
+            final ClassMetaData meta = getMDR().getMetaData(RuntimeTest2.class, null, true);
+            final RuntimeTest2 pc = new RuntimeTest2();
+            
+            for(int i = 0 ; i<10000;i++){
+                Listener l = new Listener();
+                mgr.addListener(l, null);
+                listeners.add(l);
+            }
+            
+            Thread removerThread = new Thread(){
+                public void run() {
+                    for(Listener l : listeners){
+                        mgr.removeListener(l);
+                    }
+                }
+            };
+            Thread hasLoadListenersThread = new Thread(){
+                @Override
+                public void run() {
+                    for(Listener l : listeners){
+                        if(mgr.hasLoadListeners(pc, meta) == false){
+                            System.out.println("false!");       
+                        }
+                     
+                    }
+                }
+            };
+            removerThread.setUncaughtExceptionHandler(this);
+            hasLoadListenersThread.setUncaughtExceptionHandler(this);
+            
+            hasLoadListenersThread.start();
+            removerThread.start();
+            
+            removerThread.join();
+            hasLoadListenersThread.join();
+            
+            Throwable t = exceptions.get(hasLoadListenersThread);
+            assertNull(t);
+        }
+    }
+    Map<Thread, Throwable> exceptions = new HashMap<Thread, Throwable>();
+    public void uncaughtException(Thread thread, Throwable throwable) {
+        exceptions.put(thread, throwable);
+        
+    }
+
+    private MetaDataRepository getMDR() {
+        return ((OpenJPAEntityManagerFactorySPI)getEmf()).getConfiguration().getMetaDataRepositoryInstance();
+    }
 
     private static class Listener
         implements LoadListener, StoreListener {



Mime
View raw message