use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreAdvancedTest method testSemaphoreWithFailures.
@Test(timeout = 300000)
public void testSemaphoreWithFailures() throws InterruptedException {
final String semaphoreName = randomString();
final int k = 4;
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k + 1);
final HazelcastInstance[] instances = factory.newInstances();
final ISemaphore semaphore = instances[k].getSemaphore(semaphoreName);
int initialPermits = 20;
semaphore.init(initialPermits);
for (int i = 0; i < k; i++) {
int rand = (int) (Math.random() * 5) + 1;
semaphore.acquire(rand);
initialPermits -= rand;
assertEquals(initialPermits, semaphore.availablePermits());
semaphore.release(rand);
initialPermits += rand;
assertEquals(initialPermits, semaphore.availablePermits());
instances[i].shutdown();
semaphore.acquire(rand);
initialPermits -= rand;
assertEquals(initialPermits, semaphore.availablePermits());
semaphore.release(rand);
initialPermits += rand;
assertEquals(initialPermits, semaphore.availablePermits());
}
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreAdvancedTest method testAcquire_whenInstanceShutdown.
@Test(expected = IllegalStateException.class, timeout = 30000)
public void testAcquire_whenInstanceShutdown() throws InterruptedException {
HazelcastInstance hz = createHazelcastInstance();
final ISemaphore semaphore = hz.getSemaphore(randomString());
hz.shutdown();
semaphore.acquire();
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreSplitBrainTest method testSemaphoreSplitBrain.
@Test
public void testSemaphoreSplitBrain() throws InterruptedException {
Config config = newConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
HazelcastInstance h2 = factory.newHazelcastInstance(config);
HazelcastInstance h3 = factory.newHazelcastInstance(config);
warmUpPartitions(h1, h2, h3);
final String key = generateKeyOwnedBy(h3);
final ISemaphore semaphore1 = h1.getSemaphore(key);
final ISemaphore semaphore3 = h3.getSemaphore(key);
semaphore1.init(5);
semaphore3.acquire(3);
assertEquals(2, semaphore3.availablePermits());
waitAllForSafeState(h1, h2, h3);
// create split: [h1, h2] & [h3]
closeConnectionBetween(h1, h3);
closeConnectionBetween(h2, h3);
assertClusterSizeEventually(2, h1);
assertClusterSizeEventually(2, h2);
assertClusterSizeEventually(1, h3);
// when member is down, permits are released.
// since releasing the permits is async, we use assert eventually
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(5, semaphore1.availablePermits());
}
});
semaphore1.acquire(4);
// merge back
getNode(h3).getClusterService().merge(getAddress(h1));
assertClusterSizeEventually(3, h1);
assertClusterSizeEventually(3, h2);
assertClusterSizeEventually(3, h3);
assertEquals(1, semaphore3.availablePermits());
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class ClientSemaphoreTest method testTryAcquire_whenUnAvailableWithTimeOut.
@Test
public void testTryAcquire_whenUnAvailableWithTimeOut() throws Exception {
final ISemaphore semaphore = client.getSemaphore(randomString());
semaphore.init(0);
assertFalse(semaphore.tryAcquire(1, TimeUnit.MILLISECONDS));
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class ClientSemaphoreTest method testTryAcquireMultiPermits_whenUnAvailableWithTimeOut.
@Test
public void testTryAcquireMultiPermits_whenUnAvailableWithTimeOut() throws Exception {
final ISemaphore semaphore = client.getSemaphore(randomString());
semaphore.init(5);
assertFalse(semaphore.tryAcquire(10, 1, TimeUnit.MILLISECONDS));
}
Aggregations