use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testDrainPermits.
@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);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testReleaseAcquire.
@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);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testConcurrency_MultiInstance_1_permits.
@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) {
e.printStackTrace();
}
int value = lockedCounter.get();
lockedCounter.set(value + 1);
s1.release();
});
assertThat(lockedCounter.get()).isEqualTo(iterations);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testReleaseWithoutPermits.
@Test
public void testReleaseWithoutPermits() {
RSemaphore s = redisson.getSemaphore("test");
s.release();
assertThat(s.availablePermits()).isEqualTo(1);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testConcurrency_SingleInstance.
@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) {
e.printStackTrace();
}
int value = lockedCounter.get();
lockedCounter.set(value + 1);
s1.release();
});
assertThat(lockedCounter.get()).isEqualTo(iterations);
}
Aggregations