PageRenderTime 26ms CodeModel.GetById 2ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 268 lines | 230 code | 20 blank | 18 comment | 25 complexity | 9fe0901a629810880a7e36d668911a86 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.ISet;
 20import com.hazelcast.core.ItemListener;
 21import org.junit.AfterClass;
 22import org.junit.Ignore;
 23import org.junit.Test;
 24
 25import java.util.*;
 26import java.util.concurrent.CountDownLatch;
 27import java.util.concurrent.ExecutorService;
 28import java.util.concurrent.Executors;
 29import java.util.concurrent.TimeUnit;
 30
 31import static org.junit.Assert.*;
 32
 33public class HazelcastClientSetTest extends HazelcastClientTestBase {
 34
 35    @Test(expected = NullPointerException.class)
 36    public void testAddNull() throws InterruptedException {
 37        HazelcastClient hClient = getHazelcastClient();
 38        ISet<?> set = hClient.getSet("testAddNull");
 39        set.add(null);
 40    }
 41
 42    @Test
 43    public void getSetName() {
 44        HazelcastClient hClient = getHazelcastClient();
 45        ISet<String> set = hClient.getSet("getSetName");
 46        assertEquals("getSetName", set.getName());
 47    }
 48
 49    @Test
 50    public void addRemoveItemListener() throws InterruptedException {
 51        HazelcastClient hClient = getHazelcastClient();
 52        final ISet<String> set = hClient.getSet("addRemoveItemListenerSet");
 53        final CountDownLatch addLatch = new CountDownLatch(2);
 54        final CountDownLatch removeLatch = new CountDownLatch(2);
 55        ItemListener<String> listener = new CountDownItemListener<String>(addLatch, removeLatch);
 56        set.addItemListener(listener, true);
 57        set.add("hello");
 58        set.add("hello");
 59        set.remove("hello");
 60        set.remove("hello");
 61        for (int i = 0; i < 100; i++) {
 62            if (removeLatch.getCount() != 1 || addLatch.getCount() != 1) {
 63                Thread.sleep(50);
 64            } else {
 65                break;
 66            }
 67        }
 68        assertEquals(1, removeLatch.getCount());
 69        assertEquals(1, addLatch.getCount());
 70        set.removeItemListener(listener);
 71        set.add("hello");
 72        set.add("hello");
 73        set.remove("hello");
 74        set.remove("hello");
 75        Thread.sleep(50);
 76        assertEquals(1, addLatch.getCount());
 77        assertEquals(1, removeLatch.getCount());
 78    }
 79
 80    @Test
 81    @Ignore
 82    public void TenTimesAddRemoveItemListener() throws InterruptedException {
 83        ExecutorService ex = Executors.newFixedThreadPool(1);
 84        final int count = 10;
 85        final CountDownLatch latch = new CountDownLatch(count);
 86        ex.execute(new Runnable() {
 87            public void run() {
 88                for (int i = 0; i < count; i++) {
 89                    try {
 90                        System.out.println("i: " + i);
 91                        addRemoveItemListener();
 92                        latch.countDown();
 93                    } catch (InterruptedException e) {
 94                        return;
 95                    }
 96                }
 97            }
 98        });
 99        assertTrue(latch.await(20, TimeUnit.SECONDS));
100    }
101
102    @Test
103    public void destroy() {
104        HazelcastClient hClient = getHazelcastClient();
105        ISet<Integer> set = hClient.getSet("destroy");
106        for (int i = 0; i < 100; i++) {
107            assertTrue(set.add(i));
108        }
109        ISet<Integer> set2 = hClient.getSet("destroy");
110        assertTrue(set == set2);
111        assertTrue(set.getId().equals(set2.getId()));
112        set.destroy();
113        set2 = hClient.getSet("destroy");
114        assertFalse(set == set2);
115//        for(int i=0;i<100;i++){
116//        	assertNull(list2.get(i));
117//        }
118    }
119
120    @Test
121    public void add() {
122        HazelcastClient hClient = getHazelcastClient();
123        ISet<Integer> set = hClient.getSet("add");
124        int count = 100;
125        for (int i = 0; i < count; i++) {
126            assertTrue(set.add(i));
127        }
128        for (int i = 0; i < count; i++) {
129            assertFalse(set.add(i));
130        }
131        assertEquals(count, set.size());
132    }
133
134    @Test
135    public void contains() {
136        HazelcastClient hClient = getHazelcastClient();
137        ISet<Integer> set = hClient.getSet("contains");
138        int count = 100;
139        for (int i = 0; i < count; i++) {
140            set.add(i);
141        }
142        for (int i = 0; i < count; i++) {
143            assertTrue(set.contains(i));
144        }
145        for (int i = count; i < 2 * count; i++) {
146            assertFalse(set.contains(i));
147        }
148    }
149
150    @Test
151    public void addAll() {
152        HazelcastClient hClient = getHazelcastClient();
153        ISet<Integer> set = hClient.getSet("addAll");
154        List<Integer> arr = new ArrayList<Integer>();
155        int count = 100;
156        for (int i = 0; i < count; i++) {
157            arr.add(i);
158        }
159        assertTrue(set.addAll(arr));
160    }
161
162    @Test
163    public void containsAll() {
164        HazelcastClient hClient = getHazelcastClient();
165        ISet<Integer> set = hClient.getSet("containsAll");
166        List<Integer> arrList = new ArrayList<Integer>();
167        int count = 100;
168        for (int i = 0; i < count; i++) {
169            arrList.add(i);
170        }
171        assertTrue(set.addAll(arrList));
172        assertTrue(set.containsAll(arrList));
173        arrList.set((int) count / 2, count + 1);
174        assertFalse(set.containsAll(arrList));
175    }
176
177    @Test
178    public void size() {
179        HazelcastClient hClient = getHazelcastClient();
180        ISet<Integer> set = hClient.getSet("size");
181        int count = 100;
182        assertTrue(set.isEmpty());
183        for (int i = 0; i < count; i++) {
184            assertTrue(set.add(i));
185        }
186        assertEquals(count, set.size());
187        for (int i = 0; i < count / 2; i++) {
188            assertFalse(set.add(i));
189        }
190        assertFalse(set.isEmpty());
191        assertEquals(count, set.size());
192    }
193
194    @Test
195    public void remove() {
196        HazelcastClient hClient = getHazelcastClient();
197        ISet<Integer> set = hClient.getSet("remove");
198        int count = 100;
199        assertTrue(set.isEmpty());
200        for (int i = 0; i < count; i++) {
201            assertTrue(set.add(i));
202        }
203        assertEquals(count, set.size());
204        for (int i = 0; i < count; i++) {
205            assertTrue(set.remove((Object) i));
206        }
207        assertTrue(set.isEmpty());
208        for (int i = count; i < 2 * count; i++) {
209            assertFalse(set.remove((Object) i));
210        }
211    }
212
213    @Test
214    public void clear() {
215        HazelcastClient hClient = getHazelcastClient();
216        ISet<Integer> set = hClient.getSet("clear");
217        int count = 100;
218        assertTrue(set.isEmpty());
219        for (int i = 0; i < count; i++) {
220            assertTrue(set.add(i));
221        }
222        assertEquals(count, set.size());
223        set.clear();
224        assertTrue(set.isEmpty());
225    }
226
227    @Test
228    public void removeAll() {
229        HazelcastClient hClient = getHazelcastClient();
230        ISet<Integer> set = hClient.getSet("removeAll");
231        List<Integer> arrList = new ArrayList<Integer>();
232        int count = 100;
233        for (int i = 0; i < count; i++) {
234            arrList.add(i);
235        }
236        assertTrue(set.addAll(arrList));
237        assertTrue(set.removeAll(arrList));
238        assertFalse(set.removeAll(arrList.subList(0, count / 10)));
239    }
240
241    @Test
242    public void iterate() {
243        HazelcastClient hClient = getHazelcastClient();
244        ISet<Integer> set = hClient.getSet("iterate");
245        set.add(1);
246        set.add(2);
247        set.add(2);
248        set.add(3);
249        assertEquals(3, set.size());
250        Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
251        counter.put(1, 1);
252        counter.put(2, 1);
253        counter.put(3, 1);
254        for (Iterator<Integer> iterator = set.iterator(); iterator.hasNext(); ) {
255            Integer integer = iterator.next();
256            counter.put(integer, counter.get(integer) - 1);
257            iterator.remove();
258        }
259        assertEquals(Integer.valueOf(0), counter.get(1));
260        assertEquals(Integer.valueOf(0), counter.get(2));
261        assertEquals(Integer.valueOf(0), counter.get(3));
262        assertTrue(set.isEmpty());
263    }
264
265    @AfterClass
266    public static void shutdown() {
267    }
268}