@Test public void removeFromMultiMap() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeFromMultiMap"); assertTrue(multiMap.put("a", 1)); assertTrue(multiMap.remove("a", 1)); }
@Test public void testContainsKey_whenKeyExists() { final MultiMap mm = client.getMultiMap(randomString()); mm.put("key1", "value1"); assertTrue(mm.containsKey("key1")); }
@Test public void testRemove_whenKeyNotExist() { final MultiMap mm = client.getMultiMap(randomString()); Collection coll = mm.remove("NOT_THERE"); assertEquals(Collections.EMPTY_SET, coll); }
@Test public void testPut() { final Object key = "key1"; final MultiMap mm = client.getMultiMap(randomString()); assertTrue(mm.put(key, 1)); }
@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 testMultiMapContainsEntry() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapContainsEntry"); map.put("Hello", "World"); assertTrue(map.containsEntry("Hello", "World")); }
@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 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 testValueCount_whenKeyNotThere() { final Object key = "key1"; final MultiMap mm = client.getMultiMap(randomString()); assertEquals(0, mm.valueCount("NOT_THERE")); }
@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 testContainsValue_whenExists() { final MultiMap mm = client.getMultiMap(randomString()); mm.put("key1", "value1"); assertTrue(mm.containsValue("value1")); assertFalse(mm.containsValue("NOT_THERE")); }
@Test public void testPut_WithExistingKeyValue() { final Object key = "key1"; final MultiMap mm = client.getMultiMap(randomString()); assertTrue(mm.put(key, 1)); assertFalse(mm.put(key, 1)); }
@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 testValueCount() { final Object key = "key1"; final MultiMap mm = client.getMultiMap(randomString()); mm.put(key, 1); mm.put(key, 2); assertEquals(2, mm.valueCount(key)); }
private <T, R> R testAvg(T[] values, Aggregation<String, T, R> aggregation) throws Exception { String mapName = randomMapName(); MultiMap<String, T> map = HAZELCAST_INSTANCE.getMultiMap(mapName); for (int i = 0; i < values.length; i++) { map.put("key-" + i, values[i]); } Supplier<String, T, T> supplier = Supplier.all(); return map.aggregate(supplier, aggregation); }
@Test public void testKeySet() { final int maxKeys = 23; final MultiMap mm = client.getMultiMap(randomString()); Set expected = new TreeSet(); for (int key = 0; key < maxKeys; key++) { mm.put(key, 1); expected.add(key); } assertEquals(expected, mm.keySet()); }
@Test public void testRemoveValue_whenValueNotExists() { final Object key = "key"; final int maxItemsPerKey = 4; final MultiMap mm = client.getMultiMap(randomString()); for (int i = 0; i < maxItemsPerKey; i++) { mm.put(key, i); } boolean result = mm.remove(key, "NOT_THERE"); assertFalse(result); }
@Test @Ignore public void testLotsOfRemove() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final MultiMap<Integer, String> map = hClient.getMultiMap("testLotsOfRemove"); map.put(1, "adam"); final AtomicBoolean running = new AtomicBoolean(true); final AtomicInteger p = new AtomicInteger(0); final AtomicInteger r = new AtomicInteger(0); Thread.sleep(1000); new Thread( new Runnable() { public void run() { while (running.get()) { map.put(1, "" + Math.random()); p.incrementAndGet(); } } }) .start(); new Thread( new Runnable() { public void run() { while (running.get()) { map.remove(1); r.incrementAndGet(); } } }) .start(); final CountDownLatch latch = new CountDownLatch(1); new Thread( new Runnable() { public void run() { int ip = p.get(); int ir = r.get(); try { Thread.sleep(1000); } catch (InterruptedException e) { } if (p.get() == ip || r.get() == ir) { System.out.println("STUCK p= " + p.get() + "::: r" + r.get()); } else { latch.countDown(); } } }) .start(); assertTrue(latch.await(5, TimeUnit.SECONDS)); running.set(false); }
@Test public void testClear() { final MultiMap mm = client.getMultiMap(randomString()); final int maxKeys = 9; final int maxValues = 3; for (int key = 0; key < maxKeys; key++) { for (int val = 0; val < maxValues; val++) { mm.put(key, val); } } mm.clear(); assertEquals(0, mm.size()); }
@Test public void testEntrySet() { final int maxKeys = 14; final int maxValues = 3; final MultiMap mm = client.getMultiMap(randomString()); for (int key = 0; key < maxKeys; key++) { for (int val = 0; val < maxValues; val++) { mm.put(key, val); } } assertEquals(maxKeys * maxValues, mm.entrySet().size()); }
@Override public void removeAllForValue(final V val, final Handler<AsyncResult<Void>> completionHandler) { vertx.executeBlocking( () -> { for (Map.Entry<K, V> entry : map.entrySet()) { V v = entry.getValue(); if (val.equals(v)) { map.remove(entry.getKey(), v); } } return null; }, completionHandler); }
@Test public void testRemoveKeyValue() { final Object key = "key"; final int maxItemsPerKey = 4; final MultiMap mm = client.getMultiMap(randomString()); for (int i = 0; i < maxItemsPerKey; i++) { mm.put(key, i); } for (int i = 0; i < maxItemsPerKey; i++) { boolean result = mm.remove(key, i); assertTrue(result); } }
@Test public void testRemoveKey() { final Object key = "key"; final int maxItemsPerKey = 44; final MultiMap mm = client.getMultiMap(randomString()); Set expeted = new TreeSet(); for (int i = 0; i < maxItemsPerKey; i++) { mm.put(key, i); expeted.add(i); } Set resultSet = new TreeSet(mm.remove(key)); assertEquals(expeted, resultSet); assertEquals(0, mm.size()); }
@Test public void testGet() { final Object key = "key"; final int maxItemsPerKey = 33; final MultiMap mm = client.getMultiMap(randomString()); Set expected = new TreeSet(); for (int i = 0; i < maxItemsPerKey; i++) { mm.put(key, i); expected.add(i); } Collection resultSet = new TreeSet(mm.get(key)); assertEquals(expected, resultSet); }
@Test public void testSizeCount() { final Object key1 = "key1"; final Object key2 = "key2"; final MultiMap mm = client.getMultiMap(randomString()); mm.put(key1, 1); mm.put(key1, 2); mm.put(key2, 1); mm.put(key2, 2); mm.put(key2, 2); assertEquals(4, mm.size()); }
@Test public void testHazelcastInstances() { assertNotNull(map1); assertNotNull(map2); assertNotNull(multiMap); assertNotNull(replicatedMap); assertNotNull(queue); assertNotNull(topic); assertNotNull(set); assertNotNull(list); assertNotNull(executorService); assertNotNull(idGenerator); assertNotNull(atomicLong); assertNotNull(atomicReference); assertNotNull(countDownLatch); assertNotNull(semaphore); assertNotNull(lock); assertEquals("map1", map1.getName()); assertEquals("map2", map2.getName()); assertEquals("testMultimap", multiMap.getName()); assertEquals("replicatedMap", replicatedMap.getName()); assertEquals("testQ", queue.getName()); assertEquals("testTopic", topic.getName()); assertEquals("set", set.getName()); assertEquals("list", list.getName()); assertEquals("idGenerator", idGenerator.getName()); assertEquals("atomicLong", atomicLong.getName()); assertEquals("atomicReference", atomicReference.getName()); assertEquals("countDownLatch", countDownLatch.getName()); assertEquals("semaphore", semaphore.getName()); }
@Test public void testPutAndRoleBack() throws Exception { final String mapName = randomString(); final String key = "key"; final String value = "value"; final MultiMap multiMap = client.getMultiMap(mapName); TransactionContext tx = client.newTransactionContext(); tx.beginTransaction(); TransactionalMultiMap mulitMapTxn = tx.getMultiMap(mapName); mulitMapTxn.put(key, value); mulitMapTxn.put(key, value); tx.rollbackTransaction(); assertEquals(Collections.EMPTY_LIST, multiMap.get(key)); }
@Test public void testMultiMapKeySet() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapKeySet"); map.put("Hello", "World"); map.put("Hello", "Europe"); map.put("Hello", "America"); map.put("Hello", "Asia"); map.put("Hello", "Africa"); map.put("Hello", "Antarctica"); map.put("Hello", "Australia"); Set<String> keys = map.keySet(); assertEquals(1, keys.size()); }
@Test public void testMultiMapValues() { HazelcastClient hClient = getHazelcastClient(); MultiMap<String, String> map = hClient.getMultiMap("testMultiMapValues"); map.put("Hello", "World"); map.put("Hello", "Europe"); map.put("Hello", "America"); map.put("Hello", "Asia"); map.put("Hello", "Africa"); map.put("Hello", "Antarctica"); map.put("Hello", "Australia"); Collection<String> values = map.values(); assertEquals(7, values.size()); }
@Test public void testMultiMapValueCount() { HazelcastClient hClient = getHazelcastClient(); MultiMap<Integer, String> map = hClient.getMultiMap("testMultiMapValueCount"); map.put(1, "World"); map.put(2, "Africa"); map.put(1, "America"); map.put(2, "Antarctica"); map.put(1, "Asia"); map.put(1, "Europe"); map.put(2, "Australia"); assertEquals(4, map.valueCount(1)); assertEquals(3, map.valueCount(2)); }