[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/pojo/optimistic ...

Jason Thomas Greene jgreene at jboss.com
Mon Apr 23 17:19:17 EDT 2007


  User: jgreene 
  Date: 07/04/23 17:19:17

  Modified:    tests/functional/org/jboss/cache/pojo/optimistic 
                        LocalTxTest.java
  Log:
  Switch to using latches
  Add plain cache scenario that demonstrates the perceived failure
  
  Revision  Changes    Path
  1.2       +148 -18   JBossCache/tests/functional/org/jboss/cache/pojo/optimistic/LocalTxTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: LocalTxTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/pojo/optimistic/LocalTxTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- LocalTxTest.java	13 Jan 2007 15:55:11 -0000	1.1
  +++ LocalTxTest.java	23 Apr 2007 21:19:17 -0000	1.2
  @@ -22,12 +22,29 @@
   
   package org.jboss.cache.pojo.optimistic;
   
  +import java.util.ArrayList;
  +import java.util.List;
  +import java.util.Properties;
  +import java.util.concurrent.CountDownLatch;
  +
  +import javax.naming.Context;
  +import javax.naming.InitialContext;
  +import javax.naming.NamingException;
  +import javax.transaction.NotSupportedException;
  +import javax.transaction.RollbackException;
  +import javax.transaction.SystemException;
  +import javax.transaction.Transaction;
  +import javax.transaction.UserTransaction;
  +
   import junit.framework.Test;
   import junit.framework.TestSuite;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.Cache;
   import org.jboss.cache.CacheSPI;
   import org.jboss.cache.Fqn;
  +import org.jboss.cache.Node;
   import org.jboss.cache.pojo.PojoCache;
   import org.jboss.cache.pojo.TestingUtil;
   import org.jboss.cache.pojo.test.Address;
  @@ -35,18 +52,6 @@
   import org.jboss.cache.pojo.util.CacheApiUtil;
   import org.jboss.cache.transaction.DummyTransactionManager;
   
  -import javax.naming.Context;
  -import javax.naming.InitialContext;
  -import javax.naming.NamingException;
  -import javax.transaction.NotSupportedException;
  -import javax.transaction.RollbackException;
  -import javax.transaction.SystemException;
  -import javax.transaction.Transaction;
  -import javax.transaction.UserTransaction;
  -import java.util.ArrayList;
  -import java.util.List;
  -import java.util.Properties;
  -
   /**
    */
   public class LocalTxTest extends AbstractOptimisticTestCase
  @@ -198,6 +203,8 @@
   
      public void testConcurrentSimplePutsI() throws Exception
      {
  +      final CountDownLatch latch1 = new CountDownLatch(1);
  +      final CountDownLatch latch2 = new CountDownLatch(1);
         Thread t1 = new Thread("t1")
         {
            Transaction tx;
  @@ -216,7 +223,8 @@
                  UserTransaction tx = getTransaction();
                  tx.begin();
                  p.setAddress(addr);
  -               TestingUtil.sleepThread(17000);
  +               latch1.countDown();
  +               latch2.await();
                  tx.commit();
               }
               catch (RollbackException rollback)
  @@ -227,6 +235,10 @@
               {
                  t1_ex = ex;
               }
  +            finally
  +            {
  +               latch1.countDown();
  +            }
            }
         };
   
  @@ -239,7 +251,7 @@
               UserTransaction tx = null;
               try
               {
  -               TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
  +               latch1.await();
                  Person p = createPerson("/person/test7", "p7", 40);
                  Address addr = new Address();
                  addr.setCity("Santa Clara");
  @@ -265,6 +277,10 @@
                     t2_ex = e;
                  }
               }
  +            finally
  +            {
  +               latch2.countDown();
  +            }
            }
         };
   
  @@ -286,6 +302,8 @@
   
      public void testConcurrentSimplePutsII() throws Exception
      {
  +      final CountDownLatch latch1 = new CountDownLatch(1);
  +      final CountDownLatch latch2 = new CountDownLatch(1);
         Thread t1 = new Thread("t1")
         {
            Transaction tx;
  @@ -300,7 +318,8 @@
                  UserTransaction tx = getTransaction();
                  tx.begin();
                  p.setAddress(addr);
  -               TestingUtil.sleepThread(17000);
  +               latch1.countDown();
  +               latch2.await();
                  tx.commit();
               }
               catch (RollbackException rollback)
  @@ -311,6 +330,10 @@
               {
                  t1_ex = ex;
               }
  +            finally
  +            {
  +               latch1.countDown();
  +            }
            }
         };
   
  @@ -323,7 +346,7 @@
               UserTransaction tx = null;
               try
               {
  -               TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
  +               latch1.await();
                  Person p = (Person) cache.find("/person/test6");
                  Address addr = new Address();
                  addr.setCity("Santa Clara");
  @@ -349,6 +372,10 @@
                     t2_ex = e;
                  }
               }
  +            finally
  +            {
  +               latch2.countDown();
  +            }
            }
         };
   
  @@ -374,6 +401,8 @@
   
      public void testConcurrentPuts() throws Exception
      {
  +      final CountDownLatch latch1 = new CountDownLatch(1);
  +      final CountDownLatch latch2 = new CountDownLatch(1);
         Thread t1 = new Thread("t1")
         {
            Transaction tx;
  @@ -386,7 +415,8 @@
                  UserTransaction tx = getTransaction();
                  tx.begin();
                  lang.add("German");
  -               TestingUtil.sleepThread(17000);
  +               latch1.countDown();
  +               latch2.await();
                  tx.commit();
               }
               catch (RollbackException rollback)
  @@ -397,6 +427,10 @@
               {
                  t1_ex = ex;
               }
  +            finally
  +            {
  +               latch1.countDown();
  +            }
            }
         };
   
  @@ -409,7 +443,7 @@
               UserTransaction tx = null;
               try
               {
  -               TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
  +               latch1.await();
                  List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                  tx = getTransaction();
                  tx.begin();
  @@ -432,6 +466,10 @@
                     t2_ex = e;
                  }
               }
  +            finally
  +            {
  +               latch2.countDown();
  +            }
            }
         };
   
  @@ -457,6 +495,98 @@
                 2, size);
      }
   
  +   public void testConcurrentPlainPuts() throws Exception
  +   {
  +      final CountDownLatch latch1 = new CountDownLatch(1);
  +      final CountDownLatch latch2 = new CountDownLatch(1);
  +      final Cache coreCache = cache.getCache();
  +      
  +      Thread t1 = new Thread("t1")
  +      {
  +         Transaction tx;
  +
  +         public void run()
  +         {
  +            try
  +            {
  +               Node child = coreCache.getRoot().getChild(Fqn.fromString("/testing/root"));
  +               UserTransaction tx = getTransaction();
  +               tx.begin();
  +               child.addChild(Fqn.fromString("node1"));
  +               latch1.countDown();
  +               latch2.await();
  +               tx.commit();
  +            }
  +            catch (RollbackException rollback)
  +            {
  +               ;
  +            }
  +            catch (Exception ex)
  +            {
  +               t1_ex = ex;
  +            }
  +            finally
  +            {
  +               latch1.countDown();
  +            }
  +         }
  +      };
  +
  +      Thread t2 = new Thread("t2")
  +      {
  +         Transaction tx;
  +
  +         public void run()
  +         {
  +            UserTransaction tx = null;
  +            try
  +            {
  +               latch1.await();
  +               Node child = coreCache.getRoot().getChild(Fqn.fromString("/testing/root"));
  +               tx = getTransaction();
  +               tx.begin();
  +               child.addChild(Fqn.fromString("node2"));
  +               tx.commit();
  +            }
  +            catch (RollbackException rollback)
  +            {
  +               ;
  +            }
  +            catch (Exception ex)
  +            {
  +               try
  +               {
  +                  tx.rollback();
  +               }
  +               catch (SystemException e)
  +               {
  +                  e.printStackTrace();
  +                  t2_ex = e;
  +               }
  +            }
  +            finally 
  +            {
  +               latch2.countDown();
  +            }
  +         }
  +      };
  +      
  +      Node child = coreCache.getRoot().addChild(Fqn.fromString("/testing/root"));
  +      t1.start();
  +      t2.start();
  +
  +      t1.join();
  +      t2.join();
  +
  +      // t2 should rollback due to timeout while t2 should succeed
  +      if (t2_ex != null)
  +         fail("Thread1 failed: " + t2_ex);
  +      if (t1_ex != null)
  +         fail("Thread2 failed: " + t1_ex);
  +
  +      assertEquals("There should be one entry", 1, child.getChildren().size());
  +   }
  +
      void log(String s)
      {
         long now;
  
  
  



More information about the jboss-cvs-commits mailing list