@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 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)); }
@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 values() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> multiMap = hClient.getMultiMap("entrySet"); Map<String, List<String>> valueKeyListMap = new HashMap<String, List<String>>(); int count = 100; for (int i = 0; i < count; i++) { for (int j = 0; j <= i; j++) { String key = String.valueOf(i); String value = String.valueOf(j); multiMap.put(key, value); if (valueKeyListMap.get(value) == null) { valueKeyListMap.put(value, new ArrayList<String>()); } valueKeyListMap.get(value).add(key); } } assertEquals(count * (count + 1) / 2, multiMap.size()); Collection<String> collection = multiMap.values(); assertEquals(count * (count + 1) / 2, collection.size()); Iterator<String> iterator = collection.iterator(); System.out.println(iterator.getClass()); for (; iterator.hasNext(); ) { String value = iterator.next(); assertNotNull(valueKeyListMap.get(value).remove(0)); if (valueKeyListMap.get(value).size() == 0) { valueKeyListMap.remove(value); } } assertTrue(valueKeyListMap.isEmpty()); }
/** Test for issue #39 */ @Test public void testIsMapKeyLocked() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final IMap map = hClient.getMap("testIsMapKeyLocked"); assertFalse(map.isLocked("key")); map.lock("key"); assertTrue(map.isLocked("key")); final CountDownLatch latch = new CountDownLatch(1); Thread thread = new Thread( new Runnable() { public void run() { assertTrue(map.isLocked("key")); try { while (map.isLocked("key")) { Thread.sleep(100); } } catch (InterruptedException e) { throw new RuntimeException(e); } latch.countDown(); } }); thread.start(); Thread.sleep(100); map.unlock("key"); assertTrue(latch.await(3, TimeUnit.SECONDS)); }
@Test public void testMultiMapGetNameAndType() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapGetNameAndType"); assertEquals("testMultiMapGetNameAndType", map.getName()); assertTrue(map.getInstanceType().isMultiMap()); }
@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 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 testMultiMapContainsEntry() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapContainsEntry"); map.put("Hello", "World"); assertTrue(map.containsEntry("Hello", "World")); }
@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 entrySet() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> multiMap = hClient.getMultiMap("entrySet"); Map<String, List<String>> keyValueListMap = new HashMap<String, List<String>>(); int count = 100; for (int i = 0; i < count; i++) { for (int j = 0; j <= i; j++) { String key = String.valueOf(i); String value = String.valueOf(j); multiMap.put(key, value); if (keyValueListMap.get(key) == null) { keyValueListMap.put(key, new ArrayList<String>()); } keyValueListMap.get(key).add(value); } } assertEquals(count * (count + 1) / 2, multiMap.size()); Set<Entry<String, String>> set = multiMap.entrySet(); assertEquals(count * (count + 1) / 2, set.size()); for (Iterator<Entry<String, String>> iterator = set.iterator(); iterator.hasNext(); ) { Entry<String, String> o = iterator.next(); assertTrue(Integer.valueOf(o.getValue()) < count); assertTrue(keyValueListMap.get(o.getKey()).contains(o.getValue())); } }
@Test public void removeFromMultiMap() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeFromMultiMap"); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.remove("a", 1)); }
public void run() { HazelcastClient hClient = TestUtility.newHazelcastClient(h); while (run) { Map<String, String> clientMap = hClient.getMap("putFromMultipleThreads"); clientMap.put(String.valueOf(counter.incrementAndGet()), String.valueOf(counter.get())); } }
@Test public void containsValue() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsValue"); assertFalse(multiMap.containsValue(1)); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.containsValue(1)); }
@Test public void testGetNullMapEntry() { HazelcastClient hClient = getHazelcastClient(); final IMap imap = hClient.getMap("testGetNullMapEntry"); String key = "key"; MapEntry mapEntry = imap.getMapEntry(key); assertNull(mapEntry); }
@Test public void testMultiMapClear() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapClear"); map.put("Hello", "World"); assertEquals(1, map.size()); map.clear(); assertEquals(0, map.size()); }
@Test public void testTwoMembersWithIndexes() { HazelcastClient hClient = getHazelcastClient(); final IMap imap = hClient.getMap("testTwoMembersWithIndexes"); imap.addIndex("name", false); imap.addIndex("age", true); imap.addIndex("active", false); doFunctionalQueryTest(imap); }
@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 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 tryPut() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("tryPut"); assertEquals(0, map.size()); Boolean result = map.tryPut("1", "CBDEF", 1, TimeUnit.SECONDS); assertTrue(result); assertEquals(1, map.size()); }
@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 getPuttedValueFromTheMap() { HazelcastClient hClient = getHazelcastClient(); Map<String, String> clientMap = hClient.getMap("getPuttedValueFromTheMap"); int size = clientMap.size(); clientMap.put("1", "Z"); String value = clientMap.get("1"); assertEquals("Z", value); assertEquals(size + 1, clientMap.size()); }
@Test public void removeFromMap() { HazelcastClient hClient = getHazelcastClient(); Map map = hClient.getMap("removeFromMap"); assertNull(map.put("a", "b")); assertEquals("b", map.get("a")); assertEquals("b", map.remove("a")); assertNull(map.remove("a")); assertNull(map.get("a")); }
@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 public void size() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("size"); assertEquals(0, multiMap.size()); assertTrue(multiMap.put("a", 1)); assertEquals(1, multiMap.size()); assertTrue(multiMap.put("a", 2)); assertEquals(2, multiMap.size()); }
@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 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 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)); } }