package org.jboss.cache.loader;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.cache.loader.tcp.TcpCacheServer;
import org.jboss.cache.misc.TestingUtil;

/**
 * Tests the TcpDelegatingCacheLoader
 *
 * @author Bela Ban
 * @version $Id: TcpCacheLoaderTest.java 2741 2006-10-25 14:40:48Z msurtani $
 */
public class TcpCacheLoaderTest extends CacheLoaderTestsBase
{
   static TcpCacheServer cache_server = null;

   static
   {
      Thread runner = new Thread()
      {
         public void run()
         {
            try
            {
               System.out.println("Starting TcpCacheServer");
               cache_server = new TcpCacheServer();
               cache_server.setBindAddress("127.0.0.1");
               cache_server.setPort(12121);
               cache_server.setConfig("META-INF/local-service.xml"); // must be in classpath (./etc/META-INF)
               cache_server.createService();
               cache_server.startService();
            }
            catch (Exception ex)
            {
               ex.printStackTrace();
            }
         }
      };

      Runtime.getRuntime().addShutdownHook(new Thread()
      {
         public void run()
         {
            if (cache_server != null)
            {
               System.out.println("Stopping TcpCacheServer");
               cache_server.stopService();
            }
         }
      });

      runner.start();
      // sleep for half a sec to give the server thread time to start listening.
      TestingUtil.sleepThread(500);
   }


   protected void configureCache() throws Exception
   {
      cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("",
              "org.jboss.cache.loader.tcp.TcpDelegatingCacheLoader",
              "host=127.0.0.1\nport=12121", false, true, false));
   }


   public static Test suite()
   {
      return new TestSuite(TcpCacheLoaderTest.class);
   }


   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(suite());
   }

}