Search in sources :

Example 16 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquireMultiPermits_whenUnAvailable.

@Test
public void testTryAcquireMultiPermits_whenUnAvailable() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(5);
    assertFalse(semaphore.tryAcquire(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 17 with ISemaphore

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

the class ClientSemaphoreTest method testAvailableReducePermits_WhenZero.

@Test
public void testAvailableReducePermits_WhenZero() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(0);
    semaphore.reducePermits(1);
    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 18 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquire_whenAvailableWithTimeOut.

@Test
public void testTryAcquire_whenAvailableWithTimeOut() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(1);
    assertTrue(semaphore.tryAcquire(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 19 with ISemaphore

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

the class ClientSemaphoreTest method testTryAcquire_whenAvailable.

@Test
public void testTryAcquire_whenAvailable() throws Exception {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(1);
    assertTrue(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 20 with ISemaphore

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

the class ClientSemaphoreThreadedTest method concurrent_trySemaphoreTest.

public void concurrent_trySemaphoreTest(final boolean tryWithTimeOut) {
    final ISemaphore semaphore = client.getSemaphore(randomString());
    semaphore.init(1);
    final AtomicInteger upTotal = new AtomicInteger(0);
    final AtomicInteger downTotal = new AtomicInteger(0);
    final SemaphoreTestThread[] threads = new SemaphoreTestThread[8];
    for (int i = 0; i < threads.length; i++) {
        SemaphoreTestThread t;
        if (tryWithTimeOut) {
            t = new TrySemaphoreTimeOutThread(semaphore, upTotal, downTotal);
        } else {
            t = new TrySemaphoreThread(semaphore, upTotal, downTotal);
        }
        t.start();
        threads[i] = t;
    }
    HazelcastTestSupport.assertJoinable(threads);
    for (SemaphoreTestThread t : threads) {
        assertNull("thread " + t + " has error " + t.error, t.error);
    }
    assertEquals("concurrent access to locked code caused wrong total", 0, upTotal.get() + downTotal.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ISemaphore(com.hazelcast.core.ISemaphore)

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