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

The following examples show how to use org.redisson.api.RPermitExpirableSemaphore#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: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateLeaseTime() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test");
    semaphore.trySetPermits(1);
    assertThat(semaphore.updateLeaseTime("123", 1, TimeUnit.SECONDS)).isFalse();
    String id = semaphore.acquire();
    assertThat(semaphore.updateLeaseTime(id, 1, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(1200);
    assertThat(semaphore.updateLeaseTime(id, 1, TimeUnit.SECONDS)).isFalse();
    String id2 = semaphore.tryAcquire(1, 1, TimeUnit.SECONDS);
    assertThat(semaphore.updateLeaseTime(id2, 3, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(2800);
    assertThat(semaphore.availablePermits()).isZero();
    Thread.sleep(500);
    assertThat(semaphore.availablePermits()).isOne();
    assertThat(semaphore.updateLeaseTime(id2, 2, TimeUnit.SECONDS)).isFalse();
}
 
Example 2
Source File: RedissonPermitExpirableSemaphoreTest.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();

    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);

    int iterations = 100;
    testSingleInstanceConcurrency(iterations, r -> {
        RPermitExpirableSemaphore s1 = redisson.getPermitExpirableSemaphore("test");
        try {
            String permitId = s1.acquire();
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release(permitId);
        }catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example 3
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiration() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("some-key");
    semaphore.trySetPermits(1);
    semaphore.expire(3, TimeUnit.SECONDS);
    semaphore.tryAcquire(1, 1, TimeUnit.SECONDS);
    Thread.sleep(4100);
    assertThat(redisson.getKeys().count()).isZero();
}
 
Example 4
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddPermits() throws InterruptedException {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(10);
    
    s.addPermits(5);
    assertThat(s.availablePermits()).isEqualTo(15);
    s.addPermits(-10);
    assertThat(s.availablePermits()).isEqualTo(5);
}
 
Example 5
Source File: RedissonPermitExpirableSemaphoreTest.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();

    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);

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

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