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));
}
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());
}
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));
}
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());
}
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());
}
Aggregations