OPENJPA-1517: Reduce synchronization in LifecycleEventManager.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@911194 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2010-02-17 22:17:02 +00:00
parent d037890f74
commit 5d6a881a43
1 changed files with 71 additions and 18 deletions

View File

@ -19,10 +19,12 @@
package org.apache.openjpa.persistence.event; package org.apache.openjpa.persistence.event;
import org.apache.openjpa.persistence.event.common.apps.RuntimeTest1; import java.lang.Thread.UncaughtExceptionHandler;
import org.apache.openjpa.persistence.event.common.apps.RuntimeTest2; import java.util.ArrayList;
import org.apache.openjpa.persistence.event.common.apps.RuntimeTest4; import java.util.HashMap;
import org.apache.openjpa.persistence.common.utils.AbstractTestCase; import java.util.List;
import java.util.Map;
import org.apache.openjpa.event.LifecycleEvent; import org.apache.openjpa.event.LifecycleEvent;
import org.apache.openjpa.event.LifecycleEventManager; import org.apache.openjpa.event.LifecycleEventManager;
import org.apache.openjpa.event.LoadListener; import org.apache.openjpa.event.LoadListener;
@ -30,7 +32,10 @@ import org.apache.openjpa.event.StoreListener;
import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; 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> * <p>Test the {@link LifecycleEventManager}.</p>
@ -38,18 +43,14 @@ import org.apache.openjpa.persistence.OpenJPAPersistence;
* @author Abe White * @author Abe White
*/ */
public class TestLifecycleEventManager public class TestLifecycleEventManager
extends AbstractTestCase { extends AbstractTestCase implements UncaughtExceptionHandler{
public TestLifecycleEventManager(String s) { public TestLifecycleEventManager(String s) {
super(s, "eventcactusapp"); super(s, "eventcactusapp");
} }
public void testAllClassListener() { public void atestAllClassListener() {
MetaDataRepository repos = MetaDataRepository repos = getMDR();
((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(
OpenJPAPersistence.createEntityManagerFactory("TestConv2", "")))
.
getConfiguration().getMetaDataRepositoryInstance();
ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true); ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true);
LifecycleEventManager mgr = new LifecycleEventManager(); LifecycleEventManager mgr = new LifecycleEventManager();
RuntimeTest2 pc = new RuntimeTest2(); RuntimeTest2 pc = new RuntimeTest2();
@ -118,12 +119,8 @@ public class TestLifecycleEventManager
assertEquals(2, listener.store); assertEquals(2, listener.store);
} }
public void testBaseClassListener() { public void atestBaseClassListener() {
MetaDataRepository repos = MetaDataRepository repos = getMDR();
((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(
OpenJPAPersistence.createEntityManagerFactory("TestConv2", "")))
.
getConfiguration().getMetaDataRepositoryInstance();
ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true); ClassMetaData meta = repos.getMetaData(RuntimeTest2.class, null, true);
LifecycleEventManager mgr = new LifecycleEventManager(); LifecycleEventManager mgr = new LifecycleEventManager();
@ -194,6 +191,62 @@ public class TestLifecycleEventManager
assertEquals(3, listener.load); assertEquals(3, listener.load);
assertEquals(1, listener.store); 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 private static class Listener
implements LoadListener, StoreListener { implements LoadListener, StoreListener {