import junit.framework.TestCase; import org.infinispan.Cache; import org.infinispan.config.Configuration; import org.infinispan.eviction.EvictionStrategy; import org.infinispan.manager.CacheManager; import org.infinispan.manager.DefaultCacheManager; public class TestEviction extends TestCase { public void testFIFOMaxEntries() throws Exception { Configuration c = new Configuration(); int maxEntries = 5; c.setEvictionStrategy(EvictionStrategy.FIFO); c.setEvictionMaxEntries(maxEntries); c.setEvictionWakeUpInterval(500); CacheManager manager = new DefaultCacheManager(c); Cache cache = manager.getCache(); int totalKeys = 20; for (int i = 0; i < totalKeys; i++) { cache.put("key-" + (i + 1), "value-" + (i + 1)); } Thread.sleep(1000); assertEquals(maxEntries, cache.size()); } public void testLRUMaxEntries() throws Exception { Configuration c = new Configuration(); int maxEntries = 5; c.setEvictionStrategy(EvictionStrategy.LRU); c.setEvictionMaxEntries(maxEntries); c.setEvictionWakeUpInterval(500); CacheManager manager = new DefaultCacheManager(c); Cache cache = manager.getCache(); int totalKeys = 20; for (int i = 0; i < totalKeys; i++) { cache.put("key-" + (i + 1), "value-" + (i + 1)); } Thread.sleep(1000); assertEquals(maxEntries, cache.size()); } public void testLIRSMaxEntries() throws Exception { Configuration c = new Configuration(); int maxEntries = 5; c.setEvictionStrategy(EvictionStrategy.LIRS); c.setEvictionMaxEntries(maxEntries); c.setEvictionWakeUpInterval(500); CacheManager manager = new DefaultCacheManager(c); Cache cache = manager.getCache(); int totalKeys = 20; for (int i = 0; i < totalKeys; i++) { cache.put("key-" + (i + 1), "value-" + (i + 1)); } Thread.sleep(1000); assertEquals(maxEntries, cache.size()); } public void testUNORDEREDMaxEntries() throws Exception { Configuration c = new Configuration(); int maxEntries = 5; c.setEvictionStrategy(EvictionStrategy.UNORDERED); c.setEvictionMaxEntries(maxEntries); c.setEvictionWakeUpInterval(500); CacheManager manager = new DefaultCacheManager(c); Cache cache = manager.getCache(); int totalKeys = 20; for (int i = 0; i < totalKeys; i++) { cache.put("key-" + (i + 1), "value-" + (i + 1)); } Thread.sleep(1000); assertEquals(maxEntries, cache.size()); } }