use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class PartitionControlledIdTest method testSemaphore.
@Test
public void testSemaphore() throws Exception {
String partitionKey = "hazelcast";
HazelcastInstance hz = getHazelcastInstance(partitionKey);
ISemaphore semaphore = hz.getSemaphore("semaphore@" + partitionKey);
semaphore.release();
assertEquals("semaphore@" + partitionKey, semaphore.getName());
assertEquals(partitionKey, semaphore.getPartitionKey());
SemaphoreService service = getNodeEngine(hz).getService(SemaphoreService.SERVICE_NAME);
assertTrue(service.containsSemaphore(semaphore.getName()));
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class MBeanTest method testSemaphore.
@Test
public void testSemaphore() throws Exception {
ISemaphore semaphore = holder.getHz().getSemaphore("semaphore");
semaphore.availablePermits();
holder.assertMBeanExistEventually("ISemaphore", semaphore.getName());
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class MBeanDestroyTest method testSemaphore.
@Test
public void testSemaphore() throws Exception {
ISemaphore semaphore = holder.getHz().getSemaphore("semaphore");
semaphore.availablePermits();
holder.assertMBeanExistEventually("ISemaphore", semaphore.getName());
destroyObjectAndAssert(semaphore, "ISemaphore");
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreAdvancedTest method testSemaphoreWithFailuresAndJoin.
@Test(timeout = 300000)
public void testSemaphoreWithFailuresAndJoin() {
final String semaphoreName = randomString();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
final HazelcastInstance instance1 = factory.newHazelcastInstance();
final HazelcastInstance instance2 = factory.newHazelcastInstance();
final ISemaphore semaphore = instance1.getSemaphore(semaphoreName);
final CountDownLatch countDownLatch = new CountDownLatch(1);
assertTrue(semaphore.init(0));
final Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 2; i++) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
countDownLatch.countDown();
}
};
thread.start();
instance2.shutdown();
semaphore.release();
HazelcastInstance instance3 = factory.newHazelcastInstance();
ISemaphore semaphore1 = instance3.getSemaphore(semaphoreName);
semaphore1.release();
try {
assertTrue(countDownLatch.await(15, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
thread.interrupt();
}
}
use of com.hazelcast.core.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreAdvancedTest method testMutex.
@Test(timeout = 300000)
public void testMutex() throws InterruptedException {
final String semaphoreName = randomString();
final int threadCount = 2;
final HazelcastInstance[] instances = createHazelcastInstanceFactory(threadCount).newInstances();
final CountDownLatch latch = new CountDownLatch(threadCount);
final int loopCount = 1000;
class Counter {
int count = 0;
void inc() {
count++;
}
int get() {
return count;
}
}
final Counter counter = new Counter();
assertTrue(instances[0].getSemaphore(semaphoreName).init(1));
for (int i = 0; i < threadCount; i++) {
final ISemaphore semaphore = instances[i].getSemaphore(semaphoreName);
new Thread() {
public void run() {
for (int j = 0; j < loopCount; j++) {
try {
semaphore.acquire();
sleepMillis((int) (Math.random() * 3));
counter.inc();
} catch (InterruptedException e) {
return;
} finally {
semaphore.release();
}
}
latch.countDown();
}
}.start();
}
assertOpenEventually(latch);
assertEquals(loopCount * threadCount, counter.get());
}
Aggregations