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

Manik Surtani manik at jboss.org
Fri Apr 20 09:27:09 EDT 2007


  User: msurtani
  Date: 07/04/20 09:27:09

  Modified:    tests/functional/org/jboss/cache/optimistic 
                        ConcurrentTransactionTest.java
  Log:
  added test for JBCACHE-1026
  
  Revision  Changes    Path
  1.7       +137 -19   JBossCache/tests/functional/org/jboss/cache/optimistic/ConcurrentTransactionTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ConcurrentTransactionTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/optimistic/ConcurrentTransactionTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- ConcurrentTransactionTest.java	7 Feb 2007 22:06:50 -0000	1.6
  +++ ConcurrentTransactionTest.java	20 Apr 2007 13:27:09 -0000	1.7
  @@ -6,19 +6,22 @@
    */
   package org.jboss.cache.optimistic;
   
  -import org.jboss.cache.CacheImpl;
  +import org.jboss.cache.CacheSPI;
   import org.jboss.cache.Fqn;
   import org.jboss.cache.NodeSPI;
   
   import javax.transaction.Transaction;
   import javax.transaction.TransactionManager;
  +import java.util.LinkedList;
  +import java.util.List;
  +import java.util.concurrent.CountDownLatch;
   
   /**
    * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
    */
   public class ConcurrentTransactionTest extends AbstractOptimisticTestCase
   {
  -   private CacheImpl cache;
  +   private CacheSPI cache;
   
      public ConcurrentTransactionTest(String name)
      {
  @@ -29,7 +32,9 @@
      {
         try
         {
  -         cache = createCache();
  +         cache = createCacheUnstarted();
  +         cache.getConfiguration().setUseRegionBasedMarshalling(true);
  +         cache.start();
         }
         catch (Exception e)
         {
  @@ -48,34 +53,147 @@
   
      public void testConcurrentTransactions() throws Exception
      {
  -      TransactionManager tm = cache.getTransactionManager();
  -      cache.put("/a/b/c/d", key, value);
  +      TransactionManager tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
  +      Fqn abcd = Fqn.fromString("/a/b/c/d");
  +      Fqn abce = Fqn.fromString("/a/b/c/e");
  +      Fqn abcf = Fqn.fromString("/a/b/c/f");
  +      Fqn abcg = Fqn.fromString("/a/b/c/g");
  +      Fqn abxy = Fqn.fromString("/a/b/x/y");
  +      cache.put(abcd, key, value);
         
  -      assertEquals(value, cache.get("/a/b/c/d", key));
  +      assertEquals(value, cache.get(abcd, key));
   
         tm.begin();
         Transaction tx = tm.getTransaction();
   
  -      cache.put("/a/b/x/y", key, value);
  +      cache.put(abxy, key, value);
         tm.suspend();
   
         // a number of random puts in unrelated sub nodes.
  -      cache.put("/a/b/c/d", key, value + value);
  -      cache.put("/a/b/c/e", key, value);
  -      cache.put("/a/b/c/f", key, value);
  -      cache.put("/a/b/c/g", key, value);
  -
  -      assertEquals(value + value, cache.get("/a/b/c/d", key));
  -      assertEquals(value, cache.get("/a/b/c/e", key));
  -      assertEquals(value, cache.get("/a/b/c/f", key));
  -      assertEquals(value, cache.get("/a/b/c/g", key));
  +      cache.put(abcd, key, value + value);
  +      cache.put(abce, key, value);
  +      cache.put(abcf, key, value);
  +      cache.put(abcg, key, value);
  +
  +      assertEquals(value + value, cache.get(abcd, key));
  +      assertEquals(value, cache.get(abce, key));
  +      assertEquals(value, cache.get(abcf, key));
  +      assertEquals(value, cache.get(abcg, key));
   
         tm.resume(tx);
         tx.commit();
   
  -      assertEquals(value, cache.get("/a/b/x/y", key));
  +      assertEquals(value, cache.get(abxy, key));
   
  -      NodeSPI n = (NodeSPI) cache.get(Fqn.ROOT);
  +      NodeSPI n = cache.getRoot();
         System.out.println(n.getVersion());
      }
  +
  +   public void testConcurrentCreationTestWithEmptyCache() throws Exception
  +   {
  +      doConcurrentCreationTest(false);
  +   }
  +
  +   public void testConcurrentCreationTestWithEmptyCacheActivated() throws Exception
  +   {
  +      cache.put(Fqn.fromString("/parent"), null);
  +      cache.getRegion(Fqn.fromString("/parent"), true).activate();
  +      assertNotNull(cache.peek(Fqn.fromString("/parent"), false));
  +      doConcurrentCreationTest(false);
  +   }
  +
  +   public void testConcurrentCreationTestWithPopulatedCache() throws Exception
  +   {
  +      doConcurrentCreationTest(true);
  +   }
  +
  +   public void testConcurrentReadAndRemove() throws Exception
  +   {
  +      final List exceptions = new LinkedList();
  +      final CountDownLatch readerLatch = new CountDownLatch(1);
  +      final CountDownLatch readerFinishedLatch = new CountDownLatch(1);
  +      final Fqn fqn = Fqn.fromString("/parent/child");
  +
  +      cache.put(fqn, "k", "v");
  +
  +      class Reader extends Thread
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               cache.getTransactionManager().begin();
  +               cache.get(fqn, "k"); // read
  +               readerFinishedLatch.countDown();
  +               readerLatch.await(); // wait
  +               cache.getTransactionManager().commit();
  +            }
  +            catch (Exception e)
  +            {
  +               e.printStackTrace();
  +               exceptions.add(e);
  +
  +            }
  +         }
  +      }
  +
  +      Thread reader = new Reader();
  +
  +      reader.start();
  +      readerFinishedLatch.await();
  +      cache.removeNode(fqn.getParent());
  +      assertNull(cache.peek(fqn.getParent(), false));
  +      readerLatch.countDown();
  +      reader.join();
  +
  +      assertTrue("Should not have caught any exceptions!!", exceptions.isEmpty());
  +   }
  +
  +
  +   private void doConcurrentCreationTest(boolean prepopulateParent) throws Exception
  +   {
  +      if (prepopulateParent) cache.put(Fqn.fromString("/parent/dummy"), "k", "v");
  +
  +      final List exceptions = new LinkedList();
  +      final CountDownLatch latch = new CountDownLatch(1);
  +
  +      class ConcurrentCreator extends Thread
  +      {
  +         private String name;
  +
  +         public ConcurrentCreator(String name)
  +         {
  +            this.name = name;
  +         }
  +
  +         public void run()
  +         {
  +            try
  +            {
  +               cache.getTransactionManager().begin();
  +               cache.put(Fqn.fromString("/parent/child" + name), "key", "value");
  +               latch.await();
  +               cache.getTransactionManager().commit();
  +            }
  +            catch (Exception e)
  +            {
  +               e.printStackTrace();
  +               exceptions.add(e);
  +            }
  +         }
  +      }
  +
  +      Thread one = new ConcurrentCreator("one");
  +      Thread two = new ConcurrentCreator("two");
  +
  +      one.start();
  +      two.start();
  +
  +      latch.countDown();
  +
  +      one.join();
  +      two.join();
  +
  +      assertTrue("Should not have caught any exceptions!!", exceptions.isEmpty());
  +   }
   }
  
  
  



More information about the jboss-cvs-commits mailing list