Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
added a method to retrieve Redis server version.
renamed RedisRunner.startDefaultRedisTestInstance to
RedisRunner.startDefaultRedisServerInstance
renamed RedisRunner.shutDownDefaultRedisTestInstance to
RedisRunner.shutDownDefaultRedisServerInstance
removed redis version related code from RedissonRuntimeEnvironment
travis.yml no longer need to pass redisVersion environment to run tests
  • Loading branch information
Rui Gu committed Apr 6, 2016
1 parent 103d0f6 commit fca8889
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -218,4 +218,4 @@ before_script:
- export REDIS_VERSION="$(redis-cli INFO SERVER | sed -n 2p)"
- echo $REDIS_VERSION
- redis-cli SHUTDOWN NOSAVE
script: mvn -Dtest=$REDISSON_TEST -Dsurefire.rerunFailingTestsCount=5 -DargLine="-DredisBinary=$REDIS_BIN/redis-server -DtravisEnv=true -DredisVersion=${REDIS_VERSION}" -Punit-test test
script: mvn -Dtest=$REDISSON_TEST -Dsurefire.rerunFailingTestsCount=5 -DargLine="-DredisBinary=$REDIS_BIN/redis-server -DtravisEnv=true" -Punit-test test
8 changes: 4 additions & 4 deletions src/test/java/org/redisson/BaseReactiveTest.java
Expand Up @@ -25,23 +25,23 @@ public abstract class BaseReactiveTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
defaultRedisson = createInstance();
}
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
defaultRedisson.shutdown();
}
}

@Before
public void before() throws IOException, InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
} else {
if (redisson == null) {
redisson = defaultRedisson;
Expand All @@ -54,7 +54,7 @@ public void before() throws IOException, InterruptedException {
public void after() throws InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
redisson.shutdown();
RedisRunner.shutDownDefaultRedisTestInstance();
RedisRunner.shutDownDefaultRedisServerInstance();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/redisson/BaseTest.java
Expand Up @@ -14,23 +14,23 @@ public abstract class BaseTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
defaultRedisson = createInstance();
}
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
defaultRedisson.shutdown();
}
}

@Before
public void before() throws IOException, InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
} else {
if (redisson == null) {
redisson = defaultRedisson;
Expand All @@ -43,7 +43,7 @@ public void before() throws IOException, InterruptedException {
public void after() throws InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
redisson.shutdown();
RedisRunner.shutDownDefaultRedisTestInstance();
RedisRunner.shutDownDefaultRedisServerInstance();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/redisson/RedisClientTest.java
Expand Up @@ -38,28 +38,28 @@ public class RedisClientTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
}
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.shutDownDefaultRedisServerInstance();
}
}

@Before
public void before() throws IOException, InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
}
}

@After
public void after() throws InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.shutDownDefaultRedisTestInstance();
RedisRunner.shutDownDefaultRedisServerInstance();
}
}

Expand Down
83 changes: 68 additions & 15 deletions src/test/java/org/redisson/RedisRunner.java
Expand Up @@ -11,12 +11,13 @@
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.redisson.client.RedisClient;
import org.redisson.client.RedisConnection;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.convertor.VoidReplayConvertor;

Expand Down Expand Up @@ -202,7 +203,6 @@ public static RedisProcess runRedisWithConfigFile(String configPath) throws IOEx
}

private static RedisProcess runWithOptions(RedisRunner runner, String... options) throws IOException, InterruptedException {
System.out.println("REDIS VER: " + RedissonRuntimeEnvironment.redisFullVersion);
List<String> launchOptions = Arrays.stream(options)
.map(x -> Arrays.asList(x.split(" "))).flatMap(x -> x.stream())
.collect(Collectors.toList());
Expand Down Expand Up @@ -267,7 +267,7 @@ public RedisRunner port(int port) {
addConfigOption(REDIS_OPTIONS.PORT, port);
return this;
}

public int getPort() {
return this.port;
}
Expand All @@ -286,7 +286,7 @@ public RedisRunner bind(String bind) {
public ArrayList<String> getBindAddr() {
return this.bindAddr;
}

public RedisRunner unixsocket(String unixsocket) {
addConfigOption(REDIS_OPTIONS.UNIXSOCKET, unixsocket);
return this;
Expand Down Expand Up @@ -385,6 +385,7 @@ public RedisRunner dir(String dir) {

/**
* Phantom option
*
* @return RedisRunner
*/
public RedisRunner randomDir() {
Expand Down Expand Up @@ -671,6 +672,10 @@ public String defaultDir() {
return this.defaultDir;
}

public String getInitialBindAddr() {
return bindAddr.size() > 0 ? bindAddr.get(0) : "localhost";
}

public boolean deleteDBfileDir() {
File f = new File(defaultDir);
if (f.exists()) {
Expand All @@ -695,6 +700,10 @@ public static final class RedisProcess {

private final Process redisProcess;
private final RedisRunner runner;
private String redisFullVersion;
private Integer redisMajorVersion;
private Integer redisMinorVersion;
private Integer redisPatchVersion;

private RedisProcess(Process redisProcess, RedisRunner runner) {
this.redisProcess = redisProcess;
Expand All @@ -703,45 +712,84 @@ private RedisProcess(Process redisProcess, RedisRunner runner) {

public int stop() throws InterruptedException {
if (runner.isNosave() && !runner.isRandomDir()) {
ArrayList<String> b = runner.getBindAddr();
RedisClient c = new RedisClient(b.size() > 0 ? b.get(0) : "localhost", runner.getPort());
c.connect()
.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
RedisClient c = createDefaultRedisClientInstance();
RedisConnection connection = c.connect();
connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
.await(3, TimeUnit.SECONDS);
c.shutdown();
connection.closeAsync().syncUninterruptibly();
}
redisProcess.destroy();
int exitCode = redisProcess.isAlive() ? redisProcess.waitFor() : redisProcess.exitValue();
if (runner.isRandomDir()) {
runner.deleteDBfileDir();
}
return exitCode == 1 && isWindows() ? 0 : exitCode;
return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}

public Process getRedisProcess() {
return redisProcess;
}

private boolean isWindows() {
return RedissonRuntimeEnvironment.isWindows;
public RedisClient createRedisClientInstance() {
if (redisProcess.isAlive()) {
return new RedisClient(runner.getInitialBindAddr(), runner.getPort());
}
throw new IllegalStateException("Redis server instance is not running.");
}

public String getRedisFullVersion() {
if (redisFullVersion == null) {
redisFullVersion = createRedisClientInstance().serverInfo().get("redis_version");
}
return redisFullVersion;
}

public int getRedisMajorVersion() {
if (redisMajorVersion == null) {
parseVersion();
}
return redisMajorVersion;
}

public int getRedisMinorVersion() {
if (redisMinorVersion == null) {
parseVersion();
}
return redisMinorVersion;
}

public int getRedisPatchVersion() {
if (redisPatchVersion == null) {
parseVersion();
}
return redisPatchVersion;
}

private void parseVersion() {
Matcher matcher = Pattern.compile("^([\\d]{0,2})\\.([\\d]{0,2})\\.([\\d]{0,2})$").matcher(getRedisFullVersion());
matcher.find();
redisMajorVersion = Integer.parseInt(matcher.group(1));
redisMinorVersion = Integer.parseInt(matcher.group(2));
redisPatchVersion = Integer.parseInt(matcher.group(3));
}
}

public static RedisRunner.RedisProcess startDefaultRedisTestInstance() throws IOException, InterruptedException {
public static RedisRunner.RedisProcess startDefaultRedisServerInstance() throws IOException, InterruptedException {
if (defaultRedisInstance == null) {
System.out.println("REDIS RUNNER: Starting up default instance...");
defaultRedisInstance = new RedisRunner().nosave().randomDir().run();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
shutDownDefaultRedisTestInstance();
shutDownDefaultRedisServerInstance();
} catch (InterruptedException ex) {
}
}));
}
return defaultRedisInstance;
}

public static int shutDownDefaultRedisTestInstance() throws InterruptedException {
public static int shutDownDefaultRedisServerInstance() throws InterruptedException {
if (defaultRedisInstance != null) {
System.out.println("REDIS RUNNER: Shutting down default instance...");
try {
Expand All @@ -758,4 +806,9 @@ public static int shutDownDefaultRedisTestInstance() throws InterruptedException
public static boolean isDefaultRedisTestInstanceRunning() {
return defaultRedisInstance != null && defaultRedisInstance.redisProcess.isAlive();
}

public static RedisClient createDefaultRedisClientInstance() {
return defaultRedisInstance.createRedisClientInstance();
}

}
3 changes: 3 additions & 0 deletions src/test/java/org/redisson/RedissonBlockingQueueTest.java
Expand Up @@ -24,6 +24,9 @@
import org.redisson.core.RBlockingQueue;

import io.netty.util.concurrent.Future;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class RedissonBlockingQueueTest extends BaseTest {

Expand Down
Expand Up @@ -19,28 +19,28 @@ public class RedissonCountDownLatchConcurrentTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
}
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
}
}

@Before
public void before() throws IOException, InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.startDefaultRedisTestInstance();
RedisRunner.startDefaultRedisServerInstance();
}
}

@After
public void after() throws InterruptedException {
if (RedissonRuntimeEnvironment.isTravis) {
RedisRunner.shutDownDefaultRedisTestInstance();
RedisRunner.shutDownDefaultRedisServerInstance();
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/redisson/RedissonMultiLockTest.java
Expand Up @@ -13,6 +13,12 @@

import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.RedisRunner.RedisProcess;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;

public class RedissonMultiLockTest {

Expand Down
12 changes: 0 additions & 12 deletions src/test/java/org/redisson/RedissonRuntimeEnvironment.java
@@ -1,8 +1,6 @@
package org.redisson;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RedissonRuntimeEnvironment {

Expand All @@ -11,18 +9,8 @@ public class RedissonRuntimeEnvironment {
public static final String tempDir = System.getProperty("java.io.tmpdir");
public static final String OS;
public static final boolean isWindows;
public static final String redisFullVersion;
public static final int redisMajorVersion;
public static final int redisMinorVersion;
public static final int redisPatchVersion;

static {
redisFullVersion = System.getProperty("redisVersion", "0.0.0");
Matcher matcher = Pattern.compile("^([\\d]{0,2})\\.([\\d]{0,2})\\.([\\d]{0,2})$").matcher(redisFullVersion);
matcher.find();
redisMajorVersion = Integer.parseInt(matcher.group(1));
redisMinorVersion = Integer.parseInt(matcher.group(2));
redisPatchVersion = Integer.parseInt(matcher.group(3));
OS = System.getProperty("os.name", "generic");
isWindows = OS.toLowerCase(Locale.ENGLISH).contains("win");
}
Expand Down

0 comments on commit fca8889

Please sign in to comment.