PageRenderTime 67ms CodeModel.GetById 1ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 0ms

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