Search in sources :

Example 6 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testExpiration.

@Test
public void testExpiration() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("some-key");
    semaphore.trySetPermits(1);
    semaphore.expire(Duration.ofSeconds(3));
    semaphore.tryAcquire(1, 1, TimeUnit.SECONDS);
    Thread.sleep(4100);
    assertThat(redisson.getKeys().count()).isZero();
}
Also used : RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 7 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testAddPermits.

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

Example 8 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testConcurrency_SingleInstance.

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

Example 9 with RPermitExpirableSemaphore

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

the class RedissonPermitExpirableSemaphoreTest method testBlockingAcquire.

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

        @Override
        public void run() {
            RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            s.release(permitId);
        }
    };
    t.start();
    assertThat(s.availablePermits()).isEqualTo(0);
    s.acquire();
    assertThat(s.tryAcquire()).isNull();
    assertThat(s.availablePermits()).isEqualTo(0);
}
Also used : RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) Test(org.junit.jupiter.api.Test)

Example 10 with RPermitExpirableSemaphore

use of org.redisson.api.RPermitExpirableSemaphore in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchStudentService method isPenAlreadyAssigned.

@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean isPenAlreadyAssigned(final PenRequestBatchEntity penRequestBatch, final String assignedPen) {
    boolean penAlreadyAssigned = false;
    val redisKey = "multiple-assigned-pen-check::".concat(penRequestBatch.getPenRequestBatchID().toString()).concat("::").concat(assignedPen);
    log.debug("checking for multiples in batch:: {}", penRequestBatch.getPenRequestBatchID());
    final RPermitExpirableSemaphore semaphore = this.getRedissonClient().getPermitExpirableSemaphore("checkForMultiple::" + penRequestBatch.getPenRequestBatchID());
    semaphore.trySetPermits(1);
    semaphore.expire(120, TimeUnit.SECONDS);
    try {
        final String id = semaphore.tryAcquire(120, 40, TimeUnit.SECONDS);
        final String assignedPEN = this.getStringRedisTemplate().opsForValue().get(redisKey);
        if (StringUtils.isNotBlank(assignedPEN)) {
            penAlreadyAssigned = true;
        } else {
            this.getStringRedisTemplate().opsForValue().set(redisKey, "true", Duration.ofDays(1));
        }
        semaphore.tryRelease(id);
    } catch (final Exception e) {
        log.error("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
        throw new PenRegAPIRuntimeException("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
    }
    return penAlreadyAssigned;
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) EntityNotFoundException(ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException) CompletionException(java.util.concurrent.CompletionException) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Transactional(org.springframework.transaction.annotation.Transactional)

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