db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker/metadata MetadataMultithreadedTest.java
Date Sat, 03 Jan 2004 23:03:24 GMT
arminw      2004/01/03 15:03:24

  Modified:    src/test/org/apache/ojb/broker/metadata
                        MetadataMultithreadedTest.java
  Log:
  update test case to detect memory leaks when enable
  per thread metadata changes in MetadataManager
  
  Revision  Changes    Path
  1.5       +92 -13    db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java
  
  Index: MetadataMultithreadedTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MetadataMultithreadedTest.java	19 Dec 2003 16:34:32 -0000	1.4
  +++ MetadataMultithreadedTest.java	3 Jan 2004 23:03:24 -0000	1.5
  @@ -1,16 +1,20 @@
   package org.apache.ojb.broker.metadata;
   
  +import java.util.ArrayList;
  +import java.util.List;
  +
  +import org.apache.commons.lang.ClassUtils;
   import org.apache.ojb.broker.JUnitExtensions;
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
  -import org.apache.ojb.broker.util.ClassHelper;
  -import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.Project;
  +import org.apache.ojb.broker.Person;
  +import org.apache.ojb.broker.Role;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.sequence.Repository;
  -import org.apache.ojb.odmg.PersonImpl;
  -import org.apache.commons.lang.ClassUtils;
  +import org.apache.ojb.broker.util.ClassHelper;
   
   /**
    *
  @@ -55,8 +59,12 @@
   
       public void testRuntimeMetadataChanges() throws Exception
       {
  -        int loops = 5;
  -        int threads = 5;
  +        int loops = 7;
  +        int threads = 8;
  +        // need min 80% free memory after test campared with
  +        // beginning, else test fails
  +        int minimalFreeMemAfterTest = 80;
  +
           PersistenceBroker broker = null;
           try
           {
  @@ -67,7 +75,8 @@
               mm.setDescriptor(defaultRepository);
   
               ClassDescriptor cld;
  -
  +            long memoryUseBeforeTest;
  +            long memoryUseAfterTest;
               try
               {
                   // prepare for test
  @@ -114,8 +123,12 @@
                   Runtime.getRuntime().gc();
                   Thread.sleep(200);
                   Runtime.getRuntime().gc();
  -                System.out.println("Free/total Memory after test after gc:   "
  -                        + convertToMB(Runtime.getRuntime().freeMemory())
  +                Runtime.getRuntime().gc();
  +
  +                memoryUseBeforeTest = convertToMB(memory);
  +                memoryUseAfterTest = convertToMB(Runtime.getRuntime().freeMemory());
  +                System.out.println("Free/total Memory after test and gc:   "
  +                        + memoryUseAfterTest
                           + "/" + convertToMB(Runtime.getRuntime().maxMemory()) + "MB");
                   System.out.println("Do cleanup now ...");
               }
  @@ -123,12 +136,22 @@
               {
                   MetadataManager.getInstance().setEnablePerThreadChanges(false);
               }
  -
  +            // get new PB instance
               broker = PersistenceBrokerFactory.defaultPersistenceBroker();
               cld = broker.getClassDescriptor(targetTestClass);
               String name = cld.getFullTableName();
               assertEquals(oldTestObjectString, name);
               assertFalse(MetadataManager.getInstance().isEnablePerThreadChanges());
  +            double d = ((double) memoryUseAfterTest) / ((double) memoryUseBeforeTest);
  +            int result = (int) (d * 100);
  +            if (result < minimalFreeMemAfterTest)
  +            {
  +                fail("** When using a offical version of OJB, ignore this failure! **"
+
  +                        " Memory usage after this test differs more than "+(100 - minimalFreeMemAfterTest)
  +                        +"% from beginning, this may indicate" +
  +                        " a memory leak (GC can't free unused metadata objects), but this
could also be a result" +
  +                        " of your JVM settings. Please re-run test.");
  +            }
           }
           finally
           {
  @@ -147,6 +170,7 @@
       class ThreadedUsingBroker extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
       {
           int loops;
  +        String title = "ThreadedUsingBroker_" + System.currentTimeMillis();
   
           public ThreadedUsingBroker()
           {
  @@ -192,17 +216,59 @@
                       String name = cld.getFullTableName();
                       assertEquals(newTestObjectString, name);
                       assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges());
  -                    // query the object
  -                    Query query = new QueryByCriteria(PersonImpl.class, (Criteria) null,
true);
  +
  +                    // query a test object
  +                    Query query = new QueryByCriteria(Person.class, null, true);
                       broker.getCollectionByQuery(query);
                       // store target object
  +                    /*
  +                    store some complex objects to check if references to
  +                    metadata classes are cached
  +                    */
  +                    Project project = new Project();
  +                    project.setTitle(title);
  +
  +                    Person p1 = new Person();
  +                    p1.setFirstname(title);
  +                    List l1 = new ArrayList();
  +                    l1.add(project);
  +                    p1.setProjects(l1);
  +
  +                    Person p2 = new Person();
  +                    p2.setFirstname(title);
  +                    List l2 = new ArrayList();
  +                    l2.add(project);
  +                    p2.setProjects(l2);
  +
  +                    Role r1 = new Role();
  +                    r1.setPerson(p1);
  +                    r1.setRoleName(title);
  +                    r1.setProject(project);
  +                    List roles1 = new ArrayList();
  +                    roles1.add(r1);
  +
  +                    Role r2 = new Role();
  +                    r2.setPerson(p2);
  +                    r2.setRoleName(title);
  +                    r2.setProject(project);
  +                    List roles2 = new ArrayList();
  +                    roles2.add(r2);
  +
  +                    p1.setRoles(roles1);
  +                    p2.setRoles(roles2);
  +
                       Object obj = ClassHelper.newInstance(targetTestClass);
  +
                       broker.beginTransaction();
                       broker.store(obj);
  +                    broker.store(p1);
  +                    broker.store(p2);
                       broker.commitTransaction();
                       // delete target object
                       broker.beginTransaction();
                       broker.delete(obj);
  +                    //broker.delete(p1);
  +                    //broker.delete(p2);
                       broker.commitTransaction();
                   }
                   finally
  @@ -266,6 +332,19 @@
                           // this PB instance use unchanged global metadata repository
                           assertEquals(getTestObjectString(), name);
                           // System.out.println("Default: found "+name);
  +
  +                        // query a test object
  +                        Query query = new QueryByCriteria(Person.class, null, true);
  +                        broker.getCollectionByQuery(query);
  +                        // store target object
  +                        Object obj = ClassHelper.newInstance(targetTestClass);
  +                        broker.beginTransaction();
  +                        broker.store(obj);
  +                        broker.commitTransaction();
  +                        // delete target object
  +                        broker.beginTransaction();
  +                        broker.delete(obj);
  +                        broker.commitTransaction();
                       }
                       finally
                       {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message