Search in sources :

Example 6 with ISemaphore

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

the class AbstractSemaphoreFailureTest method testAcquireOnMultipleProxies.

@Test
public void testAcquireOnMultipleProxies() {
    HazelcastInstance otherInstance = instances[0] == proxyInstance ? instances[1] : instances[0];
    ISemaphore semaphore2 = otherInstance.getCPSubsystem().getSemaphore(semaphore.getName());
    semaphore.init(1);
    semaphore.tryAcquire(1);
    assertFalse(semaphore2.tryAcquire());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.cp.ISemaphore) Test(org.junit.Test)

Example 7 with ISemaphore

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

the class SemaphoreLongAwaitTest method testLongAwait.

private void testLongAwait(HazelcastInstance instance) throws ExecutionException, InterruptedException {
    ISemaphore semaphore = instance.getCPSubsystem().getSemaphore(proxyName);
    Future<Object> f1 = spawn(() -> {
        if (!semaphore.tryAcquire(1, 5, TimeUnit.MINUTES)) {
            throw new IllegalStateException();
        }
        semaphore.release();
        return null;
    });
    Future<Object> f2 = spawn(() -> {
        semaphore.acquire();
        semaphore.release();
        return null;
    });
    assertTrueEventually(() -> {
        SemaphoreService service = getNodeEngineImpl(instance).getService(SemaphoreService.SERVICE_NAME);
        assertEquals(2, service.getLiveOperations(groupId).size());
    });
    assertTrueAllTheTime(() -> {
        SemaphoreService service = getNodeEngineImpl(instance).getService(SemaphoreService.SERVICE_NAME);
        assertEquals(2, service.getLiveOperations(groupId).size());
    }, callTimeoutSeconds + 5);
    semaphore.increasePermits(1);
    assertCompletesEventually(f1);
    assertCompletesEventually(f2);
    f1.get();
    f2.get();
}
Also used : ISemaphore(com.hazelcast.cp.ISemaphore)

Example 8 with ISemaphore

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

the class HazelcastOSGiInstanceTest method getSemaphoreCalledSuccessfullyOverOSGiInstance.

@Test
public void getSemaphoreCalledSuccessfullyOverOSGiInstance() {
    ISemaphore mockSemaphore = mock(ISemaphore.class);
    HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
    HazelcastOSGiInstance hazelcastOSGiInstance = createHazelcastOSGiInstance(mockHazelcastInstance);
    CPSubsystem cpSubsystem = mock(CPSubsystem.class);
    when(mockHazelcastInstance.getCPSubsystem()).thenReturn(cpSubsystem);
    when(cpSubsystem.getSemaphore("my-semaphore")).thenReturn(mockSemaphore);
    assertEquals(mockSemaphore, hazelcastOSGiInstance.getCPSubsystem().getSemaphore("my-semaphore"));
    verify(cpSubsystem).getSemaphore("my-semaphore");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CPSubsystem(com.hazelcast.cp.CPSubsystem) HazelcastOSGiTestUtil.createHazelcastOSGiInstance(com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance) ISemaphore(com.hazelcast.cp.ISemaphore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ISemaphore (com.hazelcast.cp.ISemaphore)8 Test (org.junit.Test)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)1 CPSubsystem (com.hazelcast.cp.CPSubsystem)1 SessionlessSemaphoreBasicTest (com.hazelcast.cp.internal.datastructures.semaphore.SessionlessSemaphoreBasicTest)1 UnsafeSessionlessSemaphoreBasicTest (com.hazelcast.cp.internal.datastructures.semaphore.UnsafeSessionlessSemaphoreBasicTest)1 HazelcastOSGiTestUtil.createHazelcastOSGiInstance (com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1