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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 303 lines · 266 code · 17 blank · 20 comment · 4 complexity · fad56151d1cd49ef8e7f429058a4eed1 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.config.SemaphoreConfig;
  19. import com.hazelcast.impl.GroupProperties;
  20. import org.junit.*;
  21. import org.junit.runner.RunWith;
  22. import java.util.Random;
  23. import java.util.concurrent.*;
  24. import static org.junit.Assert.*;
  25. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  26. public class SemaphoreTest {
  27. @BeforeClass
  28. @AfterClass
  29. public static void init() throws Exception {
  30. System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1");
  31. System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
  32. Hazelcast.shutdownAll();
  33. }
  34. @Before
  35. public void setUp() throws Exception {
  36. Hazelcast.shutdownAll();
  37. }
  38. @After
  39. public void tearDown() throws Exception {
  40. Hazelcast.shutdownAll();
  41. }
  42. @Test
  43. public void testSemaphoreWithTimeout() {
  44. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
  45. Config config = new Config();
  46. config.addSemaphoreConfig(semaphoreConfig);
  47. HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
  48. ISemaphore semaphore = instance.getSemaphore("test");
  49. //Test acquire and timeout.
  50. try {
  51. assertEquals(10, semaphore.availablePermits());
  52. semaphore.tryAcquire();
  53. assertEquals(9, semaphore.availablePermits());
  54. assertEquals(false, semaphore.tryAcquire(10, 10, TimeUnit.MILLISECONDS));
  55. assertEquals(9, semaphore.availablePermits());
  56. semaphore.release();
  57. //Test acquire and timeout and check for partial acquisitions.
  58. assertEquals(10, semaphore.availablePermits());
  59. assertEquals(false, semaphore.tryAcquire(20, 10, TimeUnit.MILLISECONDS));
  60. assertEquals(10, semaphore.availablePermits());
  61. } catch (Throwable e) {
  62. e.printStackTrace();
  63. fail(e.getMessage());
  64. }
  65. }
  66. @Test
  67. public void testSimpleSemaphore() {
  68. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
  69. Config config = new Config();
  70. config.addSemaphoreConfig(semaphoreConfig);
  71. HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
  72. ISemaphore semaphore = instance.getSemaphore("test");
  73. assertEquals(1, semaphore.availablePermits());
  74. semaphore.tryAcquire();
  75. assertEquals(0, semaphore.availablePermits());
  76. semaphore.release();
  77. assertEquals(1, semaphore.availablePermits());
  78. semaphore.tryAcquire();
  79. assertEquals(0, semaphore.availablePermits());
  80. semaphore.release();
  81. assertEquals(1, semaphore.availablePermits());
  82. semaphore.tryAcquire();
  83. assertEquals(0, semaphore.availablePermits());
  84. semaphore.release();
  85. assertEquals(1, semaphore.availablePermits());
  86. semaphore.tryAcquire();
  87. assertEquals(0, semaphore.availablePermits());
  88. semaphore.release();
  89. assertEquals(1, semaphore.availablePermits());
  90. }
  91. @Test
  92. public void testSemaphoreReducePermits() {
  93. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
  94. Config config = new Config();
  95. config.addSemaphoreConfig(semaphoreConfig);
  96. HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
  97. ISemaphore semaphore = instance.getSemaphore("test");
  98. assertEquals(10, semaphore.availablePermits());
  99. semaphore.reducePermits(1);
  100. assertEquals(9, semaphore.availablePermits());
  101. semaphore.tryAcquire(9);
  102. assertEquals(0, semaphore.availablePermits());
  103. semaphore.reducePermits(8);
  104. assertEquals(-8, semaphore.availablePermits());
  105. semaphore.release();
  106. assertEquals(-7, semaphore.availablePermits());
  107. semaphore.release(8);
  108. assertEquals(1, semaphore.availablePermits());
  109. }
  110. @Test
  111. public void testSemaphoreDisconnect() throws InterruptedException {
  112. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
  113. Config config = new Config();
  114. config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_INTERVAL, "1");
  115. config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_MAX_FAULTS, "1");
  116. config.addSemaphoreConfig(semaphoreConfig);
  117. HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
  118. HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
  119. ISemaphore semaphore1 = instance1.getSemaphore("test");
  120. ISemaphore semaphore2 = instance2.getSemaphore("test");
  121. assertEquals(10, semaphore1.availablePermits());
  122. semaphore1.tryAcquireAttach(5);
  123. semaphore1.reducePermits(1);
  124. instance1.getLifecycleService().kill();
  125. Thread.sleep(500);
  126. assertEquals(9, semaphore2.availablePermits());
  127. }
  128. @Test
  129. public void testSemaphorePeerDisconnect() throws InterruptedException {
  130. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
  131. Config config = new Config();
  132. config.addSemaphoreConfig(semaphoreConfig);
  133. HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
  134. HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
  135. ISemaphore semaphore1 = instance1.getSemaphore("test");
  136. ISemaphore semaphore2 = instance2.getSemaphore("test");
  137. semaphore2.tryAcquireAttach(5);
  138. int result = semaphore1.availablePermits();
  139. int expectedResult = 5;
  140. assertEquals(expectedResult, result);
  141. final CountDownLatch latch = new CountDownLatch(1);
  142. instance1.getCluster().addMembershipListener(new MembershipListener() {
  143. public void memberAdded(MembershipEvent membershipEvent) {
  144. }
  145. public void memberRemoved(MembershipEvent membershipEvent) {
  146. latch.countDown();
  147. }
  148. });
  149. instance2.getLifecycleService().shutdown();
  150. assertTrue(latch.await(5, TimeUnit.SECONDS));
  151. expectedResult = 10;
  152. result = semaphore1.availablePermits();
  153. assertEquals(expectedResult, result);
  154. }
  155. @Test
  156. public void testAsyncAcquire() throws Exception {
  157. final ISemaphore semaphore = Hazelcast.getSemaphore("test");
  158. assertEquals(0, semaphore.availablePermits());
  159. final Future f1 = semaphore.acquireAsync();
  160. Thread thread = new Thread() {
  161. @Override
  162. public void run() {
  163. for (; ; ) {
  164. try {
  165. f1.get(1, TimeUnit.SECONDS);
  166. break;
  167. } catch (InterruptedException e) {
  168. //expected
  169. semaphore.release();
  170. assertFalse(f1.cancel(false));
  171. break;
  172. } catch (ExecutionException e) {
  173. // not gonna happen
  174. } catch (TimeoutException e) {
  175. // keep trying
  176. }
  177. }
  178. }
  179. };
  180. assertEquals(0, semaphore.availablePermits());
  181. thread.start();
  182. Thread.sleep(3500);
  183. thread.interrupt();
  184. assertEquals(0, semaphore.availablePermits());
  185. }
  186. @Test
  187. public void testSemaphoreIncreasePermits() {
  188. SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
  189. Config config = new Config();
  190. config.addSemaphoreConfig(semaphoreConfig);
  191. HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
  192. ISemaphore semaphore = instance.getSemaphore("test");
  193. assertEquals(1, semaphore.availablePermits());
  194. semaphore.release();
  195. assertEquals(2, semaphore.availablePermits());
  196. }
  197. @Test
  198. public void testSemaphoreTryAcquireTimeout() throws InterruptedException {
  199. ISemaphore semaphore = Hazelcast.getSemaphore("test");
  200. assertEquals(0, semaphore.availablePermits());
  201. try {
  202. semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS);
  203. } catch (InstanceDestroyedException e) {
  204. e.printStackTrace();
  205. }
  206. assertEquals(0, semaphore.availablePermits());
  207. }
  208. @Test
  209. public void testMultiInstanceSemaphore() {
  210. final Random random = new Random();
  211. final int rndTimeMax = 20;
  212. int initialPermits = 2;
  213. HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(null);
  214. HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(null);
  215. HazelcastInstance instance3 = Hazelcast.newHazelcastInstance(null);
  216. final ISemaphore semaphore1 = instance1.getSemaphore("test");
  217. final ISemaphore semaphore2 = instance2.getSemaphore("test");
  218. final ISemaphore semaphore3 = instance3.getSemaphore("test");
  219. semaphore1.release(initialPermits);
  220. assertEquals(initialPermits, semaphore1.availablePermits());
  221. assertEquals(initialPermits, semaphore2.availablePermits());
  222. assertEquals(initialPermits, semaphore3.availablePermits());
  223. Thread thread1 = new Thread() {
  224. public void run() {
  225. for (int i = 0; i < 100; i++) {
  226. try {
  227. semaphore1.acquire(2);
  228. Thread.sleep(random.nextInt(rndTimeMax));
  229. semaphore1.release(2);
  230. Thread.sleep(random.nextInt(rndTimeMax));
  231. } catch (InterruptedException e) {
  232. fail(e.getMessage());
  233. } catch (InstanceDestroyedException e) {
  234. fail(e.getMessage());
  235. }
  236. }
  237. }
  238. };
  239. Thread thread2 = new Thread() {
  240. public void run() {
  241. for (int i = 0; i < 200; i++) {
  242. try {
  243. semaphore2.acquire();
  244. Thread.sleep(random.nextInt(rndTimeMax));
  245. semaphore2.release();
  246. Thread.sleep(random.nextInt(rndTimeMax));
  247. } catch (InterruptedException e) {
  248. fail(e.getMessage());
  249. } catch (InstanceDestroyedException e) {
  250. fail(e.getMessage());
  251. }
  252. }
  253. }
  254. };
  255. Thread thread3 = new Thread() {
  256. public void run() {
  257. for (int i = 0; i < 300; i++) {
  258. try {
  259. semaphore3.acquire();
  260. Thread.sleep(random.nextInt(rndTimeMax));
  261. semaphore3.release();
  262. Thread.sleep(random.nextInt(rndTimeMax));
  263. } catch (InterruptedException e) {
  264. fail(e.getMessage());
  265. } catch (InstanceDestroyedException e) {
  266. fail(e.getMessage());
  267. }
  268. }
  269. }
  270. };
  271. thread1.start();
  272. thread2.start();
  273. thread3.start();
  274. try {
  275. thread1.join();
  276. thread2.join();
  277. thread3.join();
  278. } catch (InterruptedException e) {
  279. e.printStackTrace();
  280. fail();
  281. }
  282. assertEquals(initialPermits, semaphore1.availablePermits());
  283. assertEquals(initialPermits, semaphore2.availablePermits());
  284. assertEquals(initialPermits, semaphore3.availablePermits());
  285. }
  286. }