Search in sources :

Example 11 with RSemaphore

use of org.redisson.api.RSemaphore in project redisson by redisson.

the class RedissonSemaphoreTest method testDrainPermits.

@Test
public void testDrainPermits() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    s.acquire(3);
    assertThat(s.drainPermits()).isEqualTo(7);
    assertThat(s.availablePermits()).isEqualTo(0);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Example 12 with RSemaphore

use of org.redisson.api.RSemaphore in project redisson by redisson.

the class RedissonSemaphoreTest method testReleaseAcquire.

@Test
public void testReleaseAcquire() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    s.acquire();
    assertThat(s.availablePermits()).isEqualTo(9);
    s.release();
    assertThat(s.availablePermits()).isEqualTo(10);
    s.acquire(5);
    assertThat(s.availablePermits()).isEqualTo(5);
    s.release(5);
    assertThat(s.availablePermits()).isEqualTo(10);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Example 13 with RSemaphore

use of org.redisson.api.RSemaphore in project redisson by redisson.

the class RedissonSemaphoreTest method testConcurrency_MultiInstance_1_permits.

@Test
public void testConcurrency_MultiInstance_1_permits() throws InterruptedException {
    int iterations = 30;
    final AtomicInteger lockedCounter = new AtomicInteger();
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);
    testMultiInstanceConcurrency(iterations, r -> {
        RSemaphore s1 = r.getSemaphore("test");
        try {
            s1.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });
    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Example 14 with RSemaphore

use of org.redisson.api.RSemaphore in project redisson by redisson.

the class RedissonSemaphoreTest method testReleaseWithoutPermits.

@Test
public void testReleaseWithoutPermits() {
    RSemaphore s = redisson.getSemaphore("test");
    s.release();
    assertThat(s.availablePermits()).isEqualTo(1);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Example 15 with RSemaphore

use of org.redisson.api.RSemaphore in project redisson by redisson.

the class RedissonSemaphoreTest method testConcurrency_SingleInstance.

@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
    final AtomicInteger lockedCounter = new AtomicInteger();
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);
    int iterations = 15;
    testSingleInstanceConcurrency(iterations, r -> {
        RSemaphore s1 = r.getSemaphore("test");
        try {
            s1.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });
    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Aggregations

RSemaphore (org.redisson.api.RSemaphore)17 Test (org.junit.Test)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RLock (org.redisson.api.RLock)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1