Package org.jboss.cache.api.pfer

Source Code of org.jboss.cache.api.pfer.PutForExternalReadTestBase$PutForExternalReadTestBaseTL

package org.jboss.cache.api.pfer;

import org.easymock.EasyMock;
import static org.easymock.EasyMock.*;
import org.jboss.cache.Cache;
import org.jboss.cache.CacheFactory;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.RPCManager;
import org.jboss.cache.commands.ReplicableCommand;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.factories.ComponentRegistry;
import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
import org.jboss.cache.optimistic.TransactionWorkspace;
import org.jboss.cache.transaction.GlobalTransaction;
import org.jboss.cache.transaction.OptimisticTransactionContext;
import org.jboss.cache.util.TestingUtil;
import org.jboss.cache.util.internals.ReplicationListener;
import org.jgroups.Address;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import java.util.List;
import java.util.Vector;
import org.jboss.cache.UnitTestCacheFactory;

@Test(groups = {"functional", "jgroups", "transaction"})
public abstract class PutForExternalReadTestBase
{
   // TODO: Refactor this test so it isn't bound to using Thread.sleep() calls.

   protected Configuration.CacheMode cacheMode;
   protected NodeLockingScheme nodeLockingScheme;
   protected final Fqn fqn = Fqn.fromString("/one/two");
   protected final Fqn parentFqn = fqn.getParent();
   protected final String key = "k", value = "v", value2 = "v2";
  
   protected class PutForExternalReadTestBaseTL {
      protected CacheSPI<String, String> cache1, cache2;

      ReplicationListener replListener1;
      ReplicationListener replListener2;

      protected TransactionManager tm1, tm2;

      protected boolean useTx;
   }
  
  
   ThreadLocal<PutForExternalReadTestBaseTL> threadLocal = new ThreadLocal<PutForExternalReadTestBaseTL>();

   @BeforeMethod(alwaysRun = true)
   public void setUp()
   {
      PutForExternalReadTestBaseTL tl = new PutForExternalReadTestBaseTL();
      threadLocal.set(tl);
     
      CacheFactory<String, String> cf = new UnitTestCacheFactory<String, String>();

      tl.cache1 = (CacheSPI<String, String>) cf.createCache(UnitTestCacheConfigurationFactory.createConfiguration(cacheMode), false);
      tl.cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
      tl.cache1.getConfiguration().setNodeLockingScheme(nodeLockingScheme);

      tl.cache1.start();
      tl.tm1 = tl.cache1.getConfiguration().getRuntimeConfig().getTransactionManager();

      tl.cache2 = (CacheSPI<String, String>) cf.createCache(UnitTestCacheConfigurationFactory.createConfiguration(cacheMode), false);
      tl.cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
      tl.cache2.getConfiguration().setNodeLockingScheme(nodeLockingScheme);

      tl.cache2.start();
      tl.tm2 = tl.cache2.getConfiguration().getRuntimeConfig().getTransactionManager();

      TestingUtil.blockUntilViewsReceived(10000, tl.cache1, tl.cache2);
   }

   @AfterMethod(alwaysRun = true)
   public void tearDown()
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      if (tl != null) {
         TestingUtil.killCaches(tl.cache1, tl.cache2);
         threadLocal.set(null);
      }
   }

   public void testNoOpWhenNodePresent()
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      tl.cache1.putForExternalRead(fqn, key, value);
      asyncWait();

      assertEquals("PFER should have succeeded", value, tl.cache1.get(fqn, key));
      if (isUsingInvalidation())
         assertNull("PFER should not have effected cache2", tl.cache2.get(fqn, key));
      else
         assertEquals("PFER should have replicated", value, tl.cache2.get(fqn, key));

      // reset
      tl.cache1.removeNode(fqn);
      asyncWait();

      assertFalse("Should have reset", tl.cache1.getRoot().hasChild(fqn));
      assertFalse("Should have reset", tl.cache2.getRoot().hasChild(fqn));

      tl.cache1.put(fqn, key, value);
      asyncWait();

      // now this pfer should be a no-op
      tl.cache1.putForExternalRead(fqn, key, value2);

      assertEquals("PFER should have been a no-op", value, tl.cache1.get(fqn, key));
      if (isUsingInvalidation())
         assertNull("PFER should have been a no-op", tl.cache2.get(fqn, key));
      else
         assertEquals("PFER should have been a no-op", value, tl.cache2.get(fqn, key));
   }

   private Vector<Address> anyAddresses()
   {
      anyObject();
      return null;
   }

   public void testAsyncForce() throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      RPCManager rpcManager = EasyMock.createNiceMock(RPCManager.class);
      RPCManager originalRpcManager = tl.cache1.getConfiguration().getRuntimeConfig().getRPCManager();
      List<Address> memberList = originalRpcManager.getMembers();
      expect(rpcManager.getMembers()).andReturn(memberList).anyTimes();
      // inject a mock RPC manager so that we can test whether calls made are sync or async.
      ComponentRegistry cr = TestingUtil.extractComponentRegistry(tl.cache1);
      cr.registerComponent(rpcManager, RPCManager.class);
      cr.rewire();

      // invalidations will not trigger any rpc calls for PFER
      if (!isUsingInvalidation())
      {
         // 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.
         expect(rpcManager.callRemoteMethods(anyAddresses(), (ReplicableCommand) anyObject(), eq(false), anyLong(), anyBoolean())).andReturn(null);
      }

      replay(rpcManager);

      // now try a simple replication.  Since the RPCManager is a mock object it will not actually replicate anything.
      tl.cache1.putForExternalRead(fqn, key, value);
      verify(rpcManager);

      // cleanup
      TestingUtil.extractComponentRegistry(tl.cache1).registerComponent(originalRpcManager, RPCManager.class);
      tl.cache1.removeNode(fqn);
   }

   public void testTxSuspension() throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      // create parent node first
      tl.cache1.put(parentFqn, key, value);

      // start a tx and do some stuff.
      tl.tm1.begin();
      tl.cache1.get(parentFqn, key);
      tl.cache1.putForExternalRead(fqn, key, value); // should have happened in a separate tx and have committed already.
      Transaction t = tl.tm1.suspend();

      asyncWait();

      assertLocked(parentFqn, tl.cache1, false);

      assertEquals("PFER should have completed", value, tl.cache1.get(fqn, key));
      if (isUsingInvalidation())
         assertNull("PFER should not have effected cache2", tl.cache2.get(fqn, key));
      else
         assertEquals("PFER should have completed", value, tl.cache2.get(fqn, key));

      tl.tm1.resume(t);
      tl.tm1.commit();

      assertEquals("parent fqn tx should have completed", value, tl.cache1.get(parentFqn, key));
      if (isUsingInvalidation())
         assertNull("parent fqn tx should have invalidated cache2", tl.cache2.get(parentFqn, key));
      else
         assertEquals("parent fqn tx should have completed", value, tl.cache2.get(parentFqn, key));
   }

   public void testExceptionSuppression() throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      RPCManager barfingRpcManager = EasyMock.createNiceMock(RPCManager.class);
      RPCManager originalRpcManager = tl.cache1.getConfiguration().getRuntimeConfig().getRPCManager();
      try
      {
         List<Address> memberList = originalRpcManager.getMembers();
         expect(barfingRpcManager.getMembers()).andReturn(memberList).anyTimes();
         expect(barfingRpcManager.getLocalAddress()).andReturn(originalRpcManager.getLocalAddress()).anyTimes();
         expect(barfingRpcManager.callRemoteMethods(anyAddresses(), (ReplicableCommand) anyObject(), anyBoolean(), anyLong(), anyBoolean())).andThrow(new RuntimeException("Barf!")).anyTimes();
         replay(barfingRpcManager);

         TestingUtil.extractComponentRegistry(tl.cache1).registerComponent(barfingRpcManager, RPCManager.class);
         tl.cache1.getConfiguration().getRuntimeConfig().setRPCManager(barfingRpcManager);
         TestingUtil.extractComponentRegistry(tl.cache1).rewire();

         try
         {
            tl.cache1.put(fqn, key, value);
            if (!isOptimistic()) fail("Should have barfed");
         }
         catch (RuntimeException re)
         {
         }

         if (isOptimistic() && !isUsingInvalidation())
         {
            // proves that the put did, in fact, barf.  Doesn't work for invalidations since the inability to invalidate will not cause a rollback.
            assertNull(tl.cache1.get(fqn, key));
         }
         else
         {
            // clean up any indeterminate state left over
            try
            {
               tl.cache1.removeNode(fqn);
               // as above, the inability to invalidate will not cause an exception
               if (!isUsingInvalidation()) fail("Should have barfed");
            }
            catch (RuntimeException re)
            {
            }
         }

         assertNull("Should have cleaned up", tl.cache1.get(fqn, key));

         // should not barf
         tl.cache1.putForExternalRead(fqn, key, value);
      }
      finally
      {
         TestingUtil.extractComponentRegistry(tl.cache1).registerComponent(originalRpcManager, RPCManager.class);
      }
   }

   public void testBasicPropagation() throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      assert !tl.cache1.exists(fqn);
      assert !tl.cache2.exists(fqn);

      tl.cache1.putForExternalRead(fqn, key, value);

      asyncWait();

      assertEquals("PFER updated cache1", value, tl.cache1.get(fqn, key));
      Object expected = isUsingInvalidation() ? null : value;
      assertEquals("PFER propagated to cache2 as expected", expected, tl.cache2.get(fqn, key));

      // replication to cache 1 should NOT happen.
      tl.cache2.putForExternalRead(fqn, key, value);

      assertEquals("PFER updated cache2", value, tl.cache2.get(fqn, key));
      assertEquals("Cache1 should be unaffected", value, tl.cache1.get(fqn, key));
   }

   /**
    * Tests that setting a cacheModeLocal=true Option prevents propagation
    * of the putForExternalRead().
    *
    * @throws Exception
    */
   public void testSimpleCacheModeLocal() throws Exception
   {
      cacheModeLocalTest(false);
   }

   /**
    * Tests that setting a cacheModeLocal=true Option prevents propagation
    * of the putForExternalRead() when the call occurs inside a transaction.
    *
    * @throws Exception
    */
   public void testCacheModeLocalInTx() throws Exception
   {
      cacheModeLocalTest(true);
   }

   /**
    * Tests that suspended transactions do not leak.  See JBCACHE-1246.
    */
   public void testMemLeakOnSuspendedTransactions() throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      Fqn fqn2 = Fqn.fromString("/fqn/two");

      tl.tm1.begin();
      tl.cache1.putForExternalRead(fqn, key, value);
      tl.tm1.commit();

      TestingUtil.sleepThread(500);

      assert tl.cache1.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 1 should have no stale global TXs";
      assert tl.cache1.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 1 should have no stale local TXs";
      assert tl.cache2.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 2 should have no stale global TXs";
      assert tl.cache2.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 2 should have no stale local TXs";

      tl.tm1.begin();
      tl.cache1.putForExternalRead(fqn, key, value);
      tl.cache1.put(fqn2, key, value);
      tl.tm1.commit();

      asyncWait();

      assert tl.cache1.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 1 should have no stale global TXs";
      assert tl.cache1.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 1 should have no stale local TXs";
      assert tl.cache2.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 2 should have no stale global TXs";
      assert tl.cache2.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 2 should have no stale local TXs";

      tl.tm1.begin();
      tl.cache1.put(fqn2, key, value);
      tl.cache1.putForExternalRead(fqn, key, value);
      tl.tm1.commit();

      asyncWait();

      assert tl.cache1.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 1 should have no stale global TXs";
      assert tl.cache1.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 1 should have no stale local TXs";
      assert tl.cache2.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 2 should have no stale global TXs";
      assert tl.cache2.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 2 should have no stale local TXs";

      tl.tm1.begin();
      tl.cache1.put(fqn2, key, value);
      tl.cache1.putForExternalRead(fqn, key, value);
      tl.cache1.put(fqn2, key, value);
      tl.tm1.commit();

      asyncWait();

      assert tl.cache1.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 1 should have no stale global TXs";
      assert tl.cache1.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 1 should have no stale local TXs";
      assert tl.cache2.getTransactionTable().getNumGlobalTransactions() == 0 : "Cache 2 should have no stale global TXs";
      assert tl.cache2.getTransactionTable().getNumLocalTransactions() == 0 : "Cache 2 should have no stale local TXs";
   }

   /**
    * Tests that setting a cacheModeLocal=true Option prevents propagation
    * of the putForExternalRead().
    *
    * @throws Exception
    */
   private void cacheModeLocalTest(boolean transactional) throws Exception
   {
      PutForExternalReadTestBaseTL tl = threadLocal.get();
      RPCManager rpcManager = EasyMock.createMock(RPCManager.class);
      RPCManager originalRpcManager = tl.cache1.getConfiguration().getRuntimeConfig().getRPCManager();

      // inject a mock RPC manager so that we can test whether calls made are sync or async.
      tl.cache1.getConfiguration().getRuntimeConfig().setRPCManager(rpcManager);

      // specify that we expect nothing will be called on the mock Rpc Manager.
      replay(rpcManager);

      // now try a simple replication.  Since the RPCManager is a mock object it will not actually replicate anything.
      if (transactional)
         tl.tm1.begin();

      tl.cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
      tl.cache1.putForExternalRead(fqn, key, value);

      if (transactional)
         tl.tm1.commit();

      verify(rpcManager);
      // cleanup
      tl.cache1.getConfiguration().getRuntimeConfig().setRPCManager(originalRpcManager);
      tl.cache1.removeNode(fqn);
   }

   protected abstract void assertLocked(Fqn fqn, CacheSPI cache, boolean writeLocked);

   protected TransactionWorkspace extractTransactionWorkspace(Cache c)
   {
      CacheSPI cs = (CacheSPI) c;
      try
      {
         GlobalTransaction gtx = cs.getTransactionTable().get(cs.getTransactionManager().getTransaction());
         OptimisticTransactionContext entry = (OptimisticTransactionContext) cs.getTransactionTable().get(gtx);
         return entry.getTransactionWorkSpace();
      }
      catch (SystemException e)
      {
         e.printStackTrace();
         fail("Unable to extract transaction workspace from cache");
      }
      return null;
   }

   protected boolean isUsingInvalidation()
   {
      return cacheMode.isInvalidation();
   }

   protected boolean isAsync()
   {
      return !cacheMode.isSynchronous();
   }

   protected boolean isOptimistic()
   {
      return nodeLockingScheme == NodeLockingScheme.OPTIMISTIC;
   }

   protected void asyncWait()
   {
      // always needs to do this since PFER will force async comms.
      TestingUtil.sleepThread(500);
   }
}
TOP

Related Classes of org.jboss.cache.api.pfer.PutForExternalReadTestBase$PutForExternalReadTestBaseTL

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.