/hazelcast-client/src/test/java/com/hazelcast/client/HazelcastClientMultiMapTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 450 lines · 401 code · 34 blank · 15 comment · 25 complexity · 7d8afe91a89c77f72a293cd96d2669df MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.client;
  17. import com.hazelcast.core.EntryEvent;
  18. import com.hazelcast.core.EntryListener;
  19. import com.hazelcast.core.MultiMap;
  20. import com.hazelcast.impl.base.Values;
  21. import org.junit.Ignore;
  22. import org.junit.Test;
  23. import java.util.*;
  24. import java.util.Map.Entry;
  25. import java.util.concurrent.CountDownLatch;
  26. import java.util.concurrent.TimeUnit;
  27. import java.util.concurrent.atomic.AtomicBoolean;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import static org.junit.Assert.*;
  30. public class HazelcastClientMultiMapTest extends HazelcastClientTestBase {
  31. @Test(expected = NullPointerException.class)
  32. public void testPutNull() {
  33. HazelcastClient hClient = getHazelcastClient();
  34. final MultiMap<Integer, String> map = hClient.getMultiMap("testPutNull");
  35. map.put(1, null);
  36. }
  37. @Test
  38. public void putToMultiMap() {
  39. HazelcastClient hClient = getHazelcastClient();
  40. MultiMap<String, Integer> multiMap = hClient.getMultiMap("putToMultiMap");
  41. assertTrue(multiMap.put("a", 1));
  42. }
  43. @Test
  44. public void removeFromMultiMap() {
  45. HazelcastClient hClient = getHazelcastClient();
  46. MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeFromMultiMap");
  47. assertTrue(multiMap.put("a", 1));
  48. assertTrue(multiMap.remove("a", 1));
  49. }
  50. @Test
  51. public void containsKey() {
  52. HazelcastClient hClient = getHazelcastClient();
  53. MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsKey");
  54. assertFalse(multiMap.containsKey("a"));
  55. assertTrue(multiMap.put("a", 1));
  56. assertTrue(multiMap.containsKey("a"));
  57. }
  58. @Test
  59. public void containsValue() {
  60. HazelcastClient hClient = getHazelcastClient();
  61. MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsValue");
  62. assertFalse(multiMap.containsValue(1));
  63. assertTrue(multiMap.put("a", 1));
  64. assertTrue(multiMap.containsValue(1));
  65. }
  66. @Test
  67. public void containsEntry() {
  68. HazelcastClient hClient = getHazelcastClient();
  69. MultiMap<String, Integer> multiMap = hClient.getMultiMap("containsEntry");
  70. assertFalse(multiMap.containsEntry("a", 1));
  71. assertTrue(multiMap.put("a", 1));
  72. assertTrue(multiMap.containsEntry("a", 1));
  73. assertFalse(multiMap.containsEntry("a", 2));
  74. }
  75. @Test
  76. public void size() {
  77. HazelcastClient hClient = getHazelcastClient();
  78. MultiMap<String, Integer> multiMap = hClient.getMultiMap("size");
  79. assertEquals(0, multiMap.size());
  80. assertTrue(multiMap.put("a", 1));
  81. assertEquals(1, multiMap.size());
  82. assertTrue(multiMap.put("a", 2));
  83. assertEquals(2, multiMap.size());
  84. }
  85. @Test
  86. public void get() throws InterruptedException {
  87. HazelcastClient hClient = getHazelcastClient();
  88. MultiMap<String, Integer> multiMap = hClient.getMultiMap("get");
  89. assertTrue(multiMap.put("a", 1));
  90. assertTrue(multiMap.put("a", 2));
  91. Map<Integer, CountDownLatch> map = new HashMap<Integer, CountDownLatch>();
  92. map.put(1, new CountDownLatch(1));
  93. map.put(2, new CountDownLatch(1));
  94. Collection<Integer> collection = multiMap.get("a");
  95. assertEquals(Values.class, collection.getClass());
  96. assertEquals(2, collection.size());
  97. for (Iterator<Integer> it = collection.iterator(); it.hasNext(); ) {
  98. Integer o = it.next();
  99. map.get(o).countDown();
  100. }
  101. assertTrue(map.get(1).await(10, TimeUnit.SECONDS));
  102. assertTrue(map.get(2).await(10, TimeUnit.SECONDS));
  103. }
  104. @Test
  105. public void removeKey() throws InterruptedException {
  106. HazelcastClient hClient = getHazelcastClient();
  107. MultiMap<String, Integer> multiMap = hClient.getMultiMap("removeKey");
  108. assertTrue(multiMap.put("a", 1));
  109. assertTrue(multiMap.put("a", 2));
  110. Map<Integer, CountDownLatch> map = new HashMap<Integer, CountDownLatch>();
  111. map.put(1, new CountDownLatch(1));
  112. map.put(2, new CountDownLatch(1));
  113. Collection<Integer> collection = multiMap.remove("a");
  114. assertEquals(Values.class, collection.getClass());
  115. assertEquals(2, collection.size());
  116. for (Iterator<Integer> it = collection.iterator(); it.hasNext(); ) {
  117. Object o = it.next();
  118. map.get((Integer) o).countDown();
  119. }
  120. assertTrue(map.get(1).await(10, TimeUnit.SECONDS));
  121. assertTrue(map.get(2).await(10, TimeUnit.SECONDS));
  122. }
  123. @Test
  124. public void keySet() throws InterruptedException {
  125. HazelcastClient hClient = getHazelcastClient();
  126. MultiMap<String, String> multiMap = hClient.getMultiMap("keySet");
  127. int count = 100;
  128. for (int i = 0; i < count; i++) {
  129. for (int j = 0; j <= i; j++) {
  130. multiMap.put(String.valueOf(i), String.valueOf(j));
  131. }
  132. }
  133. assertEquals(count * (count + 1) / 2, multiMap.size());
  134. Set<String> set = multiMap.keySet();
  135. assertEquals(count, set.size());
  136. Set<String> s = new HashSet<String>();
  137. for (int i = 0; i < count; i++) {
  138. s.add(String.valueOf(i));
  139. }
  140. assertEquals(s, set);
  141. }
  142. @Test
  143. public void entrySet() throws InterruptedException {
  144. HazelcastClient hClient = getHazelcastClient();
  145. MultiMap<String, String> multiMap = hClient.getMultiMap("entrySet");
  146. Map<String, List<String>> keyValueListMap = new HashMap<String, List<String>>();
  147. int count = 100;
  148. for (int i = 0; i < count; i++) {
  149. for (int j = 0; j <= i; j++) {
  150. String key = String.valueOf(i);
  151. String value = String.valueOf(j);
  152. multiMap.put(key, value);
  153. if (keyValueListMap.get(key) == null) {
  154. keyValueListMap.put(key, new ArrayList<String>());
  155. }
  156. keyValueListMap.get(key).add(value);
  157. }
  158. }
  159. assertEquals(count * (count + 1) / 2, multiMap.size());
  160. Set<Entry<String, String>> set = multiMap.entrySet();
  161. assertEquals(count * (count + 1) / 2, set.size());
  162. for (Iterator<Entry<String, String>> iterator = set.iterator(); iterator.hasNext(); ) {
  163. Entry<String, String> o = iterator.next();
  164. assertTrue(Integer.valueOf(o.getValue()) < count);
  165. assertTrue(keyValueListMap.get(o.getKey()).contains(o.getValue()));
  166. }
  167. }
  168. @Test
  169. public void values() throws InterruptedException {
  170. HazelcastClient hClient = getHazelcastClient();
  171. MultiMap<String, String> multiMap = hClient.getMultiMap("entrySet");
  172. Map<String, List<String>> valueKeyListMap = new HashMap<String, List<String>>();
  173. int count = 100;
  174. for (int i = 0; i < count; i++) {
  175. for (int j = 0; j <= i; j++) {
  176. String key = String.valueOf(i);
  177. String value = String.valueOf(j);
  178. multiMap.put(key, value);
  179. if (valueKeyListMap.get(value) == null) {
  180. valueKeyListMap.put(value, new ArrayList<String>());
  181. }
  182. valueKeyListMap.get(value).add(key);
  183. }
  184. }
  185. assertEquals(count * (count + 1) / 2, multiMap.size());
  186. Collection<String> collection = multiMap.values();
  187. assertEquals(count * (count + 1) / 2, collection.size());
  188. Iterator<String> iterator = collection.iterator();
  189. System.out.println(iterator.getClass());
  190. for (; iterator.hasNext(); ) {
  191. String value = iterator.next();
  192. assertNotNull(valueKeyListMap.get(value).remove(0));
  193. if (valueKeyListMap.get(value).size() == 0) {
  194. valueKeyListMap.remove(value);
  195. }
  196. }
  197. assertTrue(valueKeyListMap.isEmpty());
  198. }
  199. @Test
  200. public void testMultiMapPutAndGet() {
  201. HazelcastClient hClient = getHazelcastClient();
  202. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapPutAndGet");
  203. map.put("Hello", "World");
  204. Collection<String> values = map.get("Hello");
  205. assertEquals("World", values.iterator().next());
  206. map.put("Hello", "Europe");
  207. map.put("Hello", "America");
  208. map.put("Hello", "Asia");
  209. map.put("Hello", "Africa");
  210. map.put("Hello", "Antarctica");
  211. map.put("Hello", "Australia");
  212. values = map.get("Hello");
  213. assertEquals(7, values.size());
  214. assertTrue(map.containsKey("Hello"));
  215. assertFalse(map.containsKey("Hi"));
  216. }
  217. @Test
  218. public void testMultiMapGetNameAndType() {
  219. HazelcastClient hClient = getHazelcastClient();
  220. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapGetNameAndType");
  221. assertEquals("testMultiMapGetNameAndType", map.getName());
  222. assertTrue(map.getInstanceType().isMultiMap());
  223. }
  224. @Test
  225. public void testMultiMapClear() {
  226. HazelcastClient hClient = getHazelcastClient();
  227. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapClear");
  228. map.put("Hello", "World");
  229. assertEquals(1, map.size());
  230. map.clear();
  231. assertEquals(0, map.size());
  232. }
  233. @Test
  234. public void testMultiMapContainsKey() {
  235. HazelcastClient hClient = getHazelcastClient();
  236. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapContainsKey");
  237. map.put("Hello", "World");
  238. assertTrue(map.containsKey("Hello"));
  239. }
  240. @Test
  241. public void testMultiMapContainsValue() {
  242. HazelcastClient hClient = getHazelcastClient();
  243. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapContainsValue");
  244. map.put("Hello", "World");
  245. assertTrue(map.containsValue("World"));
  246. }
  247. @Test
  248. public void testMultiMapContainsEntry() {
  249. HazelcastClient hClient = getHazelcastClient();
  250. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapContainsEntry");
  251. map.put("Hello", "World");
  252. assertTrue(map.containsEntry("Hello", "World"));
  253. }
  254. @Test
  255. public void testMultiMapKeySet() {
  256. HazelcastClient hClient = getHazelcastClient();
  257. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapKeySet");
  258. map.put("Hello", "World");
  259. map.put("Hello", "Europe");
  260. map.put("Hello", "America");
  261. map.put("Hello", "Asia");
  262. map.put("Hello", "Africa");
  263. map.put("Hello", "Antarctica");
  264. map.put("Hello", "Australia");
  265. Set<String> keys = map.keySet();
  266. assertEquals(1, keys.size());
  267. }
  268. @Test
  269. public void testMultiMapValues() {
  270. HazelcastClient hClient = getHazelcastClient();
  271. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapValues");
  272. map.put("Hello", "World");
  273. map.put("Hello", "Europe");
  274. map.put("Hello", "America");
  275. map.put("Hello", "Asia");
  276. map.put("Hello", "Africa");
  277. map.put("Hello", "Antarctica");
  278. map.put("Hello", "Australia");
  279. Collection<String> values = map.values();
  280. assertEquals(7, values.size());
  281. }
  282. @Test
  283. public void testMultiMapRemove() {
  284. HazelcastClient hClient = getHazelcastClient();
  285. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapRemove");
  286. map.put("Hello", "World");
  287. map.put("Hello", "Europe");
  288. map.put("Hello", "America");
  289. map.put("Hello", "Asia");
  290. map.put("Hello", "Africa");
  291. map.put("Hello", "Antarctica");
  292. map.put("Hello", "Australia");
  293. assertEquals(7, map.size());
  294. assertEquals(1, map.keySet().size());
  295. Collection<String> values = map.remove("Hello");
  296. assertEquals(7, values.size());
  297. assertEquals(0, map.size());
  298. assertEquals(0, map.keySet().size());
  299. map.put("Hello", "World");
  300. assertEquals(1, map.size());
  301. assertEquals(1, map.keySet().size());
  302. }
  303. @Test
  304. public void testMultiMapRemoveEntries() {
  305. HazelcastClient hClient = getHazelcastClient();
  306. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapRemoveEntries");
  307. map.put("Hello", "World");
  308. map.put("Hello", "Europe");
  309. map.put("Hello", "America");
  310. map.put("Hello", "Asia");
  311. map.put("Hello", "Africa");
  312. map.put("Hello", "Antartica");
  313. map.put("Hello", "Australia");
  314. boolean removed = map.remove("Hello", "World");
  315. assertTrue(removed);
  316. assertEquals(6, map.size());
  317. }
  318. @Test
  319. public void testMultiMapEntrySet() {
  320. HazelcastClient hClient = getHazelcastClient();
  321. MultiMap<String, String> map = hClient.getMultiMap("testMultiMapEntrySet");
  322. map.put("Hello", "World");
  323. map.put("Hello", "Europe");
  324. map.put("Hello", "America");
  325. map.put("Hello", "Asia");
  326. map.put("Hello", "Africa");
  327. map.put("Hello", "Antarctica");
  328. map.put("Hello", "Australia");
  329. Set<Map.Entry<String, String>> entries = map.entrySet();
  330. assertEquals(7, entries.size());
  331. int itCount = 0;
  332. for (Map.Entry<String, String> entry : entries) {
  333. assertEquals("Hello", entry.getKey());
  334. itCount++;
  335. }
  336. assertEquals(7, itCount);
  337. }
  338. @Test
  339. public void testMultiMapValueCount() {
  340. HazelcastClient hClient = getHazelcastClient();
  341. MultiMap<Integer, String> map = hClient.getMultiMap("testMultiMapValueCount");
  342. map.put(1, "World");
  343. map.put(2, "Africa");
  344. map.put(1, "America");
  345. map.put(2, "Antarctica");
  346. map.put(1, "Asia");
  347. map.put(1, "Europe");
  348. map.put(2, "Australia");
  349. assertEquals(4, map.valueCount(1));
  350. assertEquals(3, map.valueCount(2));
  351. }
  352. @Test
  353. @Ignore
  354. public void testLotsOfRemove() throws InterruptedException {
  355. HazelcastClient hClient = getHazelcastClient();
  356. final MultiMap<Integer, String> map = hClient.getMultiMap("testLotsOfRemove");
  357. map.put(1, "adam");
  358. final AtomicBoolean running = new AtomicBoolean(true);
  359. final AtomicInteger p = new AtomicInteger(0);
  360. final AtomicInteger r = new AtomicInteger(0);
  361. Thread.sleep(1000);
  362. new Thread(new Runnable() {
  363. public void run() {
  364. while (running.get()) {
  365. map.put(1, "" + Math.random());
  366. p.incrementAndGet();
  367. }
  368. }
  369. }).start();
  370. new Thread(new Runnable() {
  371. public void run() {
  372. while (running.get()) {
  373. map.remove(1);
  374. r.incrementAndGet();
  375. }
  376. }
  377. }).start();
  378. final CountDownLatch latch = new CountDownLatch(1);
  379. new Thread(new Runnable() {
  380. public void run() {
  381. int ip = p.get();
  382. int ir = r.get();
  383. try {
  384. Thread.sleep(1000);
  385. } catch (InterruptedException e) {
  386. }
  387. if (p.get() == ip || r.get() == ir) {
  388. System.out.println("STUCK p= " + p.get() + "::: r" + r.get());
  389. } else {
  390. latch.countDown();
  391. }
  392. }
  393. }).start();
  394. assertTrue(latch.await(5, TimeUnit.SECONDS));
  395. running.set(false);
  396. }
  397. @Test
  398. public void listener() throws InterruptedException {
  399. HazelcastClient hClient = getHazelcastClient();
  400. final MultiMap<Integer, String> map = hClient.getMultiMap("listener");
  401. final CountDownLatch added = new CountDownLatch(1);
  402. map.addEntryListener(new EntryListener<Integer, String>() {
  403. public void entryAdded(EntryEvent<Integer, String> integerStringEntryEvent) {
  404. added.countDown();
  405. }
  406. public void entryRemoved(EntryEvent<Integer, String> integerStringEntryEvent) {
  407. }
  408. public void entryUpdated(EntryEvent<Integer, String> integerStringEntryEvent) {
  409. }
  410. public void entryEvicted(EntryEvent<Integer, String> integerStringEntryEvent) {
  411. }
  412. }, true);
  413. map.put(1, "v");
  414. assertTrue(added.await(5000, TimeUnit.MILLISECONDS));
  415. }
  416. }