/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.eviction;

import junit.framework.TestCase;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.lock.IsolationLevel;

/**
 * Unit tests for MRUPolicy.
 *
 * @author Daniel Huang (dhuang@jboss.org)
 * @version $Revision: 1838 $
 */
public class MRUPolicyTest extends TestCase
{
   TreeCache cache;
   int wakeupIntervalMillis = 0;
   final String ROOT_STR = "/test";
   Throwable t1_ex, t2_ex;
   final long DURATION = 10000;
   boolean isTrue;

   public void setUp() throws Exception
   {
      super.setUp();
      System.out.println("Test " + getName() + "\n");
      initCaches();
      wakeupIntervalMillis = cache.getEvictionThreadWakeupIntervalSeconds() * 1000;
      log("wakeupInterval is " + wakeupIntervalMillis);
      if (wakeupIntervalMillis < 0)
         fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);

      t1_ex = t2_ex = null;
      isTrue = true;
   }

   public void tearDown() throws Exception
   {
      super.tearDown();
      cache.stopService();
   }

   private void initCaches() throws Exception
   {
      cache = new TreeCache();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache, "META-INF/local-mru-eviction-service.xml"); // read in generic local xml
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
      cache.startService();
   }

   public void testEviction() throws Exception
   {
      cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a");
      cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b");
      cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c");
      cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d");
      cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e");

      TestingUtil.sleepThread(wakeupIntervalMillis + 500);

      System.out.println(cache.toString(true));

      cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f");
      cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g");
      cache.put("/org/jboss/test/data/h", "/org/jboss/test/data/h", "/org/jboss/test/data/h");
      assertNotNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a"));
      assertNotNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b"));

      TestingUtil.sleepThread(wakeupIntervalMillis + 500);

      System.out.println(cache.toString(true));

      assertNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a"));
      assertNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b"));
   }

   public void testNodeRemoved() throws Exception
   {
      cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a");
      cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b");
      cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c");
      cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d");
      cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e");

      TestingUtil.sleepThread(wakeupIntervalMillis + 500);

      cache.remove("/org/jboss/test/data/d");
      cache.remove("/org/jboss/test/data/e");
      cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f");
      cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g");

      TestingUtil.sleepThread(wakeupIntervalMillis + 500);

      assertNull(cache.get("/org/jboss/test/data/d", "/org/jboss/test/data/d"));
      assertNull(cache.get("/org/jboss/test/data/e", "/org/jboss/test/data/e"));

      assertNotNull(cache.get("/org/jboss/test/data/f", "/org/jboss/test/data/f"));
      assertNotNull(cache.get("/org/jboss/test/data/g", "/org/jboss/test/data/g"));
   }

   class MyPutter extends Thread
   {

      public MyPutter(String name)
      {
         super(name);
      }

      public void run()
      {
         int i = 0;
         final String myName = ROOT_STR + "/test1/node" + getName();
         while (isTrue)
         {
            try
            {
               cache.put(myName + i++, "value", new Integer(i));
               TestingUtil.sleepThread(1);
            }
            catch (Throwable e)
            {
               e.printStackTrace();
               if (t1_ex == null)
                  t1_ex = e;
            }
         }
      }
   }


   public void testConcurrentPutAndEvict() throws Exception
   {
      cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
      cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", new Integer(1));

      for (int i = 0; i < 10; i++)
      {
         new MyPutter("Putter" + i).start();
      }

      int counter = 0;
      while (true)
      {
         counter++;
         if (t1_ex != null)
         {
            fail("Exception generated in put() " + t1_ex);
         }
         log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
         TestingUtil.sleepThread(1000);
         if (counter > 10)
         { // run for 10 seconds
            isTrue = false;
            break;
         }
      }
   }

   private void log(String s)
   {
      System.out.println(s);
   }
}
