@Test public void testZeroResetsTTL() throws InterruptedException { Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testZeroResetsTTL"); int ttl = 5; mc.setTimeToLiveSeconds(ttl); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap("testZeroResetsTTL"); final CountDownLatch latch = new CountDownLatch(1); map.addEntryListener( new EntryAdapter<Object, Object>() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }, false); map.put(1, 1); map.put(2, 2); map.put(1, 2, 0, TimeUnit.SECONDS); latch.await(10, TimeUnit.SECONDS); assertNull(map.get(2)); assertEquals(2, map.get(1)); }
@Test public void evictFromMap() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("evictFromMap"); assertNull(map.put("a", "b")); assertEquals("b", map.get("a")); assertTrue(map.evict("a")); assertNull(map.get("a")); }
@Test public void clear() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("clear"); for (int i = 0; i < 100; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } map.clear(); for (int i = 0; i < 100; i++) { assertNull(map.get(i)); } }
@Test public void iterateOverMapEntries() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("iterateOverMapEntries"); map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); Set<Entry<String, String>> entrySet = map.entrySet(); assertEquals(3, entrySet.size()); Set<String> keySet = map.keySet(); for (Entry<String, String> entry : entrySet) { assertTrue(keySet.contains(entry.getKey())); assertEquals(entry.getValue(), map.get(entry.getKey())); } Iterator<Entry<String, String>> it = entrySet.iterator(); for (String key : keySet) { MapEntry mapEntry = map.getMapEntry(key); assertEquals(1, mapEntry.getHits()); } while (it.hasNext()) { it.next(); it.remove(); } assertTrue(map.isEmpty()); }
@Test public void getMapEntry() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("getMapEntry"); assertNull(map.put("a", "b")); map.get("a"); map.get("a"); MapEntry<String, String> entry = map.getMapEntry("a"); assertEquals("a", entry.getKey()); assertEquals("b", entry.getValue()); assertEquals(2, entry.getHits()); assertEquals("b", entry.getValue()); assertEquals("b", entry.setValue("c")); assertEquals("c", map.get("a")); assertEquals("c", entry.getValue()); }
@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 removeIfSame() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("remove"); String result = map.put("1", "CBDEF"); assertNull(result); assertFalse(map.remove("1", "CBD")); assertEquals("CBDEF", map.get("1")); assertTrue(map.remove("1", "CBDEF")); }
@Test public void isEmpty() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("isEmpty"); int counter = 100; assertTrue(map.isEmpty()); for (int i = 0; i < counter; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } assertFalse(map.isEmpty()); }
@Test public void addListenerForKey() throws InterruptedException, IOException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("addListenerForKey"); map.clear(); assertEquals(0, map.size()); final CountDownLatch entryAddLatch = new CountDownLatch(1); final CountDownLatch entryUpdatedLatch = new CountDownLatch(1); final CountDownLatch entryRemovedLatch = new CountDownLatch(1); CountDownLatchEntryListener<String, String> listener = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); map.addEntryListener(listener, "hello", true); assertNull(map.get("hello")); map.put("hello", "world"); map.put("hello", "new world"); assertEquals("new world", map.get("hello")); map.remove("hello"); assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS)); assertTrue(entryUpdatedLatch.await(10, TimeUnit.SECONDS)); assertTrue(entryRemovedLatch.await(10, TimeUnit.SECONDS)); }
@Test public void containsValue() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("containsValue"); int counter = 100; for (int i = 0; i < counter; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } for (int i = 0; i < counter; i++) { assertTrue(map.containsValue(i)); } }
@Test public void testIssue508And513() throws Exception { HazelcastClient client = getHazelcastClient(); IMap<String, HashSet<byte[]>> callEventsMap = client.getMap("CALL_EVENTS"); IMap<String, Long> metaDataMap = client.getMap("CALL_META_DATA"); IMap<String, byte[]> callStartMap = client.getMap("CALL_START_EVENTS"); MultiMap<String, String> calls = client.getMultiMap("CALLS"); calls.lock("1"); calls.unlock("1"); byte[] bytes = new byte[10]; HashSet<byte[]> hashSet = new HashSet<byte[]>(); hashSet.add(bytes); String callId = "1"; callEventsMap.put(callId, hashSet); callStartMap.put(callId, bytes); metaDataMap.put(callId, 10L); Transaction txn = client.getTransaction(); txn.begin(); try { // remove the data callEventsMap.remove(callId); // remove meta data metaDataMap.remove(callId); // remove call start callStartMap.remove(callId); calls.put(callId, callId); txn.commit(); } catch (Exception e) { fail(); } assertNull(callEventsMap.get(callId)); assertNull(metaDataMap.get(callId)); assertNull(callStartMap.get(callId)); assertEquals(0, callEventsMap.size()); assertEquals(0, metaDataMap.size()); assertEquals(0, callStartMap.size()); }
@Test public void putAllMany() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("putAllMany"); int counter = 100; for (int j = 0; j < 4; j++, counter *= 10) { Map tempMap = new HashMap(); for (int i = 0; i < counter; i++) { tempMap.put(i, i); } map.putAll(tempMap); assertEquals(1, map.get(1)); } map.destroy(); }
@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(); } }
/* 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(); }
@Test public void destroyMap() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("destroy"); for (int i = 0; i < 100; i++) { assertNull(map.put(i, i)); assertEquals(i, map.get(i)); } IMap<Integer, Integer> map2 = hClient.getMap("destroy"); assertTrue(map == map2); assertTrue(map.getId().equals(map2.getId())); map.destroy(); // map2 = hClient.getMap("destroy"); // assertFalse(map == map2); for (int i = 0; i < 100; i++) { // assertNull(map2.get(i)); } }
@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(); } }
/* 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 public void putAll() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("putAll"); int counter = 100; Set keys = new HashSet(counter); for (int i = 0; i < counter; i++) { keys.add(i); } Map all = map.getAll(keys); assertEquals(0, all.size()); Map tempMap = new HashMap(); for (int i = 0; i < counter; i++) { tempMap.put(i, i); } map.putAll(tempMap); for (int i = 0; i < counter; i++) { assertEquals(i, map.get(i)); } all = map.getAll(keys); assertEquals(counter, all.size()); }
@Test public void addListenerAndMultiPut() throws InterruptedException, IOException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, byte[]> map = hClient.getMap("addListenerAndMultiPut"); map.clear(); int counter = 100; assertEquals(0, map.size()); final CountDownLatch entryAddLatch = new CountDownLatch(counter); final CountDownLatch entryUpdatedLatch = new CountDownLatch(counter); final CountDownLatch entryRemovedLatch = new CountDownLatch(counter); CountDownLatchEntryListener<String, byte[]> listener = new CountDownLatchEntryListener<String, byte[]>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); map.addEntryListener(listener, true); assertNull(map.get("hello")); Map<String, byte[]> many = new HashMap<String, byte[]>(); for (int i = 0; i < counter; i++) { many.put("" + i, new byte[i]); } map.putAll(many); assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS)); // assertTrue(entryUpdatedLatch.await(10, TimeUnit.MILLISECONDS)); // assertTrue(entryRemovedLatch.await(10, TimeUnit.MILLISECONDS)); }
/** * Returns the status of whether the worker is enabled or not * * @param id the id of the worker to test * @return true if the worker is enabled, false otherwise */ @Override public boolean workerEnabled(String id) { return workerEnabled.containsKey(id) && workerEnabled.get(id); }
@Override public Object valAt(Object k) { return _map.get(k, null); }
@Test public void testPutWithTwoMember() throws Exception { Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); IMap imap1 = h1.getMap("default"); IMap imap2 = h2.getMap("default"); assertEquals(0, imap1.size()); assertEquals(0, imap2.size()); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); assertNotNull(cmap1); assertNotNull(cmap2); Object key = "1"; Object value = "value"; Data dKey = toData(key); Data dValue = toData(value); imap1.put(key, value, 5, TimeUnit.SECONDS); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); Record record1 = cmap1.getRecord(dKey); Record record2 = cmap2.getRecord(dKey); long now = System.currentTimeMillis(); long millisLeft1 = record1.getExpirationTime() - now; long millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(dValue, record1.getValueData()); assertEquals(dValue, record2.getValueData()); imap1.set("2", "value2", 5, TimeUnit.SECONDS); assertEquals("value2", imap1.get("2")); assertEquals("value2", imap2.get("2")); Thread.sleep(6000); assertNull(imap1.get("2")); assertNull(imap2.get("2")); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(23000); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); imap1.put(key, value, 10, TimeUnit.SECONDS); assertTrue(migrateKey(key, h1, h1, 0)); assertTrue(migrateKey(key, h1, h2, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey(key, h1, h2, 0)); assertTrue(migrateKey(key, h1, h1, 1)); cmap1.startCleanup(true); cmap2.startCleanup(true); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); Thread.sleep(11000); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); imap1.put("1", "value1"); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); assertEquals(Long.MAX_VALUE, record1.getExpirationTime()); assertEquals(Long.MAX_VALUE, record2.getExpirationTime()); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); imap1.remove("1"); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.mapIndexService.size()); assertEquals(0, cmap2.mapIndexService.size()); }
@Override public Object valAt(Object k, Object defaultValue) { return _map.get(k, defaultValue); }
@Test public void testMigrationOfTTLAndLock() throws Exception { Config config = new Config(); final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); ConcurrentMapManager cmm1 = getConcurrentMapManager(h1); ConcurrentMapManager cmm2 = getConcurrentMapManager(h2); final IMap imap1 = h1.getMap("default"); final IMap imap2 = h2.getMap("default"); final Data dKey = toData("1"); assertTrue(migrateKey("1", h1, h1, 0)); assertTrue(migrateKey("1", h1, h2, 1)); imap1.put("1", "value1", 60, TimeUnit.SECONDS); imap1.lock("1"); Future put2 = imap2.putAsync("1", "value2"); imap2.addEntryListener( new EntryAdapter() { @Override public void entryUpdated(EntryEvent entryEvent) { System.out.println(entryEvent); } }, "1", true); if (put2 == null) fail(); Thread.sleep(1000); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); Record record1 = cmap1.getRecord(dKey); assertEquals(1, record1.getScheduledActionCount()); for (ScheduledAction scheduledAction : record1.getScheduledActions()) { assertTrue(scheduledAction.isValid()); } assertNotNull(record1.getListeners()); assertEquals(1, record1.getListeners().size()); DistributedLock lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey("1", h1, h2, 0)); assertTrue(migrateKey("1", h1, h1, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000); assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000); Record record2 = cmap2.getRecord(dKey); lock = record2.getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); imap1.unlock("1"); put2.get(10, TimeUnit.SECONDS); assertEquals("value2", imap1.get("1")); assertEquals("value2", imap2.get("1")); assertNotNull(record2.getListeners()); assertEquals(1, record2.getListeners().size()); }