@Test public void testEvictionLFU() { try { final int k = 1; final int size = 10000; final String mapName = "testEvictionLFU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(20); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size / 2; i++) { map.put(i, i); map.get(i); } Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(3000); Assert.assertFalse("No eviction!?!?!?", map.size() == size); boolean isFrequentlyUsedEvicted = false; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { isFrequentlyUsedEvicted = true; break; } } Assert.assertFalse(isFrequentlyUsedEvicted); instances[0].getLifecycleService().shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } }
/** * Test for the issue 477. Updates should also update the TTL * * @throws Exception */ @Test public void testMapPutWithTTL() throws Exception { int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); IMap<Integer, String> map = factory.newHazelcastInstance(null).getMap("testMapPutWithTTL"); map.put(1, "value0", 100, TimeUnit.MILLISECONDS); assertEquals(true, map.containsKey(1)); Thread.sleep(2500); assertEquals(false, map.containsKey(1)); map.put(1, "value1", 10, TimeUnit.SECONDS); assertEquals(true, map.containsKey(1)); Thread.sleep(5000); assertEquals(true, map.containsKey(1)); map.put(1, "value2", 10, TimeUnit.SECONDS); Thread.sleep(5000); assertEquals(true, map.containsKey(1)); map.put(1, "value3", 10, TimeUnit.SECONDS); assertEquals(true, map.containsKey(1)); }
@Test public void testEvictionLRU() { final int k = 2; final int size = 10000; try { final String mapName = "testEvictionLRU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(10); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(2000); for (int i = 0; i < size / 2; i++) { map.put(i, i); } Thread.sleep(1000); int recentlyUsedEvicted = 0; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { recentlyUsedEvicted++; } } Assert.assertTrue(recentlyUsedEvicted == 0); } catch (InterruptedException e) { e.printStackTrace(); } }
/** * Test for issue 614 * * @throws InterruptedException */ @Test public void testContainsKeyShouldDelayEviction() throws InterruptedException { Config cfg = new Config(); String mapname = "testContainsKeyShouldDelayEviction"; cfg.getMapConfig(mapname).setMaxIdleSeconds(3); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap(mapname); map.put(1, 1); for (int i = 0; i < 20; i++) { assertTrue(map.containsKey(1)); Thread.sleep(500); } }
/* github issue 585 */ @Test public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException { Config config = new Config(); config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet"); NearCacheConfig nearCacheConfig = new NearCacheConfig(); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); HazelcastInstance h = factory.newHazelcastInstance(config); IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet"); map.set("key", "value", 1, TimeUnit.SECONDS); map.set("key", "value2", 0, TimeUnit.SECONDS); Thread.sleep(2000); assertEquals("value2", map.get("key")); h.getLifecycleService().shutdown(); }
/* github issue 304 */ @Test public void testIssue304EvictionDespitePut() throws InterruptedException { Config c = new Config(); c.getGroupConfig().setName("testIssue304EvictionDespitePut"); final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>(); final MapConfig value = new MapConfig(); value.setMaxIdleSeconds(3); mapConfigs.put("default", value); c.setMapConfigs(mapConfigs); final Properties properties = new Properties(); properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups c.setProperties(properties); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c); IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap"); final AtomicInteger evictCount = new AtomicInteger(0); map.addEntryListener( new EntryListener<String, Long>() { public void entryAdded(EntryEvent<String, Long> event) {} public void entryRemoved(EntryEvent<String, Long> event) {} public void entryUpdated(EntryEvent<String, Long> event) {} public void entryEvicted(EntryEvent<String, Long> event) { evictCount.incrementAndGet(); } }, true); String key = "key"; for (int i = 0; i < 5; i++) { map.put(key, System.currentTimeMillis()); Thread.sleep(1000); } assertEquals(evictCount.get(), 0); assertNotNull(map.get(key)); hazelcastInstance.getLifecycleService().shutdown(); }
/** * Test for the issue 537. Eviction event is fired for an object already removed * * @throws Exception */ @Test public void testEvictionAfterRemove() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap("map"); final AtomicInteger count = new AtomicInteger(0); map.addEntryListener( new EntryAdapter<Object, Object>() { @Override public void entryEvicted(EntryEvent<Object, Object> event) { count.incrementAndGet(); } }, true); map.put(1, 1, 1, TimeUnit.SECONDS); map.put(2, 2, 1, TimeUnit.SECONDS); map.remove(1); Thread.sleep(2000); assertEquals(1, count.get()); }
@Test public void testEvictionLFU2() { try { final int k = 2; final int size = 10000; final String mapName = "testEvictionLFU2"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(90); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size; i++) { map.put(i, i); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 100; j++) { assertNotNull(map.get(j)); } for (int j = size - 100; j < size; j++) { assertNotNull(map.get(j)); } Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } }
@Test public void testEvictionPerPartition() throws InterruptedException { final int k = 2; final int size = 10; final String mapName = "testEvictionPerPartition"; Config cfg = new Config(); final MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); final MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final int pnum = instances[0].getPartitionService().getPartitions().size(); int insertCount = size * pnum * 2; Map map = instances[0].getMap(mapName); for (int i = 0; i < insertCount; i++) { map.put(i, i); } Thread.sleep(2000); assertTrue(map.size() < size * pnum * (100 - mc.getEvictionPercentage()) / 100); }
@Test public void testMapWideEviction() throws InterruptedException { int size = 10000; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testMapWideEviction"); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); int n = 3; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap map = instances[0].getMap("testMapWideEviction"); for (int i = 0; i < size; i++) { map.put(i, i); } Thread.sleep(1200); assertTrue(map.size() <= (size * n * (100 - mc.getEvictionPercentage()) / 100)); }