You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/02/04 01:25:27 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/broker MultithreadedReadTest.java

arminw      2004/02/03 16:25:27

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb repository_junit_reference.xml
  Added:       src/test/org/apache/ojb/broker MultithreadedReadTest.java
  Log:
  - add new test case to show the concurrency read
  materialization problem when using ObjectCacheDefaultImpl
  
  Revision  Changes    Path
  1.62      +27 -0     db-ojb/src/schema/ojbtest-schema.xml
  
  Index: ojbtest-schema.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
  retrieving revision 1.61
  retrieving revision 1.62
  diff -u -r1.61 -r1.62
  --- ojbtest-schema.xml	28 Jan 2004 23:55:30 -0000	1.61
  +++ ojbtest-schema.xml	4 Feb 2004 00:25:27 -0000	1.62
  @@ -741,6 +741,33 @@
       </table>
   
   
  +    <table name="READTEST_ACCOUNT">
  +        <column name="OJB_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="FK_REF" type="INTEGER"/>
  +    </table>
  +
  +    <table name="READTEST_BUYER">
  +        <column name="OJB_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="FK_REF" type="INTEGER"/>
  +    </table>
  +
  +    <table name="READTEST_ADDRESS">
  +        <column name="OJB_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="FK_REF" type="INTEGER"/>
  +    </table>
  +
  +    <table name="READTEST_ADDRESS_TYPE">
  +        <column name="OJB_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <column name="FK_REF" type="INTEGER"/>
  +    </table>
  +
  +
  +
  +
   
       <!-- =================================================== -->
       <!-- ODMG/PB collection test tables                         -->
  
  
  
  1.6       +154 -2    db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml
  
  Index: repository_junit_reference.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- repository_junit_reference.xml	29 Jan 2004 12:34:51 -0000	1.5
  +++ repository_junit_reference.xml	4 Feb 2004 00:25:27 -0000	1.6
  @@ -429,7 +429,6 @@
   
   
   
  -
   <!-- ************************************************* -->
   <!--      PB collection test objects                   -->
   <!-- ************************************************* -->
  @@ -910,5 +909,158 @@
          </reference-descriptor>
   
       </class-descriptor>
  +
  +
  +    <!-- ************************************************* -->
  +    <!--      broker.MultithreadedReadTest                 -->
  +    <!-- ************************************************* -->
  +
  +    <class-descriptor class="org.apache.ojb.broker.MultithreadedReadTest$Account">
  +        <extent-class class-ref="org.apache.ojb.broker.MultithreadedReadTest$AccountImpl"/>
  +    </class-descriptor>
  +
  +    <class-descriptor class="org.apache.ojb.broker.MultithreadedReadTest$Address">
  +        <extent-class class-ref="org.apache.ojb.broker.MultithreadedReadTest$AddressImpl"/>
  +    </class-descriptor>
  +
  +    <class-descriptor class="org.apache.ojb.broker.MultithreadedReadTest$Buyer">
  +        <extent-class class-ref="org.apache.ojb.broker.MultithreadedReadTest$BuyerImpl"/>
  +    </class-descriptor>
  +
  +    <class-descriptor class="org.apache.ojb.broker.MultithreadedReadTest$AddressType">
  +        <extent-class class-ref="org.apache.ojb.broker.MultithreadedReadTest$AddressTypeImpl"/>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +       class="org.apache.ojb.broker.MultithreadedReadTest$AccountImpl"
  +       table="READTEST_ACCOUNT"
  +    >
  +       <field-descriptor
  +          name="id"
  +          column="OJB_ID"
  +          jdbc-type="INTEGER"
  +          primarykey="true"
  +          autoincrement="true"
  +       />
  +
  +      <field-descriptor
  +          name="name"
  +          column="NAME"
  +          jdbc-type="VARCHAR"
  +       />
  +
  +        <field-descriptor
  +          name="fk"
  +          column="FK_REF"
  +          jdbc-type="INTEGER"
  +          access="anonymous"
  +       />
  +
  +       <reference-descriptor
  +          name="buyer"
  +          class-ref="org.apache.ojb.broker.MultithreadedReadTest$Buyer"
  +          auto-retrieve="true"
  +          auto-update="true"
  +          auto-delete="false"
  +          proxy="true"
  +       >
  +          <foreignkey field-ref="fk"/>
  +       </reference-descriptor>
  +
  +    </class-descriptor>
  +
  +    <class-descriptor
  +       class="org.apache.ojb.broker.MultithreadedReadTest$BuyerImpl"
  +       table="READTEST_BUYER"
  +    >
  +       <field-descriptor
  +          name="id"
  +          column="OJB_ID"
  +          jdbc-type="INTEGER"
  +          primarykey="true"
  +          autoincrement="true"
  +       />
  +
  +      <field-descriptor
  +          name="name"
  +          column="NAME"
  +          jdbc-type="VARCHAR"
  +       />
  +
  +        <field-descriptor
  +          name="fk"
  +          column="FK_REF"
  +          jdbc-type="INTEGER"
  +          access="anonymous"
  +       />
  +
  +       <reference-descriptor
  +          name="address"
  +          class-ref="org.apache.ojb.broker.MultithreadedReadTest$Address"
  +          auto-retrieve="true"
  +          auto-update="true"
  +          auto-delete="false"
  +          proxy="true"
  +       >
  +          <foreignkey field-ref="fk"/>
  +       </reference-descriptor>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +       class="org.apache.ojb.broker.MultithreadedReadTest$AddressImpl"
  +       table="READTEST_ADDRESS"
  +    >
  +       <field-descriptor
  +          name="id"
  +          column="OJB_ID"
  +          jdbc-type="INTEGER"
  +          primarykey="true"
  +          autoincrement="true"
  +       />
  +
  +      <field-descriptor
  +          name="name"
  +          column="NAME"
  +          jdbc-type="VARCHAR"
  +       />
  +
  +        <field-descriptor
  +          name="fk"
  +          column="FK_REF"
  +          jdbc-type="INTEGER"
  +          access="anonymous"
  +       />
  +
  +       <reference-descriptor
  +          name="type"
  +          class-ref="org.apache.ojb.broker.MultithreadedReadTest$AddressType"
  +          auto-retrieve="true"
  +          auto-update="true"
  +          auto-delete="false"
  +          proxy="true"
  +       >
  +          <foreignkey field-ref="fk"/>
  +       </reference-descriptor>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +       class="org.apache.ojb.broker.MultithreadedReadTest$AddressTypeImpl"
  +       table="READTEST_ADDRESS_TYPE"
  +    >
  +       <field-descriptor
  +          name="id"
  +          column="OJB_ID"
  +          jdbc-type="INTEGER"
  +          primarykey="true"
  +          autoincrement="true"
  +       />
  +
  +      <field-descriptor
  +          name="name"
  +          column="NAME"
  +          jdbc-type="VARCHAR"
  +       />
  +    </class-descriptor>
  +
   
   <!-- Mapping of classes used in junit tests and tutorials ends here -->
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/MultithreadedReadTest.java
  
  Index: MultithreadedReadTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import java.util.Iterator;
  import java.util.Collection;
  import java.util.List;
  import java.util.ArrayList;
  
  import org.apache.ojb.broker.query.QueryByCriteria;
  import org.apache.ojb.broker.query.Criteria;
  
  /**
   * Tests multithreaded read of objects using proxy for nested 1:1 references
   * Account --> Buyer --> Address --> AddressType
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: MultithreadedReadTest.java,v 1.1 2004/02/04 00:25:27 arminw Exp $
   */
  public class MultithreadedReadTest extends JUnitExtensions.MultiThreadedTestCase
  {
      int loops = 2;
      int concurrentThreads = 19;
      int numberOfObjects = 30;
  
      public MultithreadedReadTest(String s)
      {
          super(s);
      }
  
      public static void main(String[] args)
      {
          String[] arr = {MultithreadedReadTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      protected void tearDown() throws Exception
      {
          super.tearDown();
      }
  
      /**
       * Read objects using lazy materialization for references from DB. Different threads
       * call the references on the read objects
       */
      public void testObjectMaterializationByDifferentThread() throws Exception
      {
          for (int k = 0; k < loops; k++)
          {
              String searchCriteria = "testObjectMaterializationByDifferentThread_" + System.currentTimeMillis();
              prepareTestRead(searchCriteria, concurrentThreads);
  
              Collection accounts;
              PersistenceBroker broker = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  Criteria crit = new Criteria();
                  crit.addEqualTo("name", searchCriteria);
                  QueryByCriteria query = new QueryByCriteria(Account.class, crit);
                  accounts = broker.getCollectionByQuery(query);
                  assertEquals(concurrentThreads, accounts.size());
              }
              finally
              {
                  if (broker != null) broker.close();
              }
              Iterator iter = accounts.iterator();
              TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads];
              for (int i = 0; i < concurrentThreads; i++)
              {
                  tct[i] = new TestHandleMaterialize((Account) iter.next(), searchCriteria);
              }
              // run test classes
              runTestCaseRunnables(tct);
          }
      }
  
      /**
       * Different threads try to materialize the same bunch of objects.
       */
      public void testMultithreadedRead() throws Exception
      {
          String searchCriteria = "testMultithreadedRead_" + System.currentTimeMillis();
          prepareTestRead(searchCriteria, numberOfObjects);
          System.out.println("Multithreaded read of objects - start");
          System.out.println("" + concurrentThreads + " concurrent threads read "
                  + numberOfObjects + " objects per thread, loop " + loops + " times");
          for (int k = 0; k < loops; k++)
          {
              PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
              broker.clearCache();
              broker.close();
              TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads];
              for (int i = 0; i < concurrentThreads; i++)
              {
                  tct[i] = new TestHandleRead(searchCriteria);
              }
              // run test classes
              runTestCaseRunnables(tct);
          }
          System.out.println();
          System.out.println("Multithreaded read of objects - end");
      }
  
      /**
       * Different threads try to materialize the same bunch of objects
       */
      public void testMultithreadedLazyRead() throws Exception
      {
          String name = "testMultithreadedLazyRead" + System.currentTimeMillis();
          List identityList = prepareTestLazyRead(name, concurrentThreads);
          System.out.println("Multithreaded lazy read of objects - start");
          System.out.println("" + concurrentThreads + " concurrent threads read different object with lazy" +
                  " materialization reference, loop " + loops + " times");
          for (int k = 0; k < loops; k++)
          {
              PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
              broker.clearCache();
              broker.close();
              TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads];
              for (int i = 0; i < concurrentThreads; i++)
              {
                  tct[i] = new TestHandleLazyRead(identityList, name);
              }
              // run test classes
              runTestCaseRunnables(tct);
          }
          System.out.println();
          System.out.println("Multithreaded lazy read of objects - end");
      }
  
      private void prepareTestRead(String searchCriteria, int numbers) throws Exception
      {
          PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
          try
          {
              broker.beginTransaction();
              for (int i = 0; i < numbers; i++)
              {
                  AddressType type = new AddressTypeImpl(searchCriteria);
                  Address address = new AddressImpl(searchCriteria, type);
                  Buyer buyer = new BuyerImpl(searchCriteria, address);
                  Account account = new AccountImpl(searchCriteria, buyer);
                  broker.store(account);
              }
              broker.commitTransaction();
              broker.clearCache();
          }
          finally
          {
              if (broker != null) broker.close();
          }
      }
  
      private List prepareTestLazyRead(String searchCriteria, int numbers) throws Exception
      {
          List result = new ArrayList();
          PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
          try
          {
              broker.beginTransaction();
              for (int i = 0; i < numbers; i++)
              {
                  AddressType type = new AddressTypeImpl(searchCriteria);
                  Address address = new AddressImpl(searchCriteria, type);
                  Buyer buyer = new BuyerImpl(searchCriteria, address);
                  Account account = new AccountImpl(searchCriteria, buyer);
                  broker.store(account);
                  Identity oid = new Identity(account, broker);
                  result.add(oid);
              }
              broker.commitTransaction();
              broker.clearCache();
          }
          finally
          {
              if (broker != null) broker.close();
          }
          return result;
      }
  
  
      //***********************************************
      // test handle of multithreaded test
      //***********************************************
      class TestHandleRead extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
      {
          String searchCriteria;
  
          public TestHandleRead(String searchCriteria)
          {
              this.searchCriteria = searchCriteria;
          }
  
          public void runTestCase() throws Throwable
          {
              readByCollection();
              readByIterator();
          }
  
          private void readByCollection() throws Exception
          {
              PersistenceBroker broker = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  broker.clearCache();
                  Criteria crit = new Criteria();
                  crit.addEqualTo("name", searchCriteria);
                  QueryByCriteria query = new QueryByCriteria(Account.class, crit);
                  Collection accounts = broker.getCollectionByQuery(query);
                  assertEquals("Wrong number of expected objects", numberOfObjects, accounts.size());
                  for (Iterator iter = accounts.iterator(); iter.hasNext();)
                  {
                      Account account = (Account) iter.next();
                      assertEquals(searchCriteria, account.getName());
                      assertNotNull("All accounts have a reference to an Buyer", account.getBuyer());
                      assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress());
                      assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType());
                      assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName());
                      // System.out.println(""+Thread.currentThread().toString()+": passed");
                  }
              }
              finally
              {
                  if (broker != null) broker.close();
              }
          }
  
          private void readByIterator() throws Exception
          {
              PersistenceBroker broker = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  broker.clearCache();
                  Criteria crit = new Criteria();
                  crit.addEqualTo("name", searchCriteria);
                  QueryByCriteria query = new QueryByCriteria(Account.class, crit);
                  Iterator iter = broker.getIteratorByQuery(query);
                  for (; iter.hasNext();)
                  {
                      Account account = (Account) iter.next();
                      assertEquals(searchCriteria, account.getName());
                      assertNotNull("All accounts have a reference to an Buyer", account.getBuyer());
                      assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress());
                      assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType());
                      assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName());
                      // System.out.println(""+Thread.currentThread().toString()+": passed");
                  }
              }
              finally
              {
                  if (broker != null) broker.close();
              }
          }
      }
  
      class TestHandleLazyRead extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
      {
          List identityList;
          String name;
  
          public TestHandleLazyRead(List identityList, String name)
          {
              this.identityList = identityList;
              this.name = name;
          }
  
          public void runTestCase() throws Throwable
          {
              PersistenceBroker broker = null;
              Account account = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  Iterator it = identityList.iterator();
                  while (it.hasNext())
                  {
                      Identity oid = (Identity) it.next();
                      account = (Account) broker.getObjectByIdentity(oid);
                  }
              }
              finally
              {
                  if (broker != null) broker.close();
              }
              assertEquals(name, account.getName());
              assertNotNull("All accounts have a reference to an Buyer", account.getBuyer());
              assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress());
              assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType());
              assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName());
          }
      }
  
  
      class TestHandleMaterialize extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
      {
          Account account;
          String name;
  
          public TestHandleMaterialize(Account account, String name)
          {
              this.account = account;
              this.name = name;
          }
  
          public void runTestCase() throws Throwable
          {
              assertEquals(name, account.getName());
              assertNotNull("All accounts have a reference to an Buyer", account.getBuyer());
              assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress());
              assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType());
              assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName());
          }
      }
  
  
      //***********************************************
      // test classes/interfaces starts here
      //***********************************************
      public interface Account extends Base
      {
          Buyer getBuyer();
  
          void setBuyer(Buyer buyer);
      }
  
      public static class AccountImpl extends BaseImpl implements Account
      {
          Buyer buyer;
  
          public AccountImpl(String name, Buyer buyer)
          {
              super(name);
              this.buyer = buyer;
          }
  
          public AccountImpl(Buyer buyer)
          {
              this.buyer = buyer;
          }
  
          public AccountImpl()
          {
  
          }
  
          public Buyer getBuyer()
          {
              return buyer;
          }
  
          public void setBuyer(Buyer buyer)
          {
              this.buyer = buyer;
          }
      }
  
      public interface Buyer extends Base
      {
          Address getAddress();
  
          void setAddress(Address address);
      }
  
      public static class BuyerImpl extends BaseImpl implements Buyer
      {
          Address address;
  
          public BuyerImpl(String name, Address address)
          {
              super(name);
              this.address = address;
          }
  
          public BuyerImpl(Address address)
          {
              this.address = address;
          }
  
          public BuyerImpl()
          {
  
          }
  
          public Address getAddress()
          {
              return address;
          }
  
          public void setAddress(Address address)
          {
              this.address = address;
          }
      }
  
      public interface Address extends Base
      {
          AddressType getType();
  
          void setType(AddressType type);
      }
  
      public static class AddressImpl extends BaseImpl implements Address
      {
          AddressType type;
  
          public AddressImpl(String name, AddressType type)
          {
              super(name);
              this.type = type;
          }
  
          public AddressImpl(AddressType type)
          {
              this.type = type;
          }
  
          public AddressImpl()
          {
  
          }
  
          public AddressType getType()
          {
              return type;
          }
  
          public void setType(AddressType type)
          {
              this.type = type;
          }
      }
  
      public interface AddressType extends Base
      {
      }
  
      public static class AddressTypeImpl extends BaseImpl implements AddressType
      {
          public AddressTypeImpl(String name)
          {
              super(name);
          }
  
          public AddressTypeImpl()
          {
          }
      }
  
      public interface Base
      {
          Integer getId();
  
          void setId(Integer id);
  
          String getName();
  
          void setName(String name);
      }
  
      public static class BaseImpl
      {
          Integer id;
          String name;
  
          public BaseImpl(String name)
          {
              this.name = name;
          }
  
          public BaseImpl()
          {
          }
  
          public Integer getId()
          {
              return id;
          }
  
          public void setId(Integer id)
          {
              this.id = id;
          }
  
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
      }
  }
  
  
  

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