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

Manik Surtani manik at jboss.org
Fri Mar 16 13:48:17 EDT 2007


  User: msurtani
  Date: 07/03/16 13:48:17

  Added:       tests/functional/org/jboss/cache/api/pfer     
                        PutForExternalReadTestBase.java
                        PFERPessimisticReplSyncTest.java
                        PFEROptimisticReplAsyncTest.java
                        PFEROptimisticReplSyncTest.java
                        PFERPessimisticReplAsyncTest.java
  Log:
  Added tests for putForExternalRead()
  
  Revision  Changes    Path
  1.1      date: 2007/03/16 17:48:17;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/api/pfer/PutForExternalReadTestBase.java
  
  Index: PutForExternalReadTestBase.java
  ===================================================================
  package org.jboss.cache.api.pfer;
  
  import org.jboss.cache.Cache;
  import org.jboss.cache.CacheImpl;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.NodeSPI;
  import org.jboss.cache.RPCManager;
  import org.jboss.cache.RPCManagerImpl;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.lock.NodeLock;
  import org.jboss.cache.misc.TestingUtil;
  import org.jgroups.Address;
  import org.jmock.Mock;
  import org.jmock.MockObjectTestCase;
  import org.jmock.core.Constraint;
  
  import javax.transaction.Transaction;
  import javax.transaction.TransactionManager;
  import java.lang.reflect.Method;
  import java.util.List;
  
  public class PutForExternalReadTestBase extends MockObjectTestCase
  {
     protected Cache<String, String> cache1, cache2;
  
     protected TransactionManager tm1, tm2;
  
     protected Fqn<String> fqn = Fqn.fromString("/one/two");
     protected Fqn<String> parentFqn = fqn.getParent();
  
     protected String key = "k", value = "v", value2 = "v2";
  
     protected boolean useTx, optimistic;
     protected Configuration.CacheMode cacheMode;
  
     protected void setUp()
     {
        cache1 = UnitTestCacheFactory.createCache(cacheMode, false);
        cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
        cache1.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
  
        cache1.start();
        tm1 = cache1.getConfiguration().getRuntimeConfig().getTransactionManager();
  
        cache2 = UnitTestCacheFactory.createCache(cacheMode, false);
        cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
        cache2.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
  
        cache2.start();
        tm2 = cache2.getConfiguration().getRuntimeConfig().getTransactionManager();
  
        TestingUtil.blockUntilViewsReceived(10000, cache1, cache2);
     }
  
     protected void tearDown()
     {
        if (cache1 != null)
        {
           if (tm1 != null)
           {
              try
              {
                 tm1.rollback();
              }
              catch (Exception e)
              {
                 // do nothing
              }
           }
           cache1.stop();
           tm1 = null;
           cache1 = null;
        }
  
        if (cache2 != null)
        {
           if (tm2 != null)
           {
              try
              {
                 tm2.rollback();
              }
              catch (Exception e)
              {
                 // do nothing
              }
           }
           cache2.stop();
           tm2 = null;
           cache2 = null;
        }
     }
  
     /**
      * Locks could only occur on the parent node is write locked since if the child node exists it is a no-op anyway.
      * If the parent node is read locked as well, there is no issue.
      */
     public void testNoOpWhenLocked() throws Exception
     {
        tm1.begin();
        cache1.put(parentFqn, key, value);
        NodeSPI parentNode = (NodeSPI) cache1.getRoot().getChild(parentFqn);
        Transaction t = tm1.suspend();
  
        assertLocked(parentNode, true);
  
        // parentFqn should be write-locked.
        cache1.putForExternalRead(fqn, key, value);
        // should not block.
  
        tm1.resume(t);
        tm1.commit();
  
        asyncWait();
  
        assertEquals("Parent node write should have succeeded", value, cache1.get(parentFqn, key));
        assertEquals("Parent node write should have replicated", value, cache2.get(parentFqn, key));
  
        assertNull("PFER should have been a no-op", cache1.get(fqn, key));
        assertNull("PFER should have been a no-op", cache2.get(fqn, key));
     }
  
     public void testNoOpWhenNodePresent()
     {
        cache1.putForExternalRead(fqn, key, value);
        asyncWait();
  
        assertEquals("PFER should have succeeded", value, cache1.get(fqn, key));
        assertEquals("PFER should have replicated", value, cache2.get(fqn, key));
  
        // reset
        cache1.removeNode(fqn);
        asyncWait();
  
        assertFalse("Should have reset", cache1.getRoot().hasChild(fqn));
        assertFalse("Should have reset", cache2.getRoot().hasChild(fqn));
  
        cache1.put(fqn, key, value);
        asyncWait();
  
        // now this pfer should be a no-op
        cache1.putForExternalRead(fqn, key, value2);
  
        assertEquals("PFER should have been a no-op", value, cache1.get(fqn, key));
        assertEquals("PFER should have been a no-op", value, cache2.get(fqn, key));
     }
  
     public void testAsyncForce()
     {
        Mock mockRpcManager = mock(RPCManager.class);
  
        // inject a mock RPC manager so that we can test whether calls made are sync or async.
        ((CacheImpl) cache1).setRpcManager((RPCManager) mockRpcManager.proxy());
  
        // specify what we expect called on the mock Rpc Manager.  For params we don't care about, just use ANYTHING.
        // setting the mock object to expect the "sync" param to be false.
        mockRpcManager.expects(once()).method("callRemoteMethods").with(new Constraint[]{ANYTHING, ANYTHING, ANYTHING, eq(false), ANYTHING, ANYTHING});
        // now try a simple replication.  Since the RPCManager is a mock object it will not actually replicate anything.
        cache1.putForExternalRead(fqn, key, value);
  
        // cleanup
        cache1.removeNode(fqn);
  
        // now for a negative test.
        // now set the mock object to expect sync = true
        mockRpcManager.expects(once()).method("callRemoteMethods").with(new Constraint[]{ANYTHING, ANYTHING, ANYTHING, eq(true), ANYTHING, ANYTHING});
  
        try
        {
           cache1.putForExternalRead(fqn, key, value);
           fail("Should have barfed");
        }
        catch (Exception e)
        {
           // expected
        }
     }
  
     public void testTxSuspension() throws Exception
     {
        // start a tx and do some stuff.
        tm1.begin();
        cache1.put(parentFqn, key, value);
        cache1.putForExternalRead(fqn, key, value); // should have happened in a separate tx and have committed already.
        Transaction t = tm1.suspend();
  
        asyncWait();
  
        assertNull("parent fqn transaction has not completed", cache1.get(parentFqn, key));
        assertNull("parent fqn transaction has not completed", cache2.get(parentFqn, key));
  
        assertEquals("PFER should have completed", value, cache1.get(fqn, key));
        assertEquals("PFER should have completed", value, cache2.get(fqn, key));
  
        tm1.resume(t);
        tm1.commit();
  
        asyncWait();
  
        assertEquals("parent fqn tx should have completed", value, cache1.get(parentFqn, key));
        assertEquals("parent fqn tx should have completed", value, cache2.get(parentFqn, key));
     }
  
     public void testExceptionSuppression()
     {
        RPCManager barfingRpcManager = new RPCManagerImpl()
        {
           public List callRemoteMethods(List<Address> recipients, Method method, Object[] arguments, boolean synchronous, boolean excludeSelf, long timeout)
           {
              throw new RuntimeException("Barf");
           }
        };
  
        ((CacheImpl) cache1).setRpcManager(barfingRpcManager);
  
        try
        {
           cache1.put(fqn, key, value);
           fail("Should have barfed");
        }
        catch (RuntimeException re)
        {
           // expected.
        }
  
        assertNull("State should not have been applied", cache1.get(fqn, key));
        // should not barf
        cache1.putForExternalRead(fqn, key, value);
  
        // but should not apply state either
        assertNull("State should not have been applied", cache1.get(fqn, key));
     }
  
     public void test0msForce()
     {
        fail("Implement me");
     }
  
     protected void assertLocked(NodeSPI n, boolean write_locked) throws Exception
     {
        NodeLock lock = n.getLock();
        assertTrue("node " + fqn + " is not locked", lock.isLocked());
        if (write_locked)
        {
           assertTrue("node " + fqn + " is not write-locked" + (lock.isReadLocked() ? " but is read-locked instead!" : "!"), lock.isWriteLocked());
        }
        else
        {
           assertTrue("node " + fqn + " is not read-locked" + (lock.isWriteLocked() ? " but is write-locked instead!" : "!"), lock.isReadLocked());
        }
     }
  
     protected void asyncWait()
     {
        TestingUtil.sleepThread(500);
     }
  }
  
  
  
  1.1      date: 2007/03/16 17:48:17;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/api/pfer/PFERPessimisticReplSyncTest.java
  
  Index: PFERPessimisticReplSyncTest.java
  ===================================================================
  package org.jboss.cache.api.pfer;
  
  import org.jboss.cache.config.Configuration;
  
  public class PFERPessimisticReplSyncTest extends PutForExternalReadTestBase
  {
     public PFERPessimisticReplSyncTest()
     {
        optimistic = false;
        cacheMode = Configuration.CacheMode.REPL_SYNC;
     }
  }
  
  
  
  1.1      date: 2007/03/16 17:48:17;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/api/pfer/PFEROptimisticReplAsyncTest.java
  
  Index: PFEROptimisticReplAsyncTest.java
  ===================================================================
  package org.jboss.cache.api.pfer;
  
  import org.jboss.cache.config.Configuration;
  
  public class PFEROptimisticReplAsyncTest extends PutForExternalReadTestBase
  {
     public PFEROptimisticReplAsyncTest()
     {
        optimistic = true;
        cacheMode = Configuration.CacheMode.REPL_ASYNC;
     }
  }
  
  
  
  1.1      date: 2007/03/16 17:48:17;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/api/pfer/PFEROptimisticReplSyncTest.java
  
  Index: PFEROptimisticReplSyncTest.java
  ===================================================================
  package org.jboss.cache.api.pfer;
  
  import org.jboss.cache.config.Configuration;
  
  public class PFEROptimisticReplSyncTest extends PutForExternalReadTestBase
  {
     public PFEROptimisticReplSyncTest()
     {
        optimistic = true;
        cacheMode = Configuration.CacheMode.REPL_SYNC;
     }
  }
  
  
  
  1.1      date: 2007/03/16 17:48:17;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/api/pfer/PFERPessimisticReplAsyncTest.java
  
  Index: PFERPessimisticReplAsyncTest.java
  ===================================================================
  package org.jboss.cache.api.pfer;
  
  import org.jboss.cache.config.Configuration;
  
  public class PFERPessimisticReplAsyncTest extends PutForExternalReadTestBase
  {
     public PFERPessimisticReplAsyncTest()
     {
        optimistic = false;
        cacheMode = Configuration.CacheMode.REPL_ASYNC;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list