Search in sources :

Example 1 with ISemaphore

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.cp.ISemaphore) ISemaphore(com.hazelcast.cp.ISemaphore) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ISemaphore

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);
}
Also used : ISemaphore(com.hazelcast.cp.ISemaphore)

Example 3 with ISemaphore

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ISemaphore(com.hazelcast.cp.ISemaphore)

Example 4 with ISemaphore

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());
}
Also used : TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ISemaphore(com.hazelcast.cp.ISemaphore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SessionlessSemaphoreBasicTest(com.hazelcast.cp.internal.datastructures.semaphore.SessionlessSemaphoreBasicTest)

Example 5 with ISemaphore

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());
}
Also used : ISemaphore(com.hazelcast.cp.ISemaphore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) UnsafeSessionlessSemaphoreBasicTest(com.hazelcast.cp.internal.datastructures.semaphore.UnsafeSessionlessSemaphoreBasicTest) 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