@Test public void testHazelcastInstances() { assertNotNull(map1); assertNotNull(map2); assertNotNull(multiMap); assertNotNull(replicatedMap); assertNotNull(queue); assertNotNull(topic); assertNotNull(set); assertNotNull(list); assertNotNull(executorService); assertNotNull(idGenerator); assertNotNull(atomicLong); assertNotNull(atomicReference); assertNotNull(countDownLatch); assertNotNull(semaphore); assertNotNull(lock); assertEquals("map1", map1.getName()); assertEquals("map2", map2.getName()); assertEquals("testMultimap", multiMap.getName()); assertEquals("replicatedMap", replicatedMap.getName()); assertEquals("testQ", queue.getName()); assertEquals("testTopic", topic.getName()); assertEquals("set", set.getName()); assertEquals("list", list.getName()); assertEquals("idGenerator", idGenerator.getName()); assertEquals("atomicLong", atomicLong.getName()); assertEquals("atomicReference", atomicReference.getName()); assertEquals("countDownLatch", countDownLatch.getName()); assertEquals("semaphore", semaphore.getName()); }
@Test public void tryAcquire() throws Exception { assertTrue(s.tryAcquire()); assertTrue(s.tryAcquire(9)); assertEquals(0, s.availablePermits()); assertFalse(s.tryAcquire(1, TimeUnit.SECONDS)); assertFalse(s.tryAcquire(2, 1, TimeUnit.SECONDS)); final CountDownLatch latch = new CountDownLatch(1); new Thread() { public void run() { try { if (s.tryAcquire(2, 5, TimeUnit.SECONDS)) { latch.countDown(); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); s.release(2); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(0, s.availablePermits()); }
@Test public void testSemaphoreSplitBrain() throws InterruptedException { Config config = newConfig(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); final HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config); final String key = generateKeyOwnedBy(h3); ISemaphore semaphore = h3.getSemaphore(key); semaphore.init(5); semaphore.acquire(3); assertEquals(2, semaphore.availablePermits()); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertTrue(h3.getPartitionService().isLocalMemberSafe()); } }); TestMemberShipListener memberShipListener = new TestMemberShipListener(2); h3.getCluster().addMembershipListener(memberShipListener); TestLifeCycleListener lifeCycleListener = new TestLifeCycleListener(1); h3.getLifecycleService().addLifecycleListener(lifeCycleListener); closeConnectionBetween(h1, h3); closeConnectionBetween(h2, h3); assertOpenEventually(memberShipListener.latch); assertClusterSizeEventually(2, h1); assertClusterSizeEventually(2, h2); assertClusterSizeEventually(1, h3); final ISemaphore semaphore1 = h1.getSemaphore(key); // when member is down, permits are released. // since releasing the permits is async, we use assert eventually assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(5, semaphore1.availablePermits()); } }); semaphore1.acquire(4); assertOpenEventually(lifeCycleListener.latch); assertClusterSizeEventually(3, h1); assertClusterSizeEventually(3, h2); assertClusterSizeEventually(3, h3); ISemaphore testSemaphore = h3.getSemaphore(key); assertEquals(1, testSemaphore.availablePermits()); }
@Test public void testAcquire() throws Exception { assertEquals(10, s.drainPermits()); final CountDownLatch latch = new CountDownLatch(1); new Thread() { public void run() { try { s.acquire(); latch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); Thread.sleep(1000); s.release(2); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(1, s.availablePermits()); }
@Before @After public void clear() throws IOException { s.reducePermits(100); s.release(10); }