openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r1383729 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test: java/org/apache/openjpa/audit/TestAudit.java java/org/apache/openjpa/audit/X.java resources/META-INF/persistence.xml
Date Wed, 12 Sep 2012 01:28:32 GMT
Author: ppoddar
Date: Wed Sep 12 01:28:31 2012
New Revision: 1383729

URL: http://svn.apache.org/viewvc?rev=1383729&view=rev
Log:
OPENJPA-2253 Add few Audit test

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestAudit.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestAudit.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestAudit.java?rev=1383729&r1=1383728&r2=1383729&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestAudit.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestAudit.java
Wed Sep 12 01:28:31 2012
@@ -19,6 +19,8 @@
 package org.apache.openjpa.audit;
 
 
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
 import java.util.List;
 
 import javax.persistence.EntityManager;
@@ -132,6 +134,37 @@ public class TestAudit extends TestCase 
     	assertTrue(entry.getUpdatedFields().contains("price"));
     }
     
+    public void testAuditDoesNotLeakMemory() {
+    	int N = 1000;
+    	EntityManager em = emf.createEntityManager();
+	   	long m2 = insert(N, em);
+    	em = Persistence.createEntityManagerFactory("no-audit").createEntityManager();
+    	assertNull(OpenJPAPersistence.cast(em).getEntityManagerFactory().getConfiguration().getAuditorInstance());
+		long m0 = insert(N, em);
+    	System.err.println("Memory used with no auditor " + m0);
+    	System.err.println("Memory used with auditor " + m2);
+		double pct = 100.0*(m2-m0)/m0;
+		System.err.println("Extra memory with auditor " + pct);
+    	assertTrue(pct < 10.0);
+    }
+    
+    private long insert(int N, EntityManager em) {
+    	assertTrue(ensureGarbageCollection());
+    	long m1 = Runtime.getRuntime().freeMemory();
+    	em.getTransaction().begin();
+    	for (int i = 0; i < N; i++) {
+    		X x = new X();
+    		x.setName("X"+System.currentTimeMillis());
+    		em.persist(x);
+    	}
+    	em.getTransaction().commit();
+    	assertTrue(ensureGarbageCollection());
+    	long m2 = Runtime.getRuntime().freeMemory();
+    	long mused = m1-m2;
+    	
+    	return mused;
+    }
+    
     /**
      * Finds the latest audit entry of the given operation type.
      * The <em>latest</em> is determined by a sort on identifier which is assumed
to be monotonically ascending.
@@ -143,6 +176,21 @@ public class TestAudit extends TestCase 
                 .setMaxResults(1).setParameter("op", op).getResultList();
         return entry.get(0);
     }
+    
+	public boolean ensureGarbageCollection() {
+		ReferenceQueue<Object> detector = new ReferenceQueue<Object>();
+		Object marker = new Object();
+		WeakReference<Object> ref = new WeakReference<Object>(marker, detector);
+		marker = null;
+		System.gc();
+		try {
+			return detector.remove() == ref;
+		} catch (InterruptedException e) {
+			
+		}
+		return false;
+	}
+
 
 
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java?rev=1383729&r1=1383728&r2=1383729&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java Wed
Sep 12 01:28:31 2012
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.audit;
 
+import java.util.concurrent.atomic.AtomicLong;
+
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
@@ -34,18 +36,25 @@ import javax.persistence.Id;
 @Auditable
 public class X {
 	@Id
-	@GeneratedValue
 	private long id;
 	
 	private String name;
 	private int price;
 	
+	private static AtomicLong ID_GENERATOR = new AtomicLong(System.currentTimeMillis());
+	
+	public X() {
+		id = ID_GENERATOR.getAndIncrement();
+	}
+	
 	public String getName() {
 		return name;
 	}
+	
 	public void setName(String name) {
 		this.name = name;
 	}
+	
 	public int getPrice() {
 		return price;
 	}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml?rev=1383729&r1=1383728&r2=1383729&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml Wed
Sep 12 01:28:31 2012
@@ -452,6 +452,15 @@
 			<property name="openjpa.DynamicEnhancementAgent" value="false" />
 		</properties>
 	</persistence-unit>
+	<persistence-unit name="no-audit">
+		<class>org.apache.openjpa.audit.X</class>
+		<class>org.apache.openjpa.audit.AuditedEntry</class>
+
+		<properties>
+			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"
/>
+			<property name="openjpa.DynamicEnhancementAgent" value="false" />
+		</properties>
+	</persistence-unit>
 
 	<persistence-unit name="query-result">
 		<mapping-file>META-INF/query-result-orm.xml</mapping-file>



Mime
View raw message