use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testAcquireWithoutSetPermits.
@Test
public void testAcquireWithoutSetPermits() throws InterruptedException {
RSemaphore s = redisson.getSemaphore("test");
s.release();
s.release();
s.acquire(2);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testTrySetPermits.
@Test
public void testTrySetPermits() {
RSemaphore s = redisson.getSemaphore("test");
assertThat(s.trySetPermits(10)).isTrue();
assertThat(s.availablePermits()).isEqualTo(10);
assertThat(s.trySetPermits(15)).isFalse();
assertThat(s.availablePermits()).isEqualTo(10);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testConcurrencyLoop_MultiInstance.
@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
final int iterations = 100;
final AtomicInteger lockedCounter = new AtomicInteger();
RSemaphore s = redisson.getSemaphore("test");
s.trySetPermits(1);
testMultiInstanceConcurrency(16, r -> {
for (int i = 0; i < iterations; i++) {
try {
r.getSemaphore("test").acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
int value = lockedCounter.get();
lockedCounter.set(value + 1);
r.getSemaphore("test").release();
}
});
assertThat(lockedCounter.get()).isEqualTo(16 * iterations);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testBlockingAcquire.
@Test
public void testBlockingAcquire() throws InterruptedException {
RSemaphore s = redisson.getSemaphore("test");
s.trySetPermits(1);
s.acquire();
Thread t = new Thread() {
@Override
public void run() {
RSemaphore s = redisson.getSemaphore("test");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.release();
}
};
t.start();
assertThat(s.availablePermits()).isEqualTo(0);
s.acquire();
assertThat(s.tryAcquire()).isFalse();
assertThat(s.availablePermits()).isEqualTo(0);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testTryNAcquire.
@Test
public void testTryNAcquire() throws InterruptedException {
RSemaphore s = redisson.getSemaphore("test");
s.trySetPermits(5);
assertThat(s.tryAcquire(3)).isTrue();
Thread t = new Thread() {
@Override
public void run() {
RSemaphore s = redisson.getSemaphore("test");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.release();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.release();
}
};
assertThat(s.tryAcquire(4)).isFalse();
t.start();
t.join(1);
long startTime = System.currentTimeMillis();
assertThat(s.tryAcquire(4, 2, TimeUnit.SECONDS)).isTrue();
assertThat(System.currentTimeMillis() - startTime).isBetween(900L, 1020L);
assertThat(s.availablePermits()).isEqualTo(0);
}
Aggregations