Search in sources :

Example 11 with ISemaphore

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

the class ClientSemaphoreTest method testAvailablePermits.

@Test
public void testAvailablePermits() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(10);
    assertEquals(10, semaphore.availablePermits());
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 12 with ISemaphore

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

the class ClientSemaphoreTest method testSemaphoreNegInit.

@Test(expected = IllegalArgumentException.class)
public void testSemaphoreNegInit() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(-1);
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 13 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquire_whenUnAvailable.

@Test
public void testTryAcquire_whenUnAvailable() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(0);
    assertFalse(semaphore.tryAcquire());
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with ISemaphore

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

the class ClientSemaphoreTest method testAvailableReducePermits.

@Test
public void testAvailableReducePermits() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(10);
    semaphore.reducePermits(5);
    assertEquals(5, semaphore.availablePermits());
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 15 with ISemaphore

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

the class ClientSemaphoreTest method testAcquire_Threaded.

@Test
public void testAcquire_Threaded() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(0);
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        public void run() {
            try {
                semaphore.acquire();
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
    sleepSeconds(1);
    semaphore.release(2);
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    assertEquals(1, semaphore.availablePermits());
}
Also used : 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