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

Manik Surtani msurtani at jboss.com
Thu Nov 23 12:57:10 EST 2006


  User: msurtani
  Date: 06/11/23 12:57:10

  Modified:    tests/functional/org/jboss/cache/transaction    Tag:
                        Branch_JBossCache_1_3_0 TransactionTest.java
                        IsolationLevelReadCommittedTest.java
  Added:       tests/functional/org/jboss/cache/transaction    Tag:
                        Branch_JBossCache_1_3_0
                        IsolationLevelSerializableTest.java
  Log:
  Updated tests
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.9.2.2   +41 -56    JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TransactionTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java,v
  retrieving revision 1.9.2.1
  retrieving revision 1.9.2.2
  diff -u -b -r1.9.2.1 -r1.9.2.2
  --- TransactionTest.java	23 Nov 2006 16:36:38 -0000	1.9.2.1
  +++ TransactionTest.java	23 Nov 2006 17:57:10 -0000	1.9.2.2
  @@ -27,7 +27,7 @@
    * Tests transactional access to a local TreeCache.
    * Note: we use DummpyTranasctionManager to replace jta
    *
  - * @version $Id: TransactionTest.java,v 1.9.2.1 2006/11/23 16:36:38 bela Exp $
  + * @version $Id: TransactionTest.java,v 1.9.2.2 2006/11/23 17:57:10 msurtani Exp $
    */
   public class TransactionTest extends TestCase {
      TreeCache cache=null;
  @@ -285,41 +285,29 @@
   
   
   
  -    public void testNodeCreation() {
  +    public void testNodeCreation() throws Exception {
         GlobalTransaction gtx;
  -      try {
            cache.put("/a/b", null);
            tx.begin();
            gtx=cache.getCurrentTransaction();
            cache.put("/a/b/c", null);
  -         checkLock(gtx, "/a", false);
  -         checkLock(gtx, "/a/b", true);
  -         checkLock(gtx, "/a/b/c", true);
  +      assertLocked(gtx, "/a", false);
  +      assertLocked(gtx, "/a/b", true);
  +      assertLocked(gtx, "/a/b/c", true);
            System.out.println("locks: " + cache.printLockInfo());
         }
  -      catch(Throwable t) {
  -         t.printStackTrace();
  -         fail(t.toString());
  -      }
  -   }
   
   
  -    public void testNodeCreation2() {
  +    public void testNodeCreation2() throws Exception {
         GlobalTransaction gtx;
  -      try {
            tx.begin();
            gtx=cache.getCurrentTransaction();
            cache.put("/a/b/c", null);
  -         checkLock(gtx, "/a", true);
  -         checkLock(gtx, "/a/b", true);
  -         checkLock(gtx, "/a/b/c", true);
  +      assertLocked(gtx, "/a", true);
  +      assertLocked(gtx, "/a/b", true);
  +      assertLocked(gtx, "/a/b/c", true);
            System.out.println("locks: " + cache.printLockInfo());
         }
  -      catch(Throwable t) {
  -         t.printStackTrace();
  -         fail(t.toString());
  -      }
  -   }
   
   
       public void testNodeRemoval() {
  @@ -329,9 +317,9 @@
            tx.begin();
            gtx=cache.getCurrentTransaction();
            cache.remove("/a/b/c", null);
  -         checkLock(gtx, "/a", false);
  -         checkLock(gtx, "/a/b", true);
  -         checkLock(gtx, "/a/b/c", true);
  +         assertLocked(gtx, "/a", false);
  +         assertLocked(gtx, "/a/b", true);
  +         assertLocked(gtx, "/a/b/c", true);
            System.out.println("locks: " + cache.printLockInfo());
         }
         catch(Throwable t) {
  @@ -350,30 +338,31 @@
            tx.begin();
            gtx=cache.getCurrentTransaction();
            cache.put("/a/b/c1", null);
  -         checkLock(gtx, "/a", false);
  -         checkLock(gtx, "/a/b", false);
  -         checkLock(gtx, "/a/b/c1", true);
  +         assertLocked(gtx, "/a", false);
  +         assertLocked(gtx, "/a/b", true);
  +         assertLocked(gtx, "/a/b/c1", true);
   
            cache.put("/a/b/c2", null);
  -         checkLock(gtx, "/a/b/c2", true);
  +         assertLocked(gtx, "/a/b", true);
  +         assertLocked(gtx, "/a/b/c2", true);
   
            cache.put("/a/b/c3", null);
            cache.put("/a/b/c1/one", null);
  -         checkLock(gtx, "/a/b/c1", true);
  -         checkLock(gtx, "/a/b/c1/one", true);
  +         assertLocked(gtx, "/a/b/c1", true);
  +         assertLocked(gtx, "/a/b/c1/one", true);
   
            cache.put("/a/b/c1/two", null);
            cache.put("/a/b/c1/one/1", null);
  -         checkLock(gtx, "/a/b/c1", true);
  -         checkLock(gtx, "/a/b/c1/one", true);
  -         checkLock(gtx, "/a/b/c1/one/1", true);
  +         assertLocked(gtx, "/a/b/c1", true);
  +         assertLocked(gtx, "/a/b/c1/one", true);
  +         assertLocked(gtx, "/a/b/c1/one/1", true);
   
            cache.put("/a/b/c1/two/2/3/4", null);
  -         checkLock(gtx, "/a/b/c1", true);
  -         checkLock(gtx, "/a/b/c1/two", true);
  -         checkLock(gtx, "/a/b/c1/two/2", false);
  -         checkLock(gtx, "/a/b/c1/two/2/3", false);
  -         checkLock(gtx, "/a/b/c1/two/2/3/4", true);
  +         assertLocked(gtx, "/a/b/c1", true);
  +         assertLocked(gtx, "/a/b/c1/two", true);
  +         assertLocked(gtx, "/a/b/c1/two/2", false);
  +         assertLocked(gtx, "/a/b/c1/two/2/3", false);
  +         assertLocked(gtx, "/a/b/c1/two/2/3/4", true);
   
            System.out.println("locks: " + cache.printLockInfo());
   
  @@ -411,23 +400,19 @@
         }
      }
   
  -   private void checkLock(Object owner, String fqn, boolean write_locked) throws Exception {
  +   private void assertLocked(Object owner, String fqn, boolean write_locked) throws Exception{
         DataNode n=cache.get(fqn);
         IdentityLock lock=n.getLock();
         if(owner == null)
            owner=Thread.currentThread();
  -      if(lock.isLocked() == false)
  -         throw new Exception("node " + fqn + " is not locked");
  +      assertTrue("node " + fqn + " is not locked", lock.isLocked());
         if(write_locked) {
  -         if(lock.isWriteLocked() == false)
  -            throw new Exception("node " + fqn + " is not write-locked");
  +         assertTrue("node " + fqn + " is not write-locked", lock.isWriteLocked());
         }
         else {
  -         if(lock.isReadLocked() == false)
  -            throw new Exception("node " + fqn + " is not read-locked");
  +         assertTrue("node " + fqn + " is not read-locked", lock.isReadLocked());
         }
  -      if(lock.isOwner(owner) == false)
  -         throw new Exception("owner " + owner + "is not owner");
  +      assertTrue("owner " + owner + "is not owner", lock.isOwner(owner));
      }
   
   
  
  
  
  1.5.2.1   +110 -9    JBossCache/tests/functional/org/jboss/cache/transaction/IsolationLevelReadCommittedTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: IsolationLevelReadCommittedTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/IsolationLevelReadCommittedTest.java,v
  retrieving revision 1.5
  retrieving revision 1.5.2.1
  diff -u -b -r1.5 -r1.5.2.1
  --- IsolationLevelReadCommittedTest.java	22 Feb 2006 17:04:45 -0000	1.5
  +++ IsolationLevelReadCommittedTest.java	23 Nov 2006 17:57:10 -0000	1.5.2.1
  @@ -11,6 +11,7 @@
   import org.jboss.cache.Fqn;
   import org.jboss.cache.TreeCache;
   import org.jboss.cache.lock.IsolationLevel;
  +import org.jboss.cache.lock.TimeoutException;
   
   import javax.transaction.NotSupportedException;
   import javax.transaction.SystemException;
  @@ -23,7 +24,7 @@
    *
    * @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
    *
  - * @version $Id: IsolationLevelReadCommittedTest.java,v 1.5 2006/02/22 17:04:45 msurtani Exp $
  + * @version $Id: IsolationLevelReadCommittedTest.java,v 1.5.2.1 2006/11/23 17:57:10 msurtani Exp $
    */
   
   public class IsolationLevelReadCommittedTest extends TestCase
  @@ -31,6 +32,7 @@
   
      private TreeCache cache = null;
      private final Fqn FQN = Fqn.fromString("/a/b/c");
  +   private final Fqn PARENT_FQN = Fqn.fromString("/a/b");
      private final String KEY = "key";
      private final String VALUE = "value";
   
  @@ -53,6 +55,7 @@
         cache.setCacheMode(TreeCache.LOCAL);
         cache.setIsolationLevel(IsolationLevel.READ_COMMITTED);
         cache.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  +      cache.setLockAcquisitionTimeout(1000);
         cache.startService();
      }
   
  @@ -105,10 +108,17 @@
                  // yet commit or roll back.
                  readerCanRead.acquire();
   
  +               try
  +               {
                  // I shouldn't be able to see the "dirty" value
                  assertEquals("thread w/ read lock " +
                               "can see subsequent uncommitted changes",
                                VALUE, cache.get(FQN, KEY));
  +               }
  +               catch (TimeoutException good)
  +               {
  +                  // this is what should happen due to writer's WL
  +               }
   
                  // let the writer know it can rollback
                  writerCanRollback.release();
  @@ -210,6 +220,99 @@
         }
      }
   
  +   /**
  +    * Test creates a cache node then starts a separate thread that removes
  +    * the node inside a tx. Test confirms that the removal cannot be seen
  +    * before the test commits.
  +    * 
  +    * @throws Exception
  +    */
  +   public void testNodeRemoved() throws Exception
  +   {
  +      final Latch readerCanRead = new Latch();
  +      final Latch readerDone = new Latch();
  +      final Latch writerDone = new Latch();
  +
  +      cache.put(FQN, KEY, VALUE);
  +      assertEquals(VALUE, cache.get(FQN, KEY));
  +
  +      // start a writer thread and a transaction
  +
  +      Thread writerThread = new Thread(new Runnable()
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               Transaction tx = startTransaction();
  +
  +               // change VALUE in a transaction
  +               cache.remove(PARENT_FQN);
  +
  +               // notify the reading thread
  +               readerCanRead.release();
  +               
  +               readerDone.acquire();
  +               
  +               tx.commit();
  +            }
  +            catch (AssertionFailedError e)
  +            {
  +               writerError = e;
  +            }
  +            catch(Throwable t)
  +            {
  +               t.printStackTrace();
  +               writerFailed = true;
  +            }
  +            finally
  +            {
  +               System.out.println("writer thread exits");
  +               readerCanRead.release();
  +               writerDone.release();
  +            }
  +         }
  +      }, "WRITER");
  +      writerThread.start();
  +      
  +      try
  +      {
  +         // wait until the writer thread changes the value in a transaction, 
  +         // but it did not yet commit or roll back.
  +         readerCanRead.acquire();
  +
  +         // I shouldn't be able to see the "dirty" value
  +         assertEquals("2nd thread cannot see uncommitted changes",
  +                       VALUE, cache.get(FQN, KEY));
  +      }
  +      catch (TimeoutException t)
  +      {
  +         // ignore, this is good
  +      }
  +      finally
  +      {               
  +         System.out.println("reader thread exits");
  +         readerDone.release();
  +      }
  +
  +      // wait for the writer to finish
  +      writerDone.acquire();
  +
  +      assertNull("Node was removed", cache.get(FQN));
  +      
  +      // If any assertion failed, throw on the AssertionFailedError
  +      
  +      if (writerError != null)
  +      {
  +         throw writerError;
  +      }
  +
  +      if (writerFailed)
  +      {
  +         fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
  +      }      
  +   }
  +
      private Transaction startTransaction() throws SystemException, NotSupportedException
      {
         DummyTransactionManager mgr=DummyTransactionManager.getInstance();
  @@ -217,8 +320,6 @@
         return mgr.getTransaction();
      }
   
  -
  -
      public static Test suite() {
   
         return new TestSuite(IsolationLevelReadCommittedTest.class);
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.2.4.2   +179 -0    JBossCache/tests/functional/org/jboss/cache/transaction/IsolationLevelSerializableTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: IsolationLevelSerializableTest.java
  ===================================================================
  RCS file: IsolationLevelSerializableTest.java
  diff -N IsolationLevelSerializableTest.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ IsolationLevelSerializableTest.java	23 Nov 2006 17:57:10 -0000	1.2.4.2
  @@ -0,0 +1,179 @@
  +package org.jboss.cache.transaction;
  +
  +
  +
  +import EDU.oswego.cs.dl.util.concurrent.Latch;
  +import junit.framework.AssertionFailedError;
  +import junit.framework.Test;
  +import junit.framework.TestCase;
  +import junit.framework.TestSuite;
  +import org.jboss.cache.DummyTransactionManagerLookup;
  +import org.jboss.cache.Fqn;
  +import org.jboss.cache.TreeCache;
  +import org.jboss.cache.lock.IsolationLevel;
  +import org.jboss.cache.lock.TimeoutException;
  +
  +import javax.transaction.NotSupportedException;
  +import javax.transaction.SystemException;
  +import javax.transaction.Transaction;
  +
  +
  +
  +/**
  + * Tests READ_COMMITED isolation level.
  + *
  + * @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
  + *
  + * @version $Id: IsolationLevelSerializableTest.java,v 1.2.4.2 2006/11/23 17:57:10 msurtani Exp $
  + */
  +
  +public class IsolationLevelSerializableTest extends TestCase
  +{
  +
  +   private TreeCache cache = null;
  +   private final Fqn FQN = Fqn.fromString("/a");
  +   private final String KEY = "key";
  +   private final String VALUE = "value";
  +
  +   private volatile boolean writerFailed;
  +   private volatile boolean readerFailed;
  +   private volatile AssertionFailedError writerError;
  +   private volatile AssertionFailedError readerError;
  +
  +   protected void setUp() throws Exception
  +   {
  +      super.setUp();
  +
  +      writerFailed = false;
  +      readerFailed = false;
  +
  +      writerError = null;
  +      readerError = null;
  +      
  +      cache = new TreeCache();
  +      cache.setCacheMode(TreeCache.LOCAL);
  +      cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
  +      cache.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  +      cache.setLockAcquisitionTimeout(1000);
  +      cache.startService();
  +   }
  +
  +
  +   protected void tearDown() throws Exception
  +   {
  +      super.tearDown();
  +
  +      cache.stopService();
  +      cache.destroyService();
  +      cache=null;
  +   }
  +   
  +   /**
  +    * Test creates a cache node then starts a separate thread that removes
  +    * the node inside a tx. Test confirms that the removal cannot be seen
  +    * before the test commits.
  +    * 
  +    * @throws Exception
  +    */
  +   public void testNodeRemoved() throws Exception
  +   {
  +      final Latch readerCanRead = new Latch();
  +      final Latch readerDone = new Latch();
  +      final Latch writerDone = new Latch();
  +
  +      cache.put(FQN, KEY, VALUE);
  +      assertEquals(VALUE, cache.get(FQN, KEY));
  +
  +      // start a writer thread and a transaction
  +
  +      Thread writerThread = new Thread(new Runnable()
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               Transaction tx = startTransaction();
  +
  +               // change VALUE in a transaction
  +               cache.remove(FQN);
  +
  +               // notify the reading thread
  +               readerCanRead.release();
  +               
  +               readerDone.acquire();
  +               
  +               tx.commit();
  +            }
  +            catch (AssertionFailedError e)
  +            {
  +               writerError = e;
  +            }
  +            catch(Throwable t)
  +            {
  +               t.printStackTrace();
  +               writerFailed = true;
  +            }
  +            finally
  +            {
  +               System.out.println("writer thread exits");
  +               readerCanRead.release();
  +               writerDone.release();
  +            }
  +         }
  +      }, "WRITER");
  +      writerThread.start();
  +      
  +      try
  +      {
  +         // wait until the writer thread changes the value in a transaction, 
  +         // but it did not yet commit or roll back.
  +         readerCanRead.acquire();
  +
  +         // I shouldn't be able to see the "dirty" value
  +         assertEquals("2nd thread cannot see uncommitted changes",
  +                       VALUE, cache.get(FQN, KEY));
  +      }
  +      catch (TimeoutException t)
  +      {
  +         // ignore, this is good
  +      }
  +      finally
  +      {               
  +         System.out.println("reader thread exits");
  +         readerDone.release();
  +      }
  +
  +      // wait for the writer to finish
  +      writerDone.acquire();
  +
  +      assertNull("Node was removed", cache.get(FQN));
  +
  +      // If any assertion failed, throw on the AssertionFailedError
  +      
  +      if (writerError != null)
  +      {
  +         throw writerError;
  +      }
  +
  +      if (writerFailed)
  +      {
  +         fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
  +      }
  +      
  +   }
  +
  +   private Transaction startTransaction() throws SystemException, NotSupportedException
  +   {
  +      DummyTransactionManager mgr=DummyTransactionManager.getInstance();
  +      mgr.begin();
  +      return mgr.getTransaction();
  +   }
  +
  +   public static Test suite() {
  +
  +      return new TestSuite(IsolationLevelSerializableTest.class);
  +
  +   }
  +
  +}
  +
  
  
  



More information about the jboss-cvs-commits mailing list