Search in sources :

Example 21 with ISemaphore

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());
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.core.ISemaphore) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with ISemaphore

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();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 23 with ISemaphore

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) AssertTask(com.hazelcast.test.AssertTask) ISemaphore(com.hazelcast.core.ISemaphore) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with ISemaphore

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));
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 25 with ISemaphore

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));
}
Also used : ISemaphore(com.hazelcast.core.ISemaphore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ISemaphore (com.hazelcast.core.ISemaphore)30 ParallelTest (com.hazelcast.test.annotation.ParallelTest)29 QuickTest (com.hazelcast.test.annotation.QuickTest)29 Test (org.junit.Test)29 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 SemaphoreService (com.hazelcast.concurrent.semaphore.SemaphoreService)1 Config (com.hazelcast.config.Config)1 AssertTask (com.hazelcast.test.AssertTask)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1