1 7 package org.jboss.cache.eviction; 8 9 import junit.framework.TestCase; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.jboss.cache.CacheImpl; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.config.Configuration; 15 import org.jboss.cache.config.EvictionConfig; 16 import org.jboss.cache.misc.TestingUtil; 17 18 24 public class ExpirationPolicyTest extends TestCase 25 { 26 private static final Log log = LogFactory.getLog(ExpirationPolicyTest.class); 27 28 private CacheImpl cache; 29 30 Fqn fqn1 = Fqn.fromString("/node/1"); 31 Fqn fqn2 = Fqn.fromString("/node/2"); 32 Fqn fqn3 = Fqn.fromString("/node/3"); 33 Fqn fqn4 = Fqn.fromString("/node/4"); 34 35 Long future; 36 Long past; 37 38 public void setUp() throws Exception 39 { 40 super.setUp(); 41 cache = new CacheImpl(); 42 Configuration conf = new Configuration(); 43 EvictionConfig econf = new EvictionConfig(ExpirationPolicy.class.getName()); 44 econf.setWakeupIntervalSeconds(1); 45 conf.setEvictionConfig(econf); 46 cache.setConfiguration(conf); 47 cache.start(); 48 49 future = System.currentTimeMillis() + 4000; 50 past = System.currentTimeMillis() - 2000; 51 } 52 53 public void tearDown() throws Exception 54 { 55 super.tearDown(); 56 cache.stop(); 57 } 58 59 public void testEviction() throws Exception 60 { 61 cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future); 62 cache.put(fqn2, ExpirationConfiguration.EXPIRATION_KEY, past); 63 cache.put(fqn3, ExpirationConfiguration.EXPIRATION_KEY, future); 64 cache.put(fqn4, "foo", "bar"); 65 TestingUtil.sleepThread(2000); 66 assertNotNull(cache.get(fqn1)); 67 assertNull(cache.get(fqn2)); 68 assertNotNull(cache.get(fqn3)); 69 assertNotNull(cache.get(fqn4)); 70 71 log.info("should remove 1 and 3 now"); 72 TestingUtil.sleepThread(3000); 73 assertNull(cache.get(fqn1)); 74 assertNull(cache.get(fqn3)); 75 } 76 77 public void testUpdate() throws Exception 78 { 79 log.info("update 1 from future to past"); 80 cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future); 81 assertNotNull(cache.get(fqn1)); 82 TestingUtil.sleepThread(1500); 83 cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, past); 84 TestingUtil.sleepThread(1500); 85 assertNull(cache.get(fqn1)); 86 cache.remove(Fqn.ROOT); 87 88 } 89 90 public void testMaxNodes() throws Exception 91 { 92 log.info("set max nodes to 2, expire soonest to expire first"); 93 EvictionPolicyConfig epc = cache.getRegionManager().getAllEvictionRegions().get(0).getEvictionPolicyConfig(); 94 ExpirationConfiguration ec = (ExpirationConfiguration) epc; 95 ec.setMaxNodes(2); 96 Long future2 = future + 500; 97 cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future2); 98 cache.put(fqn2, ExpirationConfiguration.EXPIRATION_KEY, future2); 99 cache.put(fqn3, ExpirationConfiguration.EXPIRATION_KEY, future); 100 cache.put(fqn4, ExpirationConfiguration.EXPIRATION_KEY, past); 101 assertEquals(5, cache.getNumberOfNodes()); 102 Thread.sleep(2000); 103 assertNotNull(cache.get(fqn1)); 104 assertNotNull(cache.get(fqn2)); 105 assertNull(cache.get(fqn3)); 106 assertNull(cache.get(fqn4)); 107 } 108 109 } 110 | Popular Tags |