Skip to content

Commit

Permalink
Redis server now runs on a random port
Browse files Browse the repository at this point in the history
added one more timeout
  • Loading branch information
jackygurui committed Sep 13, 2016
1 parent 780b58e commit 6f7f55a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 31 deletions.
10 changes: 5 additions & 5 deletions redisson/src/test/java/org/redisson/BaseTest.java
Expand Up @@ -61,15 +61,15 @@ public void after() throws InterruptedException {
}

public static Config createConfig() {
String redisAddress = System.getProperty("redisAddress");
if (redisAddress == null) {
redisAddress = "127.0.0.1:6379";
}
// String redisAddress = System.getProperty("redisAddress");
// if (redisAddress == null) {
// redisAddress = "127.0.0.1:6379";
// }
Config config = new Config();
// config.setCodec(new MsgPackJacksonCodec());
// config.useSentinelServers().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26379", "127.0.0.1:26389");
// config.useClusterServers().addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001", "127.0.0.1:7000");
config.useSingleServer().setAddress(redisAddress);
config.useSingleServer().setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
// .setPassword("mypass1");
// config.useMasterSlaveConnection()
// .setMasterAddress("127.0.0.1:6379")
Expand Down
12 changes: 6 additions & 6 deletions redisson/src/test/java/org/redisson/RedisClientTest.java
Expand Up @@ -75,7 +75,7 @@ public void after() throws InterruptedException {

@Test
public void testConnectAsync() throws InterruptedException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RFuture<RedisConnection> f = c.connectAsync();
final CountDownLatch l = new CountDownLatch(1);
f.addListener((FutureListener<RedisConnection>) future -> {
Expand All @@ -88,7 +88,7 @@ public void testConnectAsync() throws InterruptedException {

@Test
public void testSubscribe() throws InterruptedException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisPubSubConnection pubSubConnection = c.connectPubSub();
final CountDownLatch latch = new CountDownLatch(2);
pubSubConnection.addListener(new RedisPubSubListener<Object>() {
Expand Down Expand Up @@ -116,7 +116,7 @@ public void onPatternMessage(String pattern, String channel, Object message) {

@Test
public void test() throws InterruptedException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
final RedisConnection conn = c.connect();

conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
Expand All @@ -138,7 +138,7 @@ public void test() throws InterruptedException {

@Test
public void testPipeline() throws InterruptedException, ExecutionException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisConnection conn = c.connect();

conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
Expand Down Expand Up @@ -166,7 +166,7 @@ public void testPipeline() throws InterruptedException, ExecutionException {

@Test
public void testBigRequest() throws InterruptedException, ExecutionException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisConnection conn = c.connect();

for (int i = 0; i < 50; i++) {
Expand All @@ -181,7 +181,7 @@ public void testBigRequest() throws InterruptedException, ExecutionException {

@Test
public void testPipelineBigResponse() throws InterruptedException, ExecutionException {
RedisClient c = new RedisClient("localhost", 6379);
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedisConnection conn = c.connect();

List<CommandData<?, ?>> commands = new ArrayList<CommandData<?, ?>>();
Expand Down
65 changes: 55 additions & 10 deletions redisson/src/test/java/org/redisson/RedisRunner.java
Expand Up @@ -32,7 +32,6 @@ public enum REDIS_OPTIONS {
DAEMONIZE,
PIDFILE,
PORT,
RANDOM_PORT,
TCP_BACKLOG,
BIND(true),
UNIXSOCKET,
Expand Down Expand Up @@ -181,7 +180,8 @@ public enum KEYSPACE_EVENTS_OPTIONS {
private boolean randomDir = false;
private ArrayList<String> bindAddr = new ArrayList<>();
private int port = 6379;
private boolean randomPort = true;
private int retryCount = Integer.MAX_VALUE;
private boolean randomPort = false;

{
this.options.put(REDIS_OPTIONS.BINARY_PATH, RedissonRuntimeEnvironment.redisBinaryPath);
Expand Down Expand Up @@ -231,11 +231,31 @@ private static RedisProcess runWithOptions(RedisRunner runner, String... options
return new RedisProcess(p, runner);
}

public RedisProcess run() throws IOException, InterruptedException {
public RedisProcess run() throws IOException, InterruptedException, FailedToStartRedisException {
if (!options.containsKey(REDIS_OPTIONS.DIR)) {
options.put(REDIS_OPTIONS.DIR, defaultDir);
addConfigOption(REDIS_OPTIONS.DIR, defaultDir);
}
if (randomPort) {
for (int i = 0; i < retryCount; i++) {
this.port = findFreePort();
addConfigOption(REDIS_OPTIONS.PORT, this.port);
try {
return runAndCheck();
} catch (FailedToStartRedisException e) {
}
}
throw new FailedToStartRedisException();
} else {
return runAndCheck();
}
return runWithOptions(this, options.values().toArray(new String[0]));
}

public RedisProcess runAndCheck() throws IOException, InterruptedException, FailedToStartRedisException {
RedisProcess rp = runWithOptions(this, options.values().toArray(new String[0]));
if (rp.redisProcess.waitFor(1000, TimeUnit.MILLISECONDS)) {
throw new FailedToStartRedisException();
}
return rp;
}

private void addConfigOption(REDIS_OPTIONS option, Object... args) {
Expand Down Expand Up @@ -275,7 +295,12 @@ public RedisRunner port(int port) {
}

public RedisRunner randomPort() {
return randomPort(Integer.MAX_VALUE);
}

public RedisRunner randomPort(int retryCount) {
this.randomPort = true;
this.retryCount = retryCount;
options.remove(REDIS_OPTIONS.PORT);
return this;
}
Expand Down Expand Up @@ -753,13 +778,20 @@ public RedisVersion getRedisVersion() {
}
return redisVersion;
}


public int getRedisServerPort() {
return runner.getPort();
}

public String getRedisServerBindAddress() {
return runner.getInitialBindAddr();
}
}

public static RedisRunner.RedisProcess startDefaultRedisServerInstance() throws IOException, InterruptedException {
public static RedisRunner.RedisProcess startDefaultRedisServerInstance() throws IOException, InterruptedException, FailedToStartRedisException {
if (defaultRedisInstance == null) {
System.out.println("REDIS RUNNER: Starting up default instance...");
defaultRedisInstance = new RedisRunner().nosave().randomDir().run();
defaultRedisInstance = new RedisRunner().nosave().randomDir().randomPort().run();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
shutDownDefaultRedisServerInstance();
Expand Down Expand Up @@ -795,8 +827,14 @@ public static RedisClient createDefaultRedisClientInstance() {
public static RedisRunner.RedisProcess getDefaultRedisServerInstance() {
return defaultRedisInstance;
}

public static String getDefaultRedisServerBindAddressAndPort() {
return defaultRedisInstance.getRedisServerBindAddress()
+ ":"
+ defaultRedisInstance.getRedisServerPort();
}

private static int findFreePort() {
public static int findFreePort() {
ServerSocket socket = null;
try {
socket = new ServerSocket(0);
Expand All @@ -817,6 +855,13 @@ private static int findFreePort() {
}
}
}
throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on");
throw new IllegalStateException("Could not find a free TCP/IP port.");
}

public static class FailedToStartRedisException extends RuntimeException {

private FailedToStartRedisException() {
}
}

}
8 changes: 4 additions & 4 deletions redisson/src/test/java/org/redisson/RedissonTest.java
Expand Up @@ -117,7 +117,7 @@ public static class Dummy {
@Test(expected = RedisException.class)
public void testSer() {
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
config.useSingleServer().setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
config.setCodec(new SerializationCodec());
RedissonClient r = Redisson.create(config);
r.getMap("test").put("1", new Dummy());
Expand Down Expand Up @@ -163,7 +163,7 @@ public void testMemoryCommand() throws IOException, InterruptedException {
public void testConfigValidation() {
Config redissonConfig = new Config();
redissonConfig.useSingleServer()
.setAddress("127.0.0.1:6379")
.setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort())
.setConnectionPoolSize(2);
Redisson.create(redissonConfig);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public void onConnect(InetSocketAddress addr) {
@Test
public void testShutdown() {
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
config.useSingleServer().setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());

RedissonClient r = Redisson.create(config);
Assert.assertFalse(r.isShuttingDown());
Expand Down Expand Up @@ -349,7 +349,7 @@ public void testManyConnections() {
redisConfig.useSingleServer()
.setConnectionMinimumIdleSize(10000)
.setConnectionPoolSize(10000)
.setAddress("localhost:6379");
.setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RedissonClient r = Redisson.create(redisConfig);
r.shutdown();
}
Expand Down
Expand Up @@ -7,8 +7,11 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.ClassRule;
import org.junit.Rule;

import org.junit.Test;
import org.junit.rules.Timeout;
import org.redisson.RedisRunner;
import org.redisson.RedisRunner.RedisProcess;
import org.redisson.Redisson;
Expand All @@ -18,24 +21,31 @@

public class WeightedRoundRobinBalancerTest {

@ClassRule
public static Timeout classTimeout = new Timeout(1, TimeUnit.HOURS);
@Rule
public Timeout testTimeout = new Timeout(15, TimeUnit.MINUTES);

@Test
public void testUseMasterForReadsIfNoConnectionsToSlaves() throws IOException, InterruptedException {
RedisProcess master = null;
RedisProcess slave = null;
RedissonClient client = null;
try {
master = redisTestInstance(6379);
slave = redisTestInstance(6380);
int mPort = RedisRunner.findFreePort();
int sPort = RedisRunner.findFreePort();
master = redisTestInstance(mPort);
slave = redisTestInstance(sPort);

Map<String, Integer> weights = new HashMap<>();
weights.put("127.0.0.1:6379", 1);
weights.put("127.0.0.1:6380", 2);
weights.put("127.0.0.1:" + mPort, 1);
weights.put("127.0.0.1:" + sPort, 2);

Config config = new Config();
config.useMasterSlaveServers()
.setReadMode(ReadMode.SLAVE)
.setMasterAddress("127.0.0.1:6379")
.addSlaveAddress("127.0.0.1:6380")
.setMasterAddress("127.0.0.1:" + mPort)
.addSlaveAddress("127.0.0.1:" + sPort)
.setLoadBalancer(new WeightedRoundRobinBalancer(weights, 1));

client = Redisson.create(config);
Expand Down

0 comments on commit 6f7f55a

Please sign in to comment.