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

Brian Stansberry brian.stansberry at jboss.com
Tue Nov 21 23:55:44 EST 2006


  User: bstansberry
  Date: 06/11/21 23:55:44

  Modified:    tests/functional/org/jboss/cache/transaction  
                        IsolationLevelReadCommittedTest.java
  Added:       tests/functional/org/jboss/cache/transaction  
                        IsolationLevelSerializableTest.java
  Log:
  [JBCACHE-871] Port test from Branch_1_4_0
  
  Revision  Changes    Path
  1.9       +16 -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.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- IsolationLevelReadCommittedTest.java	21 Nov 2006 23:33:41 -0000	1.8
  +++ IsolationLevelReadCommittedTest.java	22 Nov 2006 04:55:44 -0000	1.9
  @@ -6,9 +6,14 @@
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  +
  +import org.jboss.cache.Cache;
   import org.jboss.cache.DummyTransactionManagerLookup;
   import org.jboss.cache.Fqn;
   import org.jboss.cache.TreeCache;
  +import org.jboss.cache.config.Configuration;
  +import org.jboss.cache.config.Configuration.CacheMode;
  +import org.jboss.cache.factories.DefaultCacheFactory;
   import org.jboss.cache.lock.IsolationLevel;
   
   import javax.transaction.NotSupportedException;
  @@ -20,14 +25,15 @@
    * Tests READ_COMMITED isolation level.
    *
    * @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
  - * @version $Id: IsolationLevelReadCommittedTest.java,v 1.8 2006/11/21 23:33:41 bstansberry Exp $
  + * @version $Id: IsolationLevelReadCommittedTest.java,v 1.9 2006/11/22 04:55:44 bstansberry Exp $
    */
   
   public class IsolationLevelReadCommittedTest extends TestCase
   {
   
  -   private TreeCache cache = null;
  +   private Cache cache = null;
      private final Fqn FQN = Fqn.fromString("/a/b/c");
  +   private final Fqn PARENT_FQN = FQN.getParent();
      private final String KEY = "key";
      private final String VALUE = "value";
   
  @@ -46,11 +52,12 @@
         writerError = null;
         readerError = null;
   
  -      cache = new TreeCache();
  -      cache.getConfiguration().setCacheMode("LOCAL");
  -      cache.getConfiguration().setIsolationLevel(IsolationLevel.READ_COMMITTED);
  -      cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  -      cache.start();
  +      Configuration config = new Configuration();
  +      config.setCacheMode(CacheMode.LOCAL);
  +      config.setIsolationLevel(IsolationLevel.READ_COMMITTED);
  +      config.setLockAcquisitionTimeout(1000);
  +      config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  +      cache = DefaultCacheFactory.createCache(config);
      }
   
   
  @@ -233,7 +240,7 @@
                  Transaction tx = startTransaction();
   
                  // change VALUE in a transaction
  -               cache.remove(FQN);
  +               cache.removeNode(PARENT_FQN);
   
                  // notify the reading thread
                  readerCanRead.release();
  
  
  
  1.2       +180 -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	22 Nov 2006 04:55:44 -0000	1.2
  @@ -0,0 +1,180 @@
  +package org.jboss.cache.transaction;
  +
  +
  +
  +import javax.transaction.NotSupportedException;
  +import javax.transaction.SystemException;
  +import javax.transaction.Transaction;
  +
  +import junit.framework.AssertionFailedError;
  +import junit.framework.Test;
  +import junit.framework.TestCase;
  +import junit.framework.TestSuite;
  +
  +import org.jboss.cache.Cache;
  +import org.jboss.cache.DummyTransactionManagerLookup;
  +import org.jboss.cache.Fqn;
  +import org.jboss.cache.config.Configuration;
  +import org.jboss.cache.config.Configuration.CacheMode;
  +import org.jboss.cache.factories.DefaultCacheFactory;
  +import org.jboss.cache.lock.IsolationLevel;
  +import org.jboss.cache.lock.TimeoutException;
  +
  +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: IsolationLevelSerializableTest.java,v 1.2 2006/11/22 04:55:44 bstansberry Exp $
  + */
  +
  +public class IsolationLevelSerializableTest extends TestCase
  +{
  +
  +   private Cache 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 AssertionFailedError writerError;
  +
  +   protected void setUp() throws Exception
  +   {
  +      super.setUp();
  +
  +      writerFailed = false;
  +
  +      writerError = null;
  +      
  +      Configuration config = new Configuration();
  +      config.setCacheMode(CacheMode.LOCAL);
  +      config.setIsolationLevel(IsolationLevel.SERIALIZABLE);
  +      config.setLockAcquisitionTimeout(1000);
  +      config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  +      cache = DefaultCacheFactory.createCache(config);
  +   }
  +
  +
  +   protected void tearDown() throws Exception
  +   {
  +      super.tearDown();
  +
  +      cache.stop();
  +      cache.destroy();
  +      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.removeNode(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 good)
  +      {
  +         // ignore; means worked as it should
  +      }
  +      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