|
|
@ -3,22 +3,43 @@ package org.redisson;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.BrokenBarrierException;
|
|
|
|
import java.util.concurrent.BrokenBarrierException;
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
|
|
|
|
import java.util.concurrent.CyclicBarrier;
|
|
|
|
import java.util.concurrent.CyclicBarrier;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
import org.junit.Assume;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Assume;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.redisson.api.RSemaphore;
|
|
|
|
import org.redisson.api.RSemaphore;
|
|
|
|
|
|
|
|
|
|
|
|
public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void testReducePermits() throws InterruptedException {
|
|
|
|
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
|
|
|
|
s.trySetPermits(10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.acquire(10);
|
|
|
|
|
|
|
|
s.reducePermits(5);
|
|
|
|
|
|
|
|
assertThat(s.availablePermits()).isEqualTo(-5);
|
|
|
|
|
|
|
|
s.release(10);
|
|
|
|
|
|
|
|
assertThat(s.availablePermits()).isEqualTo(5);
|
|
|
|
|
|
|
|
s.acquire(5);
|
|
|
|
|
|
|
|
assertThat(s.availablePermits()).isEqualTo(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testBlockingAcquire() throws InterruptedException {
|
|
|
|
public void testBlockingAcquire() throws InterruptedException {
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(1);
|
|
|
|
s.trySetPermits(1);
|
|
|
|
s.acquire();
|
|
|
|
s.acquire();
|
|
|
|
|
|
|
|
|
|
|
|
Thread t = new Thread() {
|
|
|
|
Thread t = new Thread() {
|
|
|
@ -46,7 +67,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testBlockingNAcquire() throws InterruptedException {
|
|
|
|
public void testBlockingNAcquire() throws InterruptedException {
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(5);
|
|
|
|
s.trySetPermits(5);
|
|
|
|
s.acquire(3);
|
|
|
|
s.acquire(3);
|
|
|
|
|
|
|
|
|
|
|
|
Thread t = new Thread() {
|
|
|
|
Thread t = new Thread() {
|
|
|
@ -80,7 +101,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testTryNAcquire() throws InterruptedException {
|
|
|
|
public void testTryNAcquire() throws InterruptedException {
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(5);
|
|
|
|
s.trySetPermits(5);
|
|
|
|
assertThat(s.tryAcquire(3)).isTrue();
|
|
|
|
assertThat(s.tryAcquire(3)).isTrue();
|
|
|
|
|
|
|
|
|
|
|
|
Thread t = new Thread() {
|
|
|
|
Thread t = new Thread() {
|
|
|
@ -126,7 +147,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testDrainPermits() throws InterruptedException {
|
|
|
|
public void testDrainPermits() throws InterruptedException {
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(10);
|
|
|
|
s.trySetPermits(10);
|
|
|
|
s.acquire(3);
|
|
|
|
s.acquire(3);
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(s.drainPermits()).isEqualTo(7);
|
|
|
|
assertThat(s.drainPermits()).isEqualTo(7);
|
|
|
@ -136,7 +157,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testReleaseAcquire() throws InterruptedException {
|
|
|
|
public void testReleaseAcquire() throws InterruptedException {
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(10);
|
|
|
|
s.trySetPermits(10);
|
|
|
|
s.acquire();
|
|
|
|
s.acquire();
|
|
|
|
assertThat(s.availablePermits()).isEqualTo(9);
|
|
|
|
assertThat(s.availablePermits()).isEqualTo(9);
|
|
|
|
s.release();
|
|
|
|
s.release();
|
|
|
@ -153,7 +174,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
|
|
|
|
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(1);
|
|
|
|
s.trySetPermits(1);
|
|
|
|
|
|
|
|
|
|
|
|
int iterations = 15;
|
|
|
|
int iterations = 15;
|
|
|
|
testSingleInstanceConcurrency(iterations, r -> {
|
|
|
|
testSingleInstanceConcurrency(iterations, r -> {
|
|
|
@ -178,7 +199,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
|
|
|
|
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(1);
|
|
|
|
s.trySetPermits(1);
|
|
|
|
|
|
|
|
|
|
|
|
testMultiInstanceConcurrency(16, r -> {
|
|
|
|
testMultiInstanceConcurrency(16, r -> {
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
@ -208,7 +229,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
|
|
|
|
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(1);
|
|
|
|
s.trySetPermits(1);
|
|
|
|
|
|
|
|
|
|
|
|
testMultiInstanceConcurrency(iterations, r -> {
|
|
|
|
testMultiInstanceConcurrency(iterations, r -> {
|
|
|
|
RSemaphore s1 = r.getSemaphore("test");
|
|
|
|
RSemaphore s1 = r.getSemaphore("test");
|
|
|
@ -233,7 +254,7 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
final AtomicInteger lockedCounter = new AtomicInteger();
|
|
|
|
|
|
|
|
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
RSemaphore s = redisson.getSemaphore("test");
|
|
|
|
s.setPermits(10);
|
|
|
|
s.trySetPermits(10);
|
|
|
|
|
|
|
|
|
|
|
|
final AtomicInteger checkPermits = new AtomicInteger(s.availablePermits());
|
|
|
|
final AtomicInteger checkPermits = new AtomicInteger(s.availablePermits());
|
|
|
|
final CyclicBarrier barrier = new CyclicBarrier(s.availablePermits());
|
|
|
|
final CyclicBarrier barrier = new CyclicBarrier(s.availablePermits());
|
|
|
|