Search in sources :

Example 1 with ISemaphore

use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.

the class PartitionControlledIdTest method testSemaphore.

@Test
public void testSemaphore() throws Exception {
    String partitionKey = "hazelcast";
    HazelcastInstance hz = getHazelcastInstance(partitionKey);
    ISemaphore semaphore = hz.getSemaphore("semaphore@" + partitionKey);
    semaphore.release();
    assertEquals("semaphore@" + partitionKey, semaphore.getName());
    assertEquals(partitionKey, semaphore.getPartitionKey());
    SemaphoreService service = getNodeEngine(hz).getService(SemaphoreService.SERVICE_NAME);
    assertTrue(service.containsSemaphore(semaphore.getName()));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) SemaphoreService(com.hazelcast.concurrent.semaphore.SemaphoreService) ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with ISemaphore

use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.

the class MBeanTest method testSemaphore.

@Test
public void testSemaphore() throws Exception {
    ISemaphore semaphore = holder.getHz().getSemaphore("semaphore");
    semaphore.availablePermits();
    holder.assertMBeanExistEventually("ISemaphore", semaphore.getName());
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ISemaphore

use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.

the class MBeanDestroyTest method testSemaphore.

@Test
public void testSemaphore() throws Exception {
    ISemaphore semaphore = holder.getHz().getSemaphore("semaphore");
    semaphore.availablePermits();
    holder.assertMBeanExistEventually("ISemaphore", semaphore.getName());
    destroyObjectAndAssert(semaphore, "ISemaphore");
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with ISemaphore

use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.

the class SemaphoreAdvancedTest method testSemaphoreWithFailuresAndJoin.

@Test(timeout = 300000)
public void testSemaphoreWithFailuresAndJoin() {
    final String semaphoreName = randomString();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    final HazelcastInstance instance1 = factory.newHazelcastInstance();
    final HazelcastInstance instance2 = factory.newHazelcastInstance();
    final ISemaphore semaphore = instance1.getSemaphore(semaphoreName);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    assertTrue(semaphore.init(0));
    final Thread thread = new Thread() {

        public void run() {
            for (int i = 0; i < 2; i++) {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            countDownLatch.countDown();
        }
    };
    thread.start();
    instance2.shutdown();
    semaphore.release();
    HazelcastInstance instance3 = factory.newHazelcastInstance();
    ISemaphore semaphore1 = instance3.getSemaphore(semaphoreName);
    semaphore1.release();
    try {
        assertTrue(countDownLatch.await(15, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        thread.interrupt();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.core.ISemaphore) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ISemaphore

use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.

the class SemaphoreAdvancedTest method testMutex.

@Test(timeout = 300000)
public void testMutex() throws InterruptedException {
    final String semaphoreName = randomString();
    final int threadCount = 2;
    final HazelcastInstance[] instances = createHazelcastInstanceFactory(threadCount).newInstances();
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final int loopCount = 1000;
    class Counter {

        int count = 0;

        void inc() {
            count++;
        }

        int get() {
            return count;
        }
    }
    final Counter counter = new Counter();
    assertTrue(instances[0].getSemaphore(semaphoreName).init(1));
    for (int i = 0; i < threadCount; i++) {
        final ISemaphore semaphore = instances[i].getSemaphore(semaphoreName);
        new Thread() {

            public void run() {
                for (int j = 0; j < loopCount; j++) {
                    try {
                        semaphore.acquire();
                        sleepMillis((int) (Math.random() * 3));
                        counter.inc();
                    } catch (InterruptedException e) {
                        return;
                    } finally {
                        semaphore.release();
                    }
                }
                latch.countDown();
            }
        }.start();
    }
    assertOpenEventually(latch);
    assertEquals(loopCount * threadCount, counter.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.core.ISemaphore) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ISemaphore (com.hazelcast.core.ISemaphore)30 ParallelTest (com.hazelcast.test.annotation.ParallelTest)29 QuickTest (com.hazelcast.test.annotation.QuickTest)29 Test (org.junit.Test)29 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 SemaphoreService (com.hazelcast.concurrent.semaphore.SemaphoreService)1 Config (com.hazelcast.config.Config)1 AssertTask (com.hazelcast.test.AssertTask)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1