/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.passivation;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.loader.DummyInMemoryCacheLoader;
import org.jboss.cache.loader.AbstractCacheLoaderTestBase;

import java.io.File;

/**
 * Tests cache behavior in the presence of concurrent passivation.
 *
 * @author Brian Stansberry
 * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 * @version $Revision: 4089 $
 */
public class ConcurrentPassivationTest extends AbstractCacheLoaderTestBase
{
   private TreeCache cache_;
   private int wakeupIntervalMillis_ = 0;

   public ConcurrentPassivationTest(String s)
   {
      super(s);
   }

   public void setUp() throws Exception
   {
      super.setUp();
      initCaches();
      wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000;
      if (wakeupIntervalMillis_ < 0)
         fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);

   }

   void initCaches() throws Exception
   {
      cache_ = new TreeCache();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache_, "META-INF/local-passivation-service.xml"); // read in generic local xml
      cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache_.setCacheLoaderConfiguration(getSingleCacheLoaderConfig(true, "/", DummyInMemoryCacheLoader.class.getName(), null, false, true, false, false));
      cache_.startService();
   }

   public void tearDown() throws Exception
   {
      super.tearDown();
      cache_.stop();
      cache_ = null;
   }

   public void testConcurrentPassivation() throws Exception
   {
      Fqn base = Fqn.fromString("/org/jboss/test/data/concurrent/passivation");

      // Create a bunch of nodes; more than the /org/jboss/test/data
      // region's maxNodes so we know eviction will kick in
      for (int i = 0; i < 35000; i++)
      {
         cache_.put(new Fqn(base, new Integer(i / 100)), new Integer(i), "value");
      }

      // Loop for long enough to have 5 runs of the eviction thread
      long loopDone = System.currentTimeMillis() + (5 * wakeupIntervalMillis_);
      while (System.currentTimeMillis() < loopDone)
      {
         // If any get returns null, that's a failure
         for (int i = 0; i < 35000; i++)
         {
            Fqn fqn = new Fqn(base, new Integer(i / 100));
            Integer key = new Integer(i);
            assertNotNull("found value under Fqn " + fqn + " and key " + key,
                    cache_.get(fqn, key));
         }
      }
   }

   public static Test suite()
   {
      return new TestSuite(org.jboss.cache.passivation.ConcurrentPassivationTest.class);
   }

   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(org.jboss.cache.passivation.ConcurrentPassivationTest.suite());
   }
}
