Search in sources :

Example 26 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquireMultiPermits_whenAvailable.

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

Example 27 with ISemaphore

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

the class ClientSemaphoreTest method testSemaphoreInit.

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

Example 28 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquireMultiPermits_whenAvailableWithTimeOut.

@Test
public void testTryAcquireMultiPermits_whenAvailableWithTimeOut() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(10);
    assertTrue(semaphore.tryAcquire(5, 1, TimeUnit.MILLISECONDS));
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 29 with ISemaphore

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

the class ClientSemaphoreTest method testAvailablePermits_AfterDrainPermits.

@Test
public void testAvailablePermits_AfterDrainPermits() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(10);
    semaphore.drainPermits();
    assertEquals(0, 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 30 with ISemaphore

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

the class ClientSemaphoreTest method tryAcquire_Threaded.

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

        public void run() {
            try {
                if (semaphore.tryAcquire(1, 5, TimeUnit.SECONDS)) {
                    latch.countDown();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
    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