package org.jgroups.tests;

import org.jgroups.Global;
import org.jgroups.util.AgeOutCache;
import org.jgroups.util.Util;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Test cases for AgeOutCache
 * 
 * @author Bela Ban
 * @version $Id: AgeOutCacheTest.java,v 1.3 2009/06/03 07:48:07 vlada Exp $
 */
@Test(groups = Global.FUNCTIONAL, sequential = false)
public class AgeOutCacheTest {
   ScheduledExecutorService timer;

   @BeforeClass
   void start() throws Exception {
      timer = Executors.newScheduledThreadPool(5);
   }

   @AfterClass
   void stop() throws Exception {
      timer.shutdownNow();
   }

   public void testExpiration() {
      AgeOutCache<Integer> cache = new AgeOutCache<Integer>(timer, 1000L,
               new AgeOutCache.Handler<Integer>() {
                  public void expired(Integer key) {
                     System.out.println(key + " expired");
                  }
               });

      for (int i = 1; i <= 5; i++)
         cache.add(i);
      System.out.println("cache:\n" + cache);
      assert cache.size() == 5;
      Util.sleep(1300);
      System.out.println("cache:\n" + cache);
      assert cache.size() == 0;
   }

   public void testRemoveAndExpiration() {
      AgeOutCache<Integer> cache = new AgeOutCache<Integer>(timer, 1000L);
      for (int i = 1; i <= 5; i++)
         cache.add(i);
      System.out.println("cache:\n" + cache);
      Util.sleep(500);
      cache.remove(3);
      cache.remove(5);
      cache.remove(6); // not existent
      System.out.println("cache:\n" + cache);
      assert cache.size() == 3;
      Util.sleep(700);
      assert cache.size() == 0;
   }

   public void testGradualExpiration() {
      AgeOutCache<Integer> cache = new AgeOutCache<Integer>(timer, 1000L);
      for (int i = 1; i <= 10; i++) {
         cache.add(i);
         System.out.print(".");
         Util.sleep(300);
      }
      System.out.println("\ncache:\n" + cache);
      int size = cache.size();
      assert size < 10 && size > 0;
   }
}
@@ -430,17 +430,17 @@
 ava,v 1.
-3
+4
  2009/06
@@ -449,18 +449,18 @@
 3 07:48:
-07
+59
  vlada E
@@ -519,12 +519,11 @@
 l = 
-fals
+tru
 e)%0Ap

make it sequential