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();
}
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);
}
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);
}
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);
}
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;
}
Aggregations