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