PageRenderTime 32ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/test/java/com/hazelcast/core/ICountDownLatchTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 225 lines | 198 code | 12 blank | 15 comment | 3 complexity | 4cbbef63096f87b6e3ea99d97085e816 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 */
 16package com.hazelcast.core;
 17
 18import com.hazelcast.config.Config;
 19import com.hazelcast.impl.CountDownLatchProxy;
 20import com.hazelcast.impl.GroupProperties;
 21import org.junit.*;
 22import org.junit.runner.RunWith;
 23
 24import java.util.concurrent.TimeUnit;
 25import java.util.concurrent.atomic.AtomicBoolean;
 26import java.util.concurrent.atomic.AtomicInteger;
 27
 28import static org.junit.Assert.*;
 29
 30@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 31public class ICountDownLatchTest {
 32
 33    @BeforeClass
 34    @AfterClass
 35    public static void init() throws Exception {
 36        System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1");
 37        System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
 38        Hazelcast.shutdownAll();
 39    }
 40
 41    @Before
 42    public void setUp() throws Exception {
 43        Hazelcast.shutdownAll();
 44    }
 45
 46    @After
 47    public void tearDown() throws Exception {
 48        Hazelcast.shutdownAll();
 49    }
 50
 51    @Test
 52    public void testCountDownLatchSimple() throws InterruptedException {
 53        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 54        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 55        final ICountDownLatch cdl1 = h1.getCountDownLatch("test");
 56        final ICountDownLatch cdl2 = h2.getCountDownLatch("test");
 57        Member h1Member = h1.getCluster().getLocalMember();
 58        final AtomicInteger result = new AtomicInteger();
 59        int count = 5;
 60        assertTrue(cdl1.setCount(count));
 61        assertEquals(count, ((CountDownLatchProxy) cdl2).getCount());
 62        assertEquals(h1Member, ((CountDownLatchProxy) cdl1).getOwner());
 63        assertEquals(h1Member, ((CountDownLatchProxy) cdl2).getOwner());
 64        Thread thread = new Thread() {
 65            @Override
 66            public void run() {
 67                try {
 68                    if (cdl2.await(10, TimeUnit.SECONDS))
 69                        result.incrementAndGet();
 70                } catch (Throwable e) {
 71                    e.printStackTrace();
 72                    fail();
 73                }
 74            }
 75        };
 76        thread.start();
 77        for (int i = count; i > 0; i--) {
 78            assertEquals(i, ((CountDownLatchProxy) cdl2).getCount());
 79            cdl1.countDown();
 80            Thread.sleep(1000);
 81        }
 82        assertEquals(1, result.get());
 83    }
 84
 85    @Test
 86    public void testCountDownLatchOwnerLeft() throws InterruptedException {
 87        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 88        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 89        final ICountDownLatch cdl1 = h1.getCountDownLatch("test");
 90        final ICountDownLatch cdl2 = h2.getCountDownLatch("test");
 91        Member h2Member = h2.getCluster().getLocalMember();
 92        final AtomicInteger result = new AtomicInteger();
 93        assertNull(((CountDownLatchProxy) cdl1).getOwner());
 94        assertNull(((CountDownLatchProxy) cdl2).getOwner());
 95        assertTrue(cdl2.setCount(1));
 96        assertEquals(1, ((CountDownLatchProxy) cdl1).getCount());
 97        assertEquals(1, ((CountDownLatchProxy) cdl2).getCount());
 98        assertEquals(h2Member, ((CountDownLatchProxy) cdl1).getOwner());
 99        assertEquals(h2Member, ((CountDownLatchProxy) cdl2).getOwner());
100        final AtomicBoolean failed = new AtomicBoolean(false);
101        Thread thread = new Thread() {
102            @Override
103            public void run() {
104                try {
105                    if (!cdl1.await(5, TimeUnit.SECONDS)) {
106                        failed.set(true);
107                    }
108                } catch (MemberLeftException e) {
109                    result.incrementAndGet();
110                } catch (Throwable e) {
111                    e.printStackTrace();
112                    failed.set(true);
113                }
114            }
115        };
116        thread.start();
117        Thread.sleep(1000);
118        h2.shutdown();
119        thread.join();
120        assertFalse("Failed latch await!", failed.get());
121        assertEquals("Should throw MemberLeftException!", 1, result.get());
122    }
123
124    @Test
125    public void testCountDownLatchOwnerLeftInstancesReversed() throws InterruptedException {
126        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
127        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
128        final ICountDownLatch cdl1 = h1.getCountDownLatch("test");
129        final ICountDownLatch cdl2 = h2.getCountDownLatch("test");
130        Member h2Member = h2.getCluster().getLocalMember();
131        final AtomicInteger result = new AtomicInteger();
132        assertNull(((CountDownLatchProxy) cdl1).getOwner());
133        assertNull(((CountDownLatchProxy) cdl2).getOwner());
134        assertTrue(cdl2.setCount(1));
135        assertEquals(1, ((CountDownLatchProxy) cdl1).getCount());
136        assertEquals(1, ((CountDownLatchProxy) cdl2).getCount());
137        assertEquals(h2Member, ((CountDownLatchProxy) cdl1).getOwner());
138        assertEquals(h2Member, ((CountDownLatchProxy) cdl2).getOwner());
139        Thread thread = new Thread() {
140            @Override
141            public void run() {
142                try {
143                    assertFalse(cdl1.await(5, TimeUnit.SECONDS));
144                    fail();
145                } catch (MemberLeftException e) {
146                    result.incrementAndGet();
147                } catch (Throwable e) {
148                    e.printStackTrace();
149                    fail();
150                }
151            }
152        };
153        thread.start();
154        Thread.sleep(1000);
155        h2.shutdown();
156        thread.join();
157        assertEquals(1, result.get());
158    }
159
160    @Test
161    public void testCountDownLatchInstanceDestroyed() throws InterruptedException {
162        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(null);
163        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(null);
164        final ICountDownLatch cdl1 = h1.getCountDownLatch("test");
165        final ICountDownLatch cdl2 = h2.getCountDownLatch("test");
166        Member h1Member = h1.getCluster().getLocalMember();
167        final AtomicInteger result = new AtomicInteger();
168        cdl1.setCount(1);
169        assertEquals(1, ((CountDownLatchProxy) cdl2).getCount());
170        assertEquals(h1Member, ((CountDownLatchProxy) cdl1).getOwner());
171        assertEquals(h1Member, ((CountDownLatchProxy) cdl2).getOwner());
172        Thread thread = new Thread() {
173            @Override
174            public void run() {
175                try {
176                    assertFalse(cdl1.await(5, TimeUnit.SECONDS));
177                    fail();
178                } catch (InstanceDestroyedException e) {
179                    result.incrementAndGet();
180                } catch (Throwable e) {
181                    e.printStackTrace();
182                    fail();
183                }
184            }
185        };
186        thread.start();
187        Thread.sleep(1000);
188        cdl1.destroy();
189        thread.join();
190        assertEquals(1, result.get());
191    }
192
193    @Test
194    public void testCountDownLatchHazelcastShutdown() throws InterruptedException {
195        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(null);
196        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(null);
197        final ICountDownLatch cdl1 = h1.getCountDownLatch("test");
198        final ICountDownLatch cdl2 = h2.getCountDownLatch("test");
199        Member h1Member = h1.getCluster().getLocalMember();
200        final AtomicInteger result = new AtomicInteger();
201        cdl1.setCount(1);
202        assertEquals(1, ((CountDownLatchProxy) cdl2).getCount());
203        assertEquals(h1Member, ((CountDownLatchProxy) cdl1).getOwner());
204        assertEquals(h1Member, ((CountDownLatchProxy) cdl2).getOwner());
205        Thread thread = new Thread() {
206            @Override
207            public void run() {
208                try {
209                    assertFalse(cdl1.await(5, TimeUnit.SECONDS));
210                    fail();
211                } catch (IllegalStateException e) {
212                    result.incrementAndGet();
213                } catch (Throwable e) {
214                    e.printStackTrace();
215                    fail();
216                }
217            }
218        };
219        thread.start();
220        Thread.sleep(1000);
221        h1.shutdown();
222        thread.join();
223        assertEquals(1, result.get());
224    }
225}