From 5d6a881a43965f778ed623c7755dd8737b538a88 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Wed, 17 Feb 2010 22:17:02 +0000 Subject: [PATCH] OPENJPA-1517: Reduce synchronization in LifecycleEventManager. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@911194 13f79535-47bb-0310-9956-ffa450edef68 --- .../event/TestLifecycleEventManager.java | 89 +++++++++++++++---- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java index caeccd3a4..604a77add 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestLifecycleEventManager.java @@ -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.event.StoreListener; 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; /** *

Test the {@link LifecycleEventManager}.

@@ -38,18 +43,14 @@ import org.apache.openjpa.persistence.OpenJPAPersistence; * @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 @@ public class TestLifecycleEventManager 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 @@ public class TestLifecycleEventManager 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 listeners = new ArrayList(); + 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 exceptions = new HashMap(); + 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 {