HungChannelTest.java :  » JBoss » JBossCache » org » jboss » cache » Java Open Source

Java Open Source » JBoss » JBossCache 
JBossCache » org » jboss » cache » HungChannelTest.java
package org.jboss.cache;

import junit.framework.TestCase;
import org.jgroups.Address;
import org.jgroups.Channel;
import org.jgroups.ChannelException;
import org.jgroups.JChannel;
import org.jgroups.conf.ProtocolStackConfigurator;
import org.w3c.dom.Element;

import java.io.File;
import java.net.URL;

/**
 * Tests the behaviour of starting a cache when the JGroups channel is caused to hang.
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 */
public class HungChannelTest extends TestCase
{
   private CacheImpl cache;

   protected void setUp() throws Exception
   {
      cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
      cache.getConfiguration().setCacheMode("REPL_SYNC");
   }

   protected void tearDown()
   {
      if (cache != null)
      {
         if (cache.channel != null)
         {
            cache.channel.close();
            cache.channel.disconnect();
            cache.channel = null;
         }
         cache.stop();
         cache = null;
      }
   }

   public void testFailingStateTransfer()
   {
      try
      {
         cache.create();
         JChannel ch = new FailingStateChannel(cache.getConfiguration().getClusterConfig());
         ch.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE);
         ch.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
         ch.setOpt(Channel.AUTO_GETSTATE, Boolean.TRUE);
         cache.getConfiguration().getRuntimeConfig().setChannel(ch);

         cache.start();// the state transfer here should fail, leading to an exception being thrown.
         fail("Expecting the startService() method to throw an exception");
      }
      catch (Exception e)
      {
         // normal behaviour
      }

      Channel c = cache.channel;// hold a reference to this since stopService will set this to null in the cache.

      assertFalse("Channel should not have connected!", c.isConnected());
      assertFalse("Channel should not be open", c.isOpen());

      cache.stop();

      assertFalse("Channel should not have connected!", c.isConnected());
      assertFalse("Channel should not be open", c.isOpen());

      assertNull("Should be null", cache.channel);
   }

   public void testSucceedingStateTransfer()
   {
      try
      {
         cache.start();
      }
      catch (Exception e)
      {
         fail("NOT expecting the startService() method to throw an exception");
      }

      Channel c = cache.channel;// hold a reference to this since stopService will set this to null in the cache.

      assertTrue("Channel should have connected!", c.isConnected());
      assertTrue("Channel should be open", c.isOpen());


      cache.stop();

      assertFalse("Channel should not have connected!", c.isConnected());
      assertFalse("Channel should not be open", c.isOpen());

      assertNull("Should be null", cache.channel);
   }

   static class FailingStateChannel extends JChannel
   {


      public FailingStateChannel() throws ChannelException
      {
         super();
      }

      public FailingStateChannel(File file) throws ChannelException
      {
         super(file);
      }

      public FailingStateChannel(Element element) throws ChannelException
      {
         super(element);
      }

      public FailingStateChannel(URL url) throws ChannelException
      {
         super(url);
      }

      public FailingStateChannel(String string) throws ChannelException
      {
         super(string);
      }

      public FailingStateChannel(ProtocolStackConfigurator protocolStackConfigurator) throws ChannelException
      {
         super(protocolStackConfigurator);
      }

      public boolean getState(Address a, long l)
      {
         throw new RuntimeException("Dummy Exception getting state");
      }
   }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.