[jbosscache-commits] JBoss Cache SVN: r5477 - core/trunk/src/test/java/org/jboss/cache/eviction.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Mon Mar 31 11:47:52 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-03-31 11:47:52 -0400 (Mon, 31 Mar 2008)
New Revision: 5477

Modified:
   core/trunk/src/test/java/org/jboss/cache/eviction/LRUPolicyTest.java
Log:
JBCACHE-1315 - Cleaned up unit tests - removed arbitrary sleep times to work around the imprecision described in JBCACHE-1315

Modified: core/trunk/src/test/java/org/jboss/cache/eviction/LRUPolicyTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/LRUPolicyTest.java	2008-03-28 17:22:39 UTC (rev 5476)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/LRUPolicyTest.java	2008-03-31 15:47:52 UTC (rev 5477)
@@ -30,6 +30,9 @@
 {
    CacheSPI<Object, Object> cache;
    int wakeupIntervalMillis_ = 0;
+   int dataRegionTTLMillis = 6000;
+   int testRegionTTLMillis = 4000;
+
    final String ROOT_STR = "/test";
    Throwable t1_ex, t2_ex;
    final long DURATION = 10000;
@@ -38,9 +41,19 @@
    @BeforeMethod(alwaysRun = true)
    public void setUp() throws Exception
    {
-      initCaches();
+      Configuration conf = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.LOCAL, true);
+      EvictionConfig evConfig = conf.getEvictionConfig();
+      evConfig.setWakeupIntervalSeconds(1);
+      List<EvictionRegionConfig> regionConfigs = new ArrayList<EvictionRegionConfig>();
+      regionConfigs.add(UnitTestCacheConfigurationFactory.buildLruEvictionRegionConfig("/org/jboss/test/data", 5, dataRegionTTLMillis / 1000));
+      regionConfigs.add(UnitTestCacheConfigurationFactory.buildLruEvictionRegionConfig("/test", 10000, testRegionTTLMillis / 1000));
+      evConfig.setEvictionRegionConfigs(regionConfigs);
+      conf.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
+      conf.setIsolationLevel(IsolationLevel.SERIALIZABLE);
+      cache = (CacheSPI<Object, Object>) new DefaultCacheFactory<Object, Object>().createCache(conf);
+
       wakeupIntervalMillis_ = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
-      log("wakeupInterval is " + wakeupIntervalMillis_);
+      System.out.println("-- wakeupInterval is " + wakeupIntervalMillis_);
       if (wakeupIntervalMillis_ < 0)
       {
          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
@@ -50,21 +63,6 @@
       isTrue = true;
    }
 
-   public void initCaches() throws Exception
-   {
-      Configuration conf = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.LOCAL, true);
-      EvictionConfig evConfig = conf.getEvictionConfig();
-      evConfig.setWakeupIntervalSeconds(5);
-      List<EvictionRegionConfig> regionConfigs = new ArrayList<EvictionRegionConfig>();
-      regionConfigs.add(UnitTestCacheConfigurationFactory.buildLruEvictionRegionConfig("/org/jboss/test/data", 5, 6));
-      regionConfigs.add(UnitTestCacheConfigurationFactory.buildLruEvictionRegionConfig("/test", 10000, 4));
-      evConfig.setEvictionRegionConfigs(regionConfigs);
-      conf.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
-      conf.setIsolationLevel(IsolationLevel.SERIALIZABLE);
-      cache = (CacheSPI<Object, Object>) new DefaultCacheFactory<Object, Object>().createCache(conf);
-   }
-
-
    @AfterMethod(alwaysRun = true)
    public void tearDown() throws Exception
    {
@@ -75,8 +73,6 @@
    {
       String rootStr = "/org/jboss/test/data/inuse/";
       Fqn fqn;
-      //      cache_.put("/", "a", "b");
-      //      TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
 
       for (int i = 0; i < 10; i++)
       {
@@ -85,11 +81,9 @@
          cache.put(fqn, str, str);
       }
 
-      System.out.println("***************************** marking as in-use");
+      System.out.println("-- Marking as in-use");
       cache.getRegionManager().getRegion(Fqn.fromString(rootStr + 5), false).markNodeCurrentlyInUse(Fqn.fromString(rootStr + 5), 0);
 
-//      TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
-
       for (int i = 10; i < 15; i++)
       {
          String str = rootStr + i;
@@ -139,16 +133,8 @@
       System.out.println(cache.toString());
       TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
       System.out.println(cache.toString());
-      try
-      {
-         String val = (String) cache.get(rootStr + "3", rootStr + "3");
-         assertNull("DataNode should be empty ", val);
-      }
-      catch (Exception e)
-      {
-         e.printStackTrace();
-         fail("Failed to get" + e);
-      }
+      String val = (String) cache.get(rootStr + "3", rootStr + "3");
+      assertNull("Node should be empty ", val);
    }
 
    public void testNodeVisited()
@@ -161,47 +147,32 @@
       {
          String str = rootStr + i;
          Fqn fqn = Fqn.fromString(str);
-         try
-         {
-            cache.put(fqn, str, str);
-         }
-         catch (Exception e)
-         {
-            fail("Failed to insert data" + e);
-            e.printStackTrace();
-         }
+         cache.put(fqn, str, str);
       }
 
       int period = (wakeupIntervalMillis_ / 2 + 500);
-      log("sleeping for " + period + "ms");
-      TestingUtil.sleepThread(period);// it really depends the eviction thread time.
+      System.out.println("-- sleeping for " + period + "ms");
+      TestingUtil.sleepThread(period);
       String str = rootStr + "7";
       Fqn fqn = Fqn.fromString(str);
-      try
-      {
-         cache.get(fqn, str);// just to keep it fresh
-         System.out.println("-- sleeping for " + period + "ms");
-         TestingUtil.sleepThread(period);// it really depends the eviction thread time.
-         cache.get(fqn, str);// just to keep it fresh
-         System.out.println("-- sleeping for " + period + "ms");
-         TestingUtil.sleepThread(period);// it really depends the eviction thread time.
-         String val = (String) cache.get(rootStr + "3", rootStr + "3");
-         System.out.println("-- val=" + val);
-         assertNull("DataNode should be empty ", val);
-         val = (String) cache.get(rootStr + "7", rootStr + "7");
-         System.out.println("-- val=" + val);
-         assertNotNull("DataNode should not be empty ", val);
-         System.out.println("-- sleeping for " + (wakeupIntervalMillis_ + 1000) + "ms");
-         TestingUtil.sleepThread(wakeupIntervalMillis_ + 1000);
-         val = (String) cache.get(rootStr + "7", rootStr + "7");
-         System.out.println("-- val=" + val);
-         assertNull("DataNode should be empty ", val);
-      }
-      catch (Exception e)
-      {
-         e.printStackTrace();
-         fail("Failed to evict" + e);
-      }
+      cache.get(fqn, str);// just to keep it fresh
+      System.out.println("-- sleeping for " + period + "ms");
+      TestingUtil.sleepThread(period);
+      cache.get(fqn, str);// just to keep it fresh
+      System.out.println("-- sleeping for " + period + "ms");
+      TestingUtil.sleepThread(period);
+      String val = (String) cache.get(rootStr + "3", rootStr + "3");
+      System.out.println("-- val=" + val);
+      assertNull("Node should be empty ", val);
+      val = (String) cache.get(rootStr + "7", rootStr + "7");
+      System.out.println("-- val=" + val);
+      assertNotNull("Node should not be empty ", val);
+      period = dataRegionTTLMillis + wakeupIntervalMillis_ + 500; // this is the TTL for nodes + time for the eviction thread to kick in
+      System.out.println("-- sleeping for " + period + "ms");
+      TestingUtil.sleepThread(period);
+      val = (String) cache.get(rootStr + "7", rootStr + "7");
+      System.out.println("-- val=" + val);
+      assertNull("Node should be empty ", val);
    }
 
    public void testNodeRemoved()
@@ -211,44 +182,28 @@
       {
          String str = rootStr + i + "/" + i;
          Fqn fqn = Fqn.fromString(str);
-         try
-         {
-            cache.put(fqn, str, str);
-         }
-         catch (Exception e)
-         {
-            fail("Failed to insert data" + e);
-            e.printStackTrace();
-         }
+         cache.put(fqn, str, str);
       }
 
       int period = (wakeupIntervalMillis_ / 2 + 500);
-      log("period is " + period);
+      System.out.println("-- period is " + period);
       //      TestingUtil.sleepThread(period);  // it really depends the eviction thread time.
       String str1 = rootStr + "7";
       Fqn fqn1 = Fqn.fromString(str1);
       String str2 = rootStr + "7/7";
       Fqn fqn2 = Fqn.fromString(str2);
-      try
-      {
-         cache.get(fqn1, str1);// just to keep it fresh
-         cache.get(fqn2, str2);// just to keep it fresh
-         TestingUtil.sleepThread(period);// it really depends the eviction thread time.
-         cache.get(fqn1, str1);// just to keep it fresh
-         cache.get(fqn2, str2);// just to keep it fresh
-         TestingUtil.sleepThread(period);// it really depends the eviction thread time.
-         String val = (String) cache.get(rootStr + "7/7", rootStr + "7/7");
-         assertNotNull("DataNode should not be empty ", val);
-         cache.removeNode(fqn1);
-         TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
-         val = (String) cache.get(rootStr + "7/7", rootStr + "7/7");
-         assertNull("DataNode should be empty ", val);
-      }
-      catch (Exception e)
-      {
-         e.printStackTrace();
-         fail("Failed to evict" + e);
-      }
+      cache.get(fqn1, str1);// just to keep it fresh
+      cache.get(fqn2, str2);// just to keep it fresh
+      TestingUtil.sleepThread(period);
+      cache.get(fqn1, str1);// just to keep it fresh
+      cache.get(fqn2, str2);// just to keep it fresh
+      TestingUtil.sleepThread(period);
+      String val = (String) cache.get(rootStr + "7/7", rootStr + "7/7");
+      assertNotNull("Node should not be empty ", val);
+      cache.removeNode(fqn1);
+      TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
+      val = (String) cache.get(rootStr + "7/7", rootStr + "7/7");
+      assertNull("Node should be empty ", val);
    }
 
    public void testCompleteRemoval() throws Exception
@@ -263,8 +218,10 @@
 
       // Give eviction time to run a few times, then confirm parent
       // is completely gone
-      TestingUtil.sleepThread((wakeupIntervalMillis_ * 4) + 100);
-      assertFalse("Parent completely removed", cache.getRoot().hasChild(parent));
+      int period = (wakeupIntervalMillis_ + testRegionTTLMillis) * 2;
+      System.out.println("-- Sleeping for " + period);
+      TestingUtil.sleepThread(period);
+      assertFalse("Parent not completely removed", cache.getRoot().hasChild(parent));
    }
 
 
@@ -322,7 +279,7 @@
          {
             fail("Exception generated in put() " + t1_ex);
          }
-         log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
+         System.out.println("-- nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
          TestingUtil.sleepThread(1000);
          if (counter > 10)
          {// run for 10 seconds
@@ -334,68 +291,36 @@
 
    public void testForEvictionInternalError()
    {
-      try
+      String rootStr = "/test/testdata";
+
+      for (int i = 0; i < 10; i++)
       {
-         //       String rootStr = "org/jboss/test/data/";
-         String rootStr = "/test/testdata";
+         String str = rootStr + i;
+         Fqn fqn = Fqn.fromString(str);
+         cache.put(fqn, str, str);
+      }
 
-         for (int i = 0; i < 10; i++)
-         {
-            String str = rootStr + i;
-            Fqn fqn = Fqn.fromString(str);
-            try
-            {
-               cache.put(fqn, str, str);
-            }
-            catch (Exception e)
-            {
-               fail("Failed to insert data" + e);
-               e.printStackTrace();
-            }
-         }
+      // wait for an eviction
+      TestingUtil.sleepThread(2 * (wakeupIntervalMillis_ + testRegionTTLMillis));
 
-         // wait for an eviction
-         TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 2000);
+      String val = (String) cache.get(rootStr + "3", rootStr + "3");
+      assertNull("Node should be empty ", val);
 
-         String val = (String) cache.get(rootStr + "3", rootStr + "3");
-         assertNull("DataNode should be empty ", val);
-
-         // reinsert the elements
-         for (int i = 0; i < 10; i++)
-         {
-            String str = rootStr + i;
-            Fqn fqn = Fqn.fromString(str);
-            try
-            {
-               cache.put(fqn, str, str);
-            }
-            catch (Exception e)
-            {
-               fail("Failed to insert data" + e);
-               e.printStackTrace();
-            }
-         }
-
-         // clear the root
-         cache.removeNode(Fqn.ROOT);
-
-         // wait for an eviction
-         TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 1000);
-
-         val = (String) cache.get(rootStr + "3", rootStr + "3");
-         assertNull("DataNode should be empty ", val);
-
-      }
-      catch (Exception e)
+      // reinsert the elements
+      for (int i = 0; i < 10; i++)
       {
-         e.printStackTrace();
-         fail("Failed to get" + e);
+         String str = rootStr + i;
+         Fqn fqn = Fqn.fromString(str);
+         cache.put(fqn, str, str);
       }
-   }
 
-   void log(String msg)
-   {
-      System.out.println("-- " + msg);
-   }
+      // clear the root
+      cache.removeNode(Fqn.ROOT);
 
+      // wait for an eviction
+      TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 1000);
+
+      val = (String) cache.get(rootStr + "3", rootStr + "3");
+      assertNull("Node should be empty ", val);
+   }
 }




More information about the jbosscache-commits mailing list