Java Code Examples for org.redisson.api.RSemaphore#trySetPermits()

The following examples show how to use org.redisson.api.RSemaphore#trySetPermits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example 2
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example 3
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReducePermits() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    
    s.acquire(10);
    s.reducePermits(5);
    assertThat(s.availablePermits()).isEqualTo(-5);
    s.release(10);
    assertThat(s.availablePermits()).isEqualTo(5);
    s.acquire(5);
    assertThat(s.availablePermits()).isEqualTo(0);
}
 
Example 4
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
    final int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();

    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);

    testMultiInstanceConcurrency(16, r -> {
        for (int i = 0; i < iterations; i++) {
            try {
                r.getSemaphore("test").acquire();
            }catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int value = lockedCounter.get();
            lockedCounter.set(value + 1);
            r.getSemaphore("test").release();
        }
    });

    assertThat(lockedCounter.get()).isEqualTo(16 * iterations);
}