Search in sources :

Example 1 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testTryAcquire.

@Test
public void testTryAcquire() throws InterruptedException {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);
    String permitId = s.tryAcquire();
    assertThat(permitId).hasSize(32);
    Thread t = new Thread() {

        @Override
        public void run() {
            RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            s.release(permitId);
        }
    };
    assertThat(s.tryAcquire()).isNull();
    t.start();
    t.join(1);
    long startTime = System.currentTimeMillis();
    String permitId2 = s.tryAcquire(1, TimeUnit.SECONDS);
    assertThat(permitId2).hasSize(32);
    assertThat(System.currentTimeMillis() - startTime).isBetween(450L, 600L);
    assertThat(s.availablePermits()).isEqualTo(0);
}
Also used : RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 2 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testTrySetPermits.

@Test
public void testTrySetPermits() {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    assertThat(s.trySetPermits(10)).isTrue();
    assertThat(s.availablePermits()).isEqualTo(10);
    assertThat(s.trySetPermits(15)).isFalse();
    assertThat(s.availablePermits()).isEqualTo(10);
}
Also used : RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 3 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testUpdateLeaseTime.

@Test
public void testUpdateLeaseTime() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test");
    semaphore.trySetPermits(1);
    assertThat(semaphore.updateLeaseTime("1234", 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();
}
Also used : RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 4 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testConcurrencyLoop_MultiInstance.

@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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 5 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testExpireTryAcquire.

@Test
public void testExpireTryAcquire() throws InterruptedException {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);
    String permitId = s.tryAcquire(100, 2, TimeUnit.SECONDS);
    final long startTime = System.currentTimeMillis();
    AtomicBoolean bool = new AtomicBoolean();
    Thread t = new Thread() {

        public void run() {
            RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
            try {
                String permitId = s.acquire();
                long spendTime = System.currentTimeMillis() - startTime;
                assertThat(spendTime).isBetween(1900L, 2100L);
                s.release(permitId);
                bool.set(true);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    t.start();
    t.join();
    assertThat(s.tryRelease(permitId)).isFalse();
    assertThat(bool.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Aggregations

RPermitExpirableSemaphore (org.redisson.api.RPermitExpirableSemaphore)15 Test (org.junit.jupiter.api.Test)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 EntityNotFoundException (ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException)1 InvalidParameterException (ca.bc.gov.educ.penreg.api.exception.InvalidParameterException)1 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)1 CompletionException (java.util.concurrent.CompletionException)1 lombok.val (lombok.val)1 Transactional (org.springframework.transaction.annotation.Transactional)1