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

Manik Surtani msurtani at jboss.com
Fri Dec 1 10:22:44 EST 2006


  User: msurtani
  Date: 06/12/01 10:22:44

  Modified:    tests/functional/org/jboss/cache/transaction  Tag:
                        Branch_JBossCache_1_3_0
                        IsolationLevelRepeatableReadTest.java
  Log:
  wc
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +64 -269   JBossCache/tests/functional/org/jboss/cache/transaction/Attic/IsolationLevelRepeatableReadTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: IsolationLevelRepeatableReadTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/Attic/IsolationLevelRepeatableReadTest.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -b -r1.1.2.1 -r1.1.2.2
  --- IsolationLevelRepeatableReadTest.java	26 Nov 2006 11:52:05 -0000	1.1.2.1
  +++ IsolationLevelRepeatableReadTest.java	1 Dec 2006 15:22:44 -0000	1.1.2.2
  @@ -8,17 +8,20 @@
   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;
   
  +import EDU.oswego.cs.dl.util.concurrent.Latch;
  +
   /**
    * Tests READ_COMMITED isolation level.
    *
    * @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
    *
  - * @version $Id: IsolationLevelRepeatableReadTest.java,v 1.1.2.1 2006/11/26 11:52:05 msurtani Exp $
  + * @version $Id: IsolationLevelRepeatableReadTest.java,v 1.1.2.2 2006/12/01 15:22:44 msurtani Exp $
    */
   
   public class IsolationLevelRepeatableReadTest extends TestCase
  @@ -62,307 +65,99 @@
         cache=null;
      }
   
  -   public void testMultipleReaders() throws Exception
  +   /**
  +    * 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 Fqn fqn = Fqn.fromString("/test");
  -//      final Latch latch = new Latch();
  -      final Object lock = new Object();
  +      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));
   
  -      class ReaderThread extends Thread
  +      // start a writer thread and a transaction
  +
  +      Thread writerThread = new Thread(new Runnable()
         {
            public void run()
            {
               try
               {
  -               cache.getTransactionManager().begin();
  -//               latch.acquire();
  -
  -               synchronized(lock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  lock.wait();
  -               }
  +               Transaction tx = startTransaction();
   
  -               assertNotNull(cache.get(fqn));
  +               // change VALUE in a transaction
  +               cache.remove(FQN);
   
  -               synchronized(lock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  lock.wait();
  -               }
  -               cache.getTransactionManager().commit();
  -            }
  -            catch (Exception e)
  -            {
  -               e.printStackTrace();
  -               fail();
  -            }
  -         }
  -      }
  -
  -      Thread[] t = new Thread[5];
  -      for (int i=0; i<t.length; i++)
  -      {
  -         t[i] = new ReaderThread();
  -         t[i].start();
  -      }
  +               // notify the reading thread
  +               readerCanRead.release();
   
  -      cache.put(fqn, "k", "v");
  -      // now all are ready to go...
  -//      latch.release();
  +               readerDone.acquire();
   
  -      synchronized(lock)
  -      {
  -         System.out.println(cache.printLockInfo());
  -         lock.notifyAll();
  +               tx.commit();
         }
  -
  -      System.out.println("2nd time...");
  -
  -      synchronized(lock)
  +            catch (AssertionFailedError e)
         {
  -         System.out.println(cache.printLockInfo());
  -         // check that we have multiple readers:         
  -         assertEquals(5, cache.get(Fqn.ROOT).getLock().getReaderOwners().size());
  -         assertEquals(5, cache.get(fqn).getLock().getReaderOwners().size());
  -         assertNull(cache.get(Fqn.ROOT).getLock().getWriterOwner());
  -         assertNull(cache.get(fqn).getLock().getWriterOwner());
  -
  -         lock.notifyAll();
  -      }
  -
  -
  -      for (int i=0; i<t.length; i++) t[i].join();
  -
  -
  -
  -      assertEquals(0, cache.get(Fqn.ROOT).getLock().getReaderOwners().size());
  -      assertEquals(0, cache.get(fqn).getLock().getReaderOwners().size());
  -      assertNull(cache.get(Fqn.ROOT).getLock().getWriterOwner());
  -      assertNull(cache.get(fqn).getLock().getWriterOwner());
  +               writerError = e;
      }
  -
  -
  -   public void testMultipleReadersOneWriter() throws Exception
  -   {
  -      final Fqn fqn = Fqn.fromString("/test");
  -//      final Latch latch = new Latch();
  -      final Object rLock = new Object();
  -      final Object wLock = new Object();
  -
  -      class ReaderThread extends Thread
  -      {
  -         public void run()
  +            catch(Throwable t)
            {
  -            try
  -            {
  -               cache.getTransactionManager().begin();
  -//               latch.acquire();
  -
  -               synchronized(rLock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  rLock.wait();
  +               t.printStackTrace();
  +               writerFailed = true;
                  }
  -
  -               assertNotNull(cache.get(fqn));
  -
  -               synchronized(rLock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  rLock.wait();
  -               }
  -               cache.getTransactionManager().commit();
  -            }
  -            catch (Exception e)
  +            finally
               {
  -               e.printStackTrace();
  -               fail();
  -            }
  +               System.out.println("writer thread exits");
  +               readerCanRead.release();
  +               writerDone.release();
            }
         }
  +      }, "WRITER");
  +      writerThread.start();
         
  -      class WriterThread extends Thread
  -      {
  -         public void run()
  -         {
               try
               {
  -               cache.getTransactionManager().begin();
  -//               latch.acquire();
  -
  -               synchronized(wLock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  wLock.wait();
  -               }
  -
  -               cache.put(fqn, "k2", "v2");
  -               fail("Should not be allowed to acquire the WL");
  +         // wait until the writer thread changes the value in a transaction,
  +         // but it did not yet commit or roll back.
  +         readerCanRead.acquire();
   
  -               synchronized(rLock)
  -               {
  -                  System.out.println(getName() + " waiting on lock");
  -                  rLock.wait();
  +         // I shouldn't be able to see the "dirty" value
  +         assertEquals("2nd thread cannot see uncommitted changes",
  +                       VALUE, cache.get(FQN, KEY));
                  }
  -               cache.getTransactionManager().commit();
  -            }
  -            catch (Exception e)
  +      catch (TimeoutException t)
               {
  -               //expected
  +         // ignore, this is good
               }
               finally
               {
  -               try
  -               {
  -                  cache.getTransactionManager().rollback();
  -               }
  -               catch (Exception e)
  -               {
  -               }
  -            }
  -         }
  +         System.out.println("reader thread exits");
  +         readerDone.release();
         }
   
  -      cache.put(fqn, "k", "v");
  +      // wait for the writer to finish
  +      writerDone.acquire();
   
  -      Thread[] t = new Thread[6];
  -      for (int i=0; i<t.length - 1; i++)
  -      {
  -         t[i] = new ReaderThread();
  -         t[i].start();
  -      }
  -
  -      t[t.length - 1] = new WriterThread();
  -      t[t.length - 1].start();
  -
  -
  -      // now all are ready to go...
  -//      latch.release();
  -
  -      synchronized(rLock) { rLock.notifyAll();}
  -      synchronized(wLock) { wLock.notifyAll();}
  +      assertNull("Node was removed", cache.get(FQN));
   
  -      System.out.println("2nd time...");
  +      // If any assertion failed, throw on the AssertionFailedError
   
  -      synchronized(rLock)
  +      if (writerError != null)
         {
  -         System.out.println(cache.printLockInfo());
  -         // check that we have multiple readers:
  -         assertEquals(6, cache.get(Fqn.ROOT).getLock().getReaderOwners().size());
  -         assertEquals(5, cache.get(fqn).getLock().getReaderOwners().size());
  -         assertNull(cache.get(Fqn.ROOT).getLock().getWriterOwner());
  -         assertNull(cache.get(fqn).getLock().getWriterOwner());
  -
  -         rLock.notifyAll();
  +         throw writerError;
         }
   
  -
  -      for (int i=0; i<t.length; i++) t[i].join();
  -
  -
  -      System.out.println("Locks: " + cache.printLockInfo());
  -
  -      assertEquals(0, cache.get(Fqn.ROOT).getLock().getReaderOwners().size());
  -      assertEquals(0, cache.get(fqn).getLock().getReaderOwners().size());
  -      assertNull(cache.get(Fqn.ROOT).getLock().getWriterOwner());
  -      assertNull(cache.get(fqn).getLock().getWriterOwner());
  +      if (writerFailed)
  +      {
  +         fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
      }
   
  -
  -   /**
  -    * 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
      {
  
  
  



More information about the jboss-cvs-commits mailing list