|
|
|
@ -7,9 +7,8 @@ import org.redisson.api.RedissonClient;
|
|
|
|
|
import org.redisson.client.*;
|
|
|
|
|
import org.redisson.client.protocol.RedisCommands;
|
|
|
|
|
import org.redisson.config.Config;
|
|
|
|
|
import org.redisson.connection.balancer.RandomLoadBalancer;
|
|
|
|
|
import org.testcontainers.containers.GenericContainer;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
@ -81,19 +80,13 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSubscriptionsPerConnection() throws InterruptedException, IOException {
|
|
|
|
|
RedisRunner.RedisProcess runner = new RedisRunner()
|
|
|
|
|
.port(RedisRunner.findFreePort())
|
|
|
|
|
.nosave()
|
|
|
|
|
.randomDir()
|
|
|
|
|
.run();
|
|
|
|
|
|
|
|
|
|
public void testSubscriptionsPerConnection() throws InterruptedException {
|
|
|
|
|
Config config = new Config();
|
|
|
|
|
config.useSingleServer()
|
|
|
|
|
.setSubscriptionConnectionPoolSize(1)
|
|
|
|
|
.setSubscriptionConnectionMinimumIdleSize(1)
|
|
|
|
|
.setSubscriptionsPerConnection(1)
|
|
|
|
|
.setAddress(runner.getRedisServerAddressAndPort());
|
|
|
|
|
.setAddress(redisson.getConfig().useSingleServer().getAddress());
|
|
|
|
|
|
|
|
|
|
RedissonClient redisson = Redisson.create(config);
|
|
|
|
|
ExecutorService e = Executors.newFixedThreadPool(32);
|
|
|
|
@ -124,7 +117,7 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
assertThat(errors.get()).isZero();
|
|
|
|
|
|
|
|
|
|
RedisClientConfig cc = new RedisClientConfig();
|
|
|
|
|
cc.setAddress(runner.getRedisServerAddressAndPort());
|
|
|
|
|
cc.setAddress(redisson.getConfig().useSingleServer().getAddress());
|
|
|
|
|
RedisClient c = RedisClient.create(cc);
|
|
|
|
|
RedisConnection ccc = c.connect();
|
|
|
|
|
List<String> channels = ccc.sync(RedisCommands.PUBSUB_CHANNELS);
|
|
|
|
@ -132,20 +125,13 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
c.shutdown();
|
|
|
|
|
|
|
|
|
|
redisson.shutdown();
|
|
|
|
|
runner.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSinglePubSub() throws IOException, InterruptedException, ExecutionException {
|
|
|
|
|
RedisRunner.RedisProcess runner = new RedisRunner()
|
|
|
|
|
.port(RedisRunner.findFreePort())
|
|
|
|
|
.nosave()
|
|
|
|
|
.randomDir()
|
|
|
|
|
.run();
|
|
|
|
|
|
|
|
|
|
public void testSinglePubSub() throws InterruptedException, ExecutionException {
|
|
|
|
|
Config config = new Config();
|
|
|
|
|
config.useSingleServer()
|
|
|
|
|
.setAddress(runner.getRedisServerAddressAndPort())
|
|
|
|
|
.setAddress(redisson.getConfig().useSingleServer().getAddress())
|
|
|
|
|
.setSubscriptionConnectionPoolSize(1)
|
|
|
|
|
.setSubscriptionsPerConnection(1);
|
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
|
|
|
@ -165,7 +151,6 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
|
|
|
|
|
assertThat(hasFails).isFalse();
|
|
|
|
|
redissonClient.shutdown();
|
|
|
|
|
runner.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -193,23 +178,25 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testRedisFailed() {
|
|
|
|
|
Assertions.assertThrows(WriteRedisConnectionException.class, () -> {
|
|
|
|
|
RedisRunner.RedisProcess master = new RedisRunner()
|
|
|
|
|
.port(6377)
|
|
|
|
|
.nosave()
|
|
|
|
|
.randomDir()
|
|
|
|
|
.run();
|
|
|
|
|
GenericContainer<?> redis =
|
|
|
|
|
new GenericContainer<>("redis:7.2")
|
|
|
|
|
.withExposedPorts(6379);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
Config config = new Config();
|
|
|
|
|
config.useSingleServer().setAddress("redis://127.0.0.1:6377");
|
|
|
|
|
RedissonClient redisson = Redisson.create(config);
|
|
|
|
|
Config config = new Config();
|
|
|
|
|
config.useSingleServer().setAddress("redis://127.0.0.1:" + redis.getFirstMappedPort());
|
|
|
|
|
RedissonClient redisson = Redisson.create(config);
|
|
|
|
|
|
|
|
|
|
Assertions.assertThrows(WriteRedisConnectionException.class, () -> {
|
|
|
|
|
|
|
|
|
|
RLock lock = redisson.getLock("myLock");
|
|
|
|
|
// kill RedisServer while main thread is sleeping.
|
|
|
|
|
master.stop();
|
|
|
|
|
redis.stop();
|
|
|
|
|
Thread.sleep(3000);
|
|
|
|
|
lock.tryLock(5, 10, TimeUnit.SECONDS);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
redisson.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -223,7 +210,7 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
lock.tryLock(3, TimeUnit.SECONDS);
|
|
|
|
|
assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
|
|
|
|
|
assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3350L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -289,35 +276,13 @@ public class RedissonLockTest extends BaseConcurrentTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInCluster() throws Exception {
|
|
|
|
|
RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
|
|
|
|
|
RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
|
|
|
|
|
RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
|
|
|
|
|
RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
|
|
|
|
|
RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
|
|
|
|
|
RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
|
|
|
|
|
|
|
|
|
|
ClusterRunner clusterRunner = new ClusterRunner()
|
|
|
|
|
.addNode(master1, slave1)
|
|
|
|
|
.addNode(master2, slave2)
|
|
|
|
|
.addNode(master3, slave3);
|
|
|
|
|
ClusterRunner.ClusterProcesses process = clusterRunner.run();
|
|
|
|
|
|
|
|
|
|
Thread.sleep(5000);
|
|
|
|
|
|
|
|
|
|
Config config = new Config();
|
|
|
|
|
config.useClusterServers()
|
|
|
|
|
.setLoadBalancer(new RandomLoadBalancer())
|
|
|
|
|
.addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
|
|
|
|
|
RedissonClient redisson = Redisson.create(config);
|
|
|
|
|
|
|
|
|
|
RLock lock = redisson.getLock("myLock");
|
|
|
|
|
lock.lock();
|
|
|
|
|
assertThat(lock.isLocked()).isTrue();
|
|
|
|
|
lock.unlock();
|
|
|
|
|
assertThat(lock.isLocked()).isFalse();
|
|
|
|
|
|
|
|
|
|
redisson.shutdown();
|
|
|
|
|
process.shutdown();
|
|
|
|
|
testInCluster(redisson -> {
|
|
|
|
|
RLock lock = redisson.getLock("myLock");
|
|
|
|
|
lock.lock();
|
|
|
|
|
assertThat(lock.isLocked()).isTrue();
|
|
|
|
|
lock.unlock();
|
|
|
|
|
assertThat(lock.isLocked()).isFalse();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|