mirror of https://github.com/apache/openjpa.git
OPENJPA-2253 Add few Audit test
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1383729 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ae3441b049
commit
0ae1199283
|
@ -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.
|
||||
|
@ -144,5 +177,20 @@ public class TestAudit extends TestCase {
|
|||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue