@Test public void testMapRecordEviction() throws InterruptedException { int size = 100000; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testMapRecordEviction"); mc.setTimeToLiveSeconds(1); final CountDownLatch latch = new CountDownLatch(size); mc.addEntryListenerConfig( new EntryListenerConfig() .setImplementation( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }) .setLocal(true)); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap map = instances[0].getMap("testMapRecordEviction"); for (int i = 0; i < size; i++) { map.put(i, i); } assertTrue(latch.await(5, TimeUnit.MINUTES)); assertEquals(0, map.size()); }
@Test public void testMapRecordIdleEvictionOnMigration() throws InterruptedException { Config cfg = new Config(); final String name = "testMapRecordIdleEvictionOnMigration"; MapConfig mc = cfg.getMapConfig(name); int maxIdleSeconds = 10; int size = 100; final int nsize = size / 5; mc.setMaxIdleSeconds(maxIdleSeconds); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3); HazelcastInstance instance1 = factory.newHazelcastInstance(cfg); final IMap map = instance1.getMap(name); final CountDownLatch latch = new CountDownLatch(size - nsize); map.addEntryListener( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }, false); for (int i = 0; i < size; i++) { map.put(i, i); } final Thread thread = new Thread( new Runnable() { public void run() { while (!Thread.currentThread().isInterrupted()) { try { for (int i = 0; i < nsize; i++) { map.get(i); } Thread.sleep(1000); } catch (HazelcastInstanceNotActiveException e) { return; } catch (InterruptedException e) { return; } } } }); thread.start(); HazelcastInstance instance2 = factory.newHazelcastInstance(cfg); HazelcastInstance instance3 = factory.newHazelcastInstance(cfg); assertTrue(latch.await(1, TimeUnit.MINUTES)); Assert.assertEquals(nsize, map.size()); thread.interrupt(); thread.join(5000); }
/* github issue 585 */ @Test public void testIssue585SetWithoutTTL() 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"); Thread.sleep(2000); assertEquals(0, map.size()); h.getLifecycleService().shutdown(); }
@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 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)); }