use of com.hazelcast.cp.ISemaphore in project hazelcast by hazelcast.
the class AbstractSemaphoreAdvancedTest method testPermitAcquired_whenPermitOwnerShutsDown.
@Test
public void testPermitAcquired_whenPermitOwnerShutsDown() throws InterruptedException {
semaphore.init(1);
semaphore.acquire();
CountDownLatch acquiredLatch = new CountDownLatch(1);
spawn(() -> {
HazelcastInstance otherInstance = instances[0] == proxyInstance ? instances[1] : instances[0];
ISemaphore remoteSemaphore = otherInstance.getCPSubsystem().getSemaphore(semaphore.getName());
try {
remoteSemaphore.acquire();
acquiredLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
assertTrueEventually(() -> {
SemaphoreService service = getNodeEngineImpl(proxyInstance).getService(SemaphoreService.SERVICE_NAME);
SemaphoreRegistry registry = service.getRegistryOrNull(getGroupId());
assertNotNull(registry);
Semaphore semaphore = registry.getResourceOrNull(objectName);
assertNotNull(semaphore);
assertFalse(semaphore.getInternalWaitKeysMap().isEmpty());
});
proxyInstance.shutdown();
assertOpenEventually(acquiredLatch);
}
use of com.hazelcast.cp.ISemaphore in project hazelcast by hazelcast.
the class SemaphoreSplitBrainTest method onBeforeSplitBrainCreated.
@Override
protected void onBeforeSplitBrainCreated(HazelcastInstance[] instances) {
waitUntilCPDiscoveryCompleted(instances);
ISemaphore sema = instances[0].getCPSubsystem().getSemaphore(name);
sema.init(initialPermits);
futures = new Future[instances.length];
for (int i = 0; i < instances.length; i++) {
futures[i] = spawn(new Locker(instances[i % instances.length]));
}
sleepSeconds(3);
}
use of com.hazelcast.cp.ISemaphore in project hazelcast by hazelcast.
the class ClientSemaphoreThreadedTest method concurrent_trySemaphoreTest.
public void concurrent_trySemaphoreTest(final boolean tryWithTimeOut) {
final ISemaphore semaphore = client.getCPSubsystem().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());
}
use of com.hazelcast.cp.ISemaphore in project hazelcast by hazelcast.
the class SessionlessSemaphoreClientBasicTest method testAcquireOnMultipleProxies.
@Test
public void testAcquireOnMultipleProxies() {
ISemaphore semaphore2 = ((TestHazelcastFactory) factory).newHazelcastClient().getCPSubsystem().getSemaphore(semaphore.getName());
semaphore.init(1);
semaphore.tryAcquire(1);
assertFalse(semaphore2.tryAcquire());
}
use of com.hazelcast.cp.ISemaphore in project hazelcast by hazelcast.
the class UnsafeSessionlessSemaphoreClientBasicTest method testAcquireOnMultipleProxies.
@Test
public void testAcquireOnMultipleProxies() {
ISemaphore semaphore2 = factory.newHazelcastClient().getCPSubsystem().getSemaphore(proxyName);
semaphore.init(1);
semaphore.tryAcquire(1);
assertFalse(semaphore2.tryAcquire());
}
Aggregations