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

Brian Stansberry brian.stansberry at jboss.com
Tue Jan 23 00:38:22 EST 2007


  User: bstansberry
  Date: 07/01/23 00:38:22

  Added:       tests/functional/org/jboss/cache/passivation 
                        PassivationActivationCallbacksTestCase.java
  Log:
  Add a test of the usage pattern used by EJB3 SFSBs.
  
  Revision  Changes    Path
  1.2       +223 -0    JBossCache/tests/functional/org/jboss/cache/passivation/PassivationActivationCallbacksTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PassivationActivationCallbacksTestCase.java
  ===================================================================
  RCS file: PassivationActivationCallbacksTestCase.java
  diff -N PassivationActivationCallbacksTestCase.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ PassivationActivationCallbacksTestCase.java	23 Jan 2007 05:38:22 -0000	1.2
  @@ -0,0 +1,223 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
  +package org.jboss.cache.passivation;
  +
  +import java.util.ArrayList;
  +import java.util.HashSet;
  +import java.util.List;
  +import java.util.Set;
  +
  +import junit.framework.TestCase;
  +
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.AbstractCacheListener;
  +import org.jboss.cache.CacheException;
  +import org.jboss.cache.CacheImpl;
  +import org.jboss.cache.CacheSPI;
  +import org.jboss.cache.DefaultCacheFactory;
  +import org.jboss.cache.Fqn;
  +import org.jboss.cache.Node;
  +import org.jboss.cache.RegionManager;
  +import org.jboss.cache.config.CacheLoaderConfig;
  +import org.jboss.cache.config.EvictionConfig;
  +import org.jboss.cache.config.EvictionRegionConfig;
  +import org.jboss.cache.eviction.LRUConfiguration;
  +import org.jboss.cache.loader.CacheLoader;
  +import org.jboss.cache.loader.FileCacheLoaderConfig;
  +import org.jboss.cache.lock.IsolationLevel;
  +import org.jboss.cache.misc.TestingUtil;
  +import org.jboss.cache.xml.XmlHelper;
  +import org.w3c.dom.Element;
  +
  +/**
  + * Tests that the TreeCacheListener implementation used by EJB3 SFSBs works. 
  + * 
  + * @author Brian Stansberry
  + *
  + */
  +public class PassivationActivationCallbacksTestCase extends TestCase
  +{
  +   private static final Fqn BASE = Fqn.fromString("/base");
  +   private static final Log log = LogFactory.getLog(PassivationActivationCallbacksTestCase.class);
  +
  +   //Cache Loader fields
  +   private CacheSPI         cache;
  +   private CacheLoader   loader=null;
  +   private CacheListener listener = null;
  +   
  +   protected void setUp() throws Exception
  +   {
  +      super.setUp();
  +      log.debug("Testing " + getName());
  +      log.debug("");
  +      cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
  +      cache.getConfiguration().setCacheMode("local");
  +      configureEviction();
  +      configureCacheLoader();
  +      cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
  +      cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
  +      listener = new CacheListener();
  +      cache.addCacheListener(listener);
  +      cache.create();
  +      cache.start();
  +      loader = cache.getCacheLoaderManager().getCacheLoader();
  +   }
  +   
  +   protected void configureCacheLoader() throws Exception 
  +   {
  +      String tmp_location = "/tmp/fcl";
  +      String OS = System.getProperty("os.name").toLowerCase();      
  +      if(OS.indexOf("win") > -1 || OS.indexOf("nt") > -1) 
  +      {
  +         tmp_location= "c:\\tmp\\fcl";
  +      } 
  +      
  +      tmp_location=System.getProperty("java.io.tmpdir", tmp_location);
  +      
  +      
  +      CacheLoaderConfig clc = new CacheLoaderConfig();
  +      clc.setPassivation(true);
  +      clc.setShared(false);
  +      clc.setPreload("/");
  +      FileCacheLoaderConfig fclc = new FileCacheLoaderConfig();
  +      fclc.setAsync(false);
  +      fclc.setFetchPersistentState(true);
  +      fclc.setIgnoreModifications(false);
  +      fclc.setLocation(tmp_location);
  +      
  +      clc.addIndividualCacheLoaderConfig(fclc);
  +
  +      cache.getConfiguration().setCacheLoaderConfig(clc);
  +   }
  +
  +   protected void configureEviction() throws Exception
  +   {
  +      EvictionConfig ec = new EvictionConfig();
  +      ec.setWakeupIntervalSeconds(1);
  +      
  +      List<EvictionRegionConfig> ercs = new ArrayList<EvictionRegionConfig>();
  +      
  +      EvictionRegionConfig erc = new EvictionRegionConfig();
  +      erc.setRegionFqn(RegionManager.DEFAULT_REGION);
  +      LRUConfiguration epc = new LRUConfiguration();
  +      epc.setMaxNodes(0);
  +      epc.setTimeToLiveSeconds(5);
  +      erc.setEvictionPolicyConfig(epc);
  +      ercs.add(erc);
  +      
  +      erc = new EvictionRegionConfig();
  +      erc.setRegionFqn(BASE);
  +      epc = new LRUConfiguration();
  +      epc.setMaxNodes(0);
  +      epc.setTimeToLiveSeconds(1);
  +      erc.setEvictionPolicyConfig(epc);
  +      ercs.add(erc);
  +      
  +      ec.setEvictionRegionConfigs(ercs);
  +      
  +      cache.getConfiguration().setEvictionConfig(ec);
  +   }
  +   
  +   protected void tearDown() throws Exception
  +   {
  +      super.tearDown();
  +      cache.removeNode(Fqn.ROOT);
  +      loader.remove(Fqn.fromString("/"));
  +      cache.stop();
  +      cache.destroy();
  +   }
  +   
  +   public void testSimpleLifecycle() throws Exception
  +   {
  +      Fqn fqn = new Fqn(BASE, "bean1");
  +      cache.put(fqn, "bean", "A bean");
  +      
  +      TestingUtil.sleepThread(3000);
  +      
  +      assertNull("No activation exception", listener.activationException);
  +      assertNull("No passivation exception", listener.passivationException);
  +      assertTrue(listener.passivated.contains(fqn));
  +      assertFalse(listener.activated.contains(fqn));
  +      
  +      Object obj = cache.get(fqn, "bean");
  +      assertEquals("Got bean", "A bean", obj);
  +      
  +      assertNull("No activation exception", listener.activationException);
  +      assertNull("No passivation exception", listener.passivationException);
  +      assertTrue(listener.activated.contains(fqn));
  +   }
  +   
  +   /**
  +    * Mimics the TreeCacheListener used by EJB3 SFSBs.
  +    */
  +   private class CacheListener extends AbstractCacheListener
  +   {     
  +      protected Log log = LogFactory.getLog(CacheListener.class);
  +
  +      protected Set passivated = new HashSet();
  +      protected Set activated = new HashSet();
  +      protected Exception passivationException;
  +      protected Exception activationException;
  +      
  +      @Override
  +      public void nodeActivated(Fqn fqn, boolean pre) 
  +      {
  +         if(pre) 
  +            return;  // we are not interested in postActivate event
  +         
  +         if(!fqn.isChildOrEquals(BASE)) return;  // don't care about fqn that doesn't belong to me.
  +
  +         Object bean = null;
  +         try {
  +            bean = cache.get(fqn, "bean");
  +         } catch (CacheException e) {
  +            log.error("nodeActivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
  +            activationException = e;
  +            return;
  +         }
  +         if(bean == null)
  +         {
  +            activationException = new IllegalStateException("nodeActivate(): null bean instance.");
  +            throw (IllegalStateException) activationException;
  +         }
  +
  +         if(log.isTraceEnabled())
  +         {
  +            log.trace("nodeActivate(): saw postActivate event on fqn: " +fqn);
  +         }
  +
  +         activated.add(fqn);
  +      }
  +
  +      @Override
  +      public void nodePassivated(Fqn fqn, boolean pre) {
  +         if(!pre) return;  // we are not interested in postPassivate event
  +         if(!fqn.isChildOrEquals(BASE)) return;  // don't care about fqn that doesn't belong to me.
  +
  +         try {
  +            // TODO Can this cause deadlock in the cache level? Should be ok but need review.
  +            Object bean = cache.get(fqn, "bean");
  +            if (bean != null)
  +            {
  +               if(log.isTraceEnabled())
  +               {
  +                  log.trace("nodePassivate(): send prePassivate event on fqn: " +fqn);
  +               }
  +               passivated.add(fqn);
  +            }
  +
  +         } catch (CacheException e) {
  +            log.error("nodePassivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
  +            passivationException = e;
  +            return;
  +         }
  +
  +      }
  +   }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list