Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 347   Methods: 11
NCLOC: 281   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LFUPolicyTest.java 92.6% 91% 100% 91.9%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.eviction;
 8   
 9    import junit.framework.TestCase;
 10    import org.jboss.cache.CacheImpl;
 11    import org.jboss.cache.DefaultCacheFactory;
 12    import org.jboss.cache.Fqn;
 13    import org.jboss.cache.lock.IsolationLevel;
 14    import org.jboss.cache.misc.TestingUtil;
 15   
 16    /**
 17    * Unit tests for LFU Policy.
 18    *
 19    * @author Daniel Huang (dhuang@jboss.org)
 20    * @version $Revision: 1.11 $
 21    */
 22    public class LFUPolicyTest extends TestCase
 23    {
 24    CacheImpl cache;
 25    int wakeupIntervalMillis = 0;
 26    final String ROOT_STR = "/test";
 27    Throwable t1_ex, t2_ex;
 28    final long DURATION = 10000;
 29    boolean isTrue;
 30   
 31  4 public LFUPolicyTest(String s)
 32    {
 33  4 super(s);
 34    }
 35   
 36  4 public void setUp() throws Exception
 37    {
 38  4 super.setUp();
 39  4 initCaches();
 40  4 wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
 41  4 log("wakeupInterval is " + wakeupIntervalMillis);
 42  4 if (wakeupIntervalMillis < 0)
 43    {
 44  0 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);
 45    }
 46   
 47  4 t1_ex = t2_ex = null;
 48  4 isTrue = true;
 49    }
 50   
 51  4 void initCaches() throws Exception
 52    {
 53  4 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-lfu-eviction-service.xml", false);
 54  4 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 55  4 cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
 56  4 cache.start();
 57    }
 58   
 59  4 public void tearDown() throws Exception
 60    {
 61  4 super.tearDown();
 62  4 cache.stop();
 63    }
 64   
 65  1 public void testEviction() throws Exception
 66    {
 67  1 String rootStr = "/org/jboss/data/";
 68  1 for (int i = 0; i < 8000; i++)
 69    {
 70  8000 String str = rootStr + i;
 71  8000 Fqn fqn = Fqn.fromString(str);
 72  8000 try
 73    {
 74  8000 cache.put(fqn, str, str);
 75    }
 76    catch (Exception e)
 77    {
 78  0 fail("Failed to insert data" + e);
 79  0 e.printStackTrace();
 80    }
 81   
 82    // visit odd numbered nodes an extra time to make them get evicted last.
 83  8000 if (i % 2 != 0)
 84    {
 85  4000 cache.get(fqn, str);
 86    }
 87    }
 88   
 89  1 long period = wakeupIntervalMillis + 500;
 90  1 TestingUtil.sleepThread(period);
 91   
 92  1 for (int i = 0; i < 8000; i++)
 93    {
 94  8000 String str = rootStr + i;
 95  8000 Fqn fqn = Fqn.fromString(str);
 96   
 97  8000 if (i % 2 == 0)
 98    {
 99  4000 assertNull(cache.get(fqn, str));
 100    }
 101    else
 102    {
 103  4000 assertNotNull(cache.get(fqn, str));
 104    }
 105    }
 106    }
 107   
 108  1 public void testNodeVisited()
 109    {
 110  1 String rootStr = "/org/jboss/test/data/";
 111  1 for (int i = 0; i < 10; i++)
 112    {
 113  10 String str = rootStr + i;
 114  10 Fqn fqn = Fqn.fromString(str);
 115  10 try
 116    {
 117  10 cache.put(fqn, str, str);
 118    }
 119    catch (Exception e)
 120    {
 121  0 fail("Failed to insert data" + e);
 122  0 e.printStackTrace();
 123    }
 124    }
 125  1 System.out.println("cache is:\n" + cache.toString(true));
 126   
 127  1 int period = wakeupIntervalMillis + 500;
 128   
 129  1 log("sleeping for " + period + "ms");
 130  1 TestingUtil.sleepThread(period);// it really depends the eviction thread time.
 131   
 132  1 System.out.println("cache is:\n" + cache.toString(true));
 133   
 134  1 try
 135    {
 136  1 for (int i = 0; i < 5; i++)
 137    {
 138  5 String str = rootStr + Integer.toString(i);
 139  5 Fqn fqn = Fqn.fromString(str);
 140  5 assertNull(cache.get(fqn, str));
 141    }
 142  1 for (int i = 5; i < 10; i++)
 143    {
 144  5 String str = rootStr + Integer.toString(i);
 145  5 Fqn fqn = Fqn.fromString(str);
 146  5 assertNotNull(cache.get(fqn, str));
 147    }
 148   
 149  1 TestingUtil.sleepThread(period);
 150   
 151  1 System.out.println(cache.toString(true));
 152   
 153    // since min is 5 the cache won't deplete past 5 nodes left in the cache.
 154  1 for (int i = 5; i < 10; i++)
 155    {
 156  5 String str = rootStr + Integer.toString(i);
 157  5 Fqn fqn = Fqn.fromString(str);
 158  5 assertNotNull(cache.get(fqn, str));
 159    }
 160   
 161    // now we add some more nodes and we selectively visit some older nodes but not all. they should not get
 162    // evicted when the thread next runs.
 163  1 for (int i = 5; i < 7; i++)
 164    {
 165  2 String str = rootStr + Integer.toString(i);
 166  2 Fqn fqn = Fqn.fromString(str);
 167  2 cache.get(fqn, str);
 168    }
 169   
 170    // add 2 more to push the limit to 5 so we cause the old unvisited nodes to get evicted.
 171  1 for (int i = 10; i < 13; i++)
 172    {
 173  3 String str = rootStr + Integer.toString(i);
 174  3 Fqn fqn = Fqn.fromString(str);
 175  3 cache.put(fqn, str, str);
 176   
 177    // now bring up their hit count for LFU purposes (cache hit count based eviction).
 178  3 for (int k = 0; k < 10; k++)
 179    {
 180  30 cache.get(fqn, str);
 181    }
 182    }
 183   
 184  1 TestingUtil.sleepThread(period);
 185   
 186  1 System.out.println(cache.toString(true));
 187   
 188    // now make sure we still only have 5 nodes and they are the ones we expect based on LFU
 189  1 for (int i = 5; i < 7; i++)
 190    {
 191  2 String str = rootStr + Integer.toString(i);
 192  2 Fqn fqn = Fqn.fromString(str);
 193  2 assertNotNull(cache.get(fqn, str));
 194    }
 195   
 196  1 for (int i = 7; i < 10; i++)
 197    {
 198  3 String str = rootStr + Integer.toString(i);
 199  3 Fqn fqn = Fqn.fromString(str);
 200  3 assertNull(cache.get(fqn, str));
 201    }
 202   
 203  1 for (int i = 10; i < 13; i++)
 204    {
 205  3 String str = rootStr + Integer.toString(i);
 206  3 Fqn fqn = Fqn.fromString(str);
 207  3 assertNotNull(cache.get(fqn, str));
 208    }
 209   
 210    }
 211    catch (Exception e)
 212    {
 213  0 e.printStackTrace();
 214  0 fail("Failed to evict" + e);
 215    }
 216    }
 217   
 218  1 public void testNodeRemoved() throws Exception
 219    {
 220  1 String rootStr = "/org/jboss/data/";
 221  1 for (int i = 0; i < 5000; i++)
 222    {
 223  5000 String str = rootStr + i;
 224  5000 Fqn fqn = Fqn.fromString(str);
 225  5000 try
 226    {
 227  5000 cache.put(fqn, str, str);
 228    }
 229    catch (Exception e)
 230    {
 231  0 fail("Failed to insert data" + e);
 232  0 e.printStackTrace();
 233    }
 234    }
 235   
 236  1 int period = (wakeupIntervalMillis + 500);
 237  1 log("period is " + period);
 238  1 TestingUtil.sleepThread(period);
 239   
 240  1 for (int i = 0; i < 1000; i++)
 241    {
 242  1000 String str = rootStr + i;
 243  1000 Fqn fqn = Fqn.fromString(str);
 244  1000 assertNull(cache.get(fqn, str));
 245    }
 246   
 247  1 for (int i = 1000; i < 5000; i++)
 248    {
 249  4000 String str = rootStr + i;
 250  4000 Fqn fqn = Fqn.fromString(str);
 251  4000 assertNotNull(cache.get(fqn, str));
 252    }
 253   
 254  1 for (int i = 1000; i < 5000; i++)
 255    {
 256  4000 if (i % 2 == 0)
 257    {
 258  2000 String str = rootStr + i;
 259  2000 Fqn fqn = Fqn.fromString(str);
 260  2000 cache.remove(fqn);
 261    }
 262    }
 263   
 264  1 TestingUtil.sleepThread(period);
 265   
 266   
 267  1 for (int i = 1000; i < 5000; i++)
 268    {
 269  4000 if (i % 2 == 0)
 270    {
 271  2000 String str = rootStr + i;
 272  2000 Fqn fqn = Fqn.fromString(str);
 273  2000 assertNull(cache.get(fqn));
 274    }
 275    }
 276    }
 277   
 278   
 279    class MyPutter extends Thread
 280    {
 281   
 282  10 public MyPutter(String name)
 283    {
 284  10 super(name);
 285    }
 286   
 287  10 public void run()
 288    {
 289  10 int i = 0;
 290  10 final String myName = ROOT_STR + "/test1/node" + getName();
 291  10 while (isTrue)
 292    {
 293  13446 try
 294    {
 295  13446 cache.put(myName + i++, "value", i);
 296  13446 sleep(1);
 297    }
 298    catch (Throwable e)
 299    {
 300  0 e.printStackTrace();
 301  0 if (t1_ex == null)
 302    {
 303  0 t1_ex = e;
 304    }
 305    }
 306    }
 307    }
 308    }
 309   
 310   
 311  1 public void testConcurrentPutAndEvict() throws Exception
 312    {
 313  1 cache.stop();
 314  1 cache.destroy();
 315  1 cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
 316  1 cache.start();
 317  1 cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1);
 318   
 319  1 for (int i = 0; i < 10; i++)
 320    {
 321  10 new MyPutter("Putter" + i).start();
 322    }
 323   
 324  1 int counter = 0;
 325  1 while (true)
 326    {
 327  11 counter++;
 328  11 if (t1_ex != null)
 329    {
 330  0 fail("Exception generated in put() " + t1_ex);
 331    }
 332  11 log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
 333  11 TestingUtil.sleepThread(1000);
 334  11 if (counter > 10)
 335    {// run for 10 seconds
 336  1 isTrue = false;
 337  1 break;
 338    }
 339    }
 340    }
 341   
 342  17 void log(String msg)
 343    {
 344  17 System.out.println("-- " + msg);
 345    }
 346   
 347    }