@Test public void removeListener() throws InterruptedException, IOException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("removeListener"); final CountDownLatch entryAddLatch = new CountDownLatch(5); final CountDownLatch entryUpdatedLatch = new CountDownLatch(5); final CountDownLatch entryRemovedLatch = new CountDownLatch(5); CountDownLatchEntryListener<String, String> listener1 = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); CountDownLatchEntryListener<String, String> listener2 = new CountDownLatchEntryListener<String, String>( entryAddLatch, entryUpdatedLatch, entryRemovedLatch); map.addEntryListener(listener1, true); map.put("hello", "world"); map.put("hello", "new world"); map.remove("hello"); Thread.sleep(100); assertEquals(4, entryAddLatch.getCount()); assertEquals(4, entryRemovedLatch.getCount()); assertEquals(4, entryUpdatedLatch.getCount()); map.removeEntryListener(listener1); map.put("hello", "world"); map.put("hello", "new world"); map.remove("hello"); Thread.sleep(100); assertEquals(4, entryAddLatch.getCount()); assertEquals(4, entryRemovedLatch.getCount()); assertEquals(4, entryUpdatedLatch.getCount()); }
@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()); }
public void doFunctionalQueryTest(IMap imap) { Employee em = new Employee("joe", 33, false, 14.56); imap.put("1", new Employee("joe", 33, false, 14.56)); imap.put("2", new Employee("ali", 23, true, 15.00)); for (int i = 3; i < 103; i++) { imap.put( String.valueOf(i), new Employee("name" + i, i % 60, ((i % 2) == 1), Double.valueOf(i))); } Set<Map.Entry> entries = imap.entrySet(); assertEquals(102, entries.size()); int itCount = 0; for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); itCount++; } assertEquals(102, itCount); EntryObject e = new PredicateBuilder().getEntryObject(); Predicate predicate = e.is("active").and(e.get("age").equal(23)); entries = imap.entrySet(predicate); assertEquals(3, entries.size()); for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); assertEquals(c.getAge(), 23); assertTrue(c.isActive()); } imap.remove("2"); entries = imap.entrySet(predicate); assertEquals(2, entries.size()); for (Map.Entry entry : entries) { Employee c = (Employee) entry.getValue(); assertEquals(c.getAge(), 23); assertTrue(c.isActive()); } }
@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 valuesToArray() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("valuesToArray"); assertEquals(0, map.size()); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); assertEquals(3, map.size()); { final Object[] values = map.values().toArray(); Arrays.sort(values); assertArrayEquals(new Object[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[3]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[2]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[5]); Arrays.sort(values, 0, 3); assertArrayEquals(new String[] {"1", "2", "3", null, null}, values); } }
@Test public void addIndex() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("addIndex"); int size = 1000; for (int i = 0; i < size; i++) { map.put(String.valueOf(i), new Employee("name" + i, i, true, 0)); } EntryObject e = new PredicateBuilder().getEntryObject(); Predicate predicate = e.get("age").equal(23); long begin = Clock.currentTimeMillis(); Set<Entry<Object, Object>> set = map.entrySet(predicate); long timeWithoutIndex = Clock.currentTimeMillis() - begin; assertEquals(1, set.size()); assertEquals(size, map.size()); map.destroy(); map = hClient.getMap("addIndex"); map.addIndex("age", true); for (int i = 0; i < size; i++) { map.put(String.valueOf(i), new Employee("name" + i, i, true, 0)); } begin = Clock.currentTimeMillis(); set = map.entrySet(predicate); long timeWithIndex = Clock.currentTimeMillis() - begin; assertEquals(1, set.size()); assertEquals(size, map.size()); assertTrue(timeWithoutIndex > 2 * timeWithIndex); // map.addIndex("age", true); }
@Test public void testIssue321() throws Exception { HazelcastClient hClient = getHazelcastClient(); final IMap<Integer, Integer> imap = hClient.getMap("testIssue321_1"); final BlockingQueue<EntryEvent<Integer, Integer>> events1 = new LinkedBlockingQueue<EntryEvent<Integer, Integer>>(); final BlockingQueue<EntryEvent<Integer, Integer>> events2 = new LinkedBlockingQueue<EntryEvent<Integer, Integer>>(); imap.addEntryListener( new EntryAdapter<Integer, Integer>() { @Override public void entryAdded(EntryEvent event) { events2.add(event); } }, false); imap.addEntryListener( new EntryAdapter<Integer, Integer>() { @Override public void entryAdded(EntryEvent event) { events1.add(event); } }, true); imap.put(1, 1); final EntryEvent<Integer, Integer> event1 = events1.poll(10, TimeUnit.MILLISECONDS); final EntryEvent<Integer, Integer> event2 = events2.poll(10, TimeUnit.MILLISECONDS); assertNotNull(event1); assertNotNull(event2); assertNotNull(event1.getValue()); assertNull(event2.getValue()); }
@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 lockMap() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final IMap<String, String> map = hClient.getMap("lockMap"); final CountDownLatch unlockLatch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1); map.put("a", "b"); map.lockMap(1, TimeUnit.SECONDS); assertTrue(map.tryPut("a", "c", 10, TimeUnit.MILLISECONDS)); new Thread( new Runnable() { public void run() { assertFalse(map.lockMap(10, TimeUnit.MILLISECONDS)); unlockLatch.countDown(); assertTrue(map.lockMap(Long.MAX_VALUE, TimeUnit.SECONDS)); latch.countDown(); // map.unlockMap(); } }) .start(); assertTrue(unlockLatch.await(10, TimeUnit.SECONDS)); Thread.sleep(2000); map.unlockMap(); assertEquals("c", map.getMapEntry("a").getValue()); assertTrue(latch.await(10, TimeUnit.SECONDS)); }
/* github issue 455 */ @Test public void testIssue455ZeroTTLShouldPreventEviction() throws InterruptedException { Config config = new Config(); config.getGroupConfig().setName("testIssue455ZeroTTLShouldPreventEviction"); 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("testIssue455ZeroTTLShouldPreventEviction"); map.put("key", "value", 1, TimeUnit.SECONDS); map.put("key", "value2", 0, TimeUnit.SECONDS); Thread.sleep(2000); assertEquals("value2", map.get("key")); 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 putIfAbsent() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("putIfAbsent"); String result = map.put("1", "CBDEF"); assertNull(result); assertNull(map.putIfAbsent("2", "C")); assertEquals("C", map.putIfAbsent("2", "D")); }
@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 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 putWithTTL() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("putWithTTL"); assertEquals(0, map.size()); map.put("1", "CBDEF", 100, TimeUnit.MILLISECONDS); assertEquals(1, map.size()); Thread.sleep(200); assertEquals(0, map.size()); }
@Test public void testGetAsync() throws Exception { HazelcastClient hClient = getHazelcastClient(); String key = "key"; String value1 = "value1"; IMap<String, String> map = hClient.getMap("map:test:getAsync"); map.put(key, value1); Future<String> f1 = map.getAsync(key); assertEquals(value1, f1.get()); }
/** * 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 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); }
@Test public void putIfAbsentWithTtl() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("putIfAbsentWithTtl"); String result = map.put("1", "CBDEF"); assertNull(result); assertNull(map.putIfAbsent("2", "C", 100, TimeUnit.MILLISECONDS)); assertEquals(2, map.size()); assertEquals("C", map.putIfAbsent("2", "D", 100, TimeUnit.MILLISECONDS)); Thread.sleep(100); assertEquals(1, map.size()); }
@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 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 getSize() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("getSize"); assertEquals(0, map.size()); map.put("a", "b"); assertEquals(1, map.size()); for (int i = 0; i < 100; i++) { map.put(String.valueOf(i), String.valueOf(i)); } assertEquals(101, map.size()); map.remove("a"); assertEquals(100, map.size()); for (int i = 0; i < 50; i++) { map.remove(String.valueOf(i)); } assertEquals(50, map.size()); for (int i = 50; i < 100; i++) { map.remove(String.valueOf(i)); } assertEquals(0, map.size()); }
@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 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); } }
@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 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 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 testMapPutTtlWithListener() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); final HazelcastInstance[] instances = factory.newInstances(cfg); warmUpPartitions(instances); final int k = 10; final int putCount = 10000; final CountDownLatch latch = new CountDownLatch(k * putCount); final IMap map = instances[0].getMap("testMapEvictionTtlWithListener"); final AtomicBoolean error = new AtomicBoolean(false); final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>()); map.addEntryListener( new EntryAdapter() { public void entryEvicted(final EntryEvent event) { final Long expectedEvictionTime = (Long) (event.getOldValue()); long timeDifference = System.currentTimeMillis() - expectedEvictionTime; if (timeDifference > 5000) { error.set(true); times.add(timeDifference); } latch.countDown(); } }, true); for (int i = 0; i < k; i++) { final int threadId = i; int ttl = (int) (Math.random() * 5000 + 3000); for (int j = 0; j < putCount; j++) { final long expectedEvictionTime = ttl + System.currentTimeMillis(); map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS); } } assertTrue(latch.await(1, TimeUnit.MINUTES)); assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get()); }