diff --git a/src/test/java/org/redisson/BaseReactiveTest.java b/src/test/java/org/redisson/BaseReactiveTest.java index 93f78cd80..73655b02e 100644 --- a/src/test/java/org/redisson/BaseReactiveTest.java +++ b/src/test/java/org/redisson/BaseReactiveTest.java @@ -20,17 +20,42 @@ import reactor.rx.Streams; public abstract class BaseReactiveTest { protected RedissonReactiveClient redisson; + protected static RedissonReactiveClient defaultRedisson; + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson = createInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson.shutdown(); + } + } @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); - redisson = createInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } else { + if (redisson == null) { + redisson = defaultRedisson; + } + redisson.getKeys().flushall(); + } } @After public void after() throws InterruptedException { - redisson.shutdown(); - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + redisson.shutdown(); + RedisRunner.shutDownDefaultRedisTestInstance(); + } } public Iterable sync(RScoredSortedSetReactive list) { diff --git a/src/test/java/org/redisson/BaseTest.java b/src/test/java/org/redisson/BaseTest.java index 8b4162ab3..ba3e0d7f2 100644 --- a/src/test/java/org/redisson/BaseTest.java +++ b/src/test/java/org/redisson/BaseTest.java @@ -2,22 +2,49 @@ package org.redisson; import java.io.IOException; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; public abstract class BaseTest { protected RedissonClient redisson; - + protected static RedissonClient defaultRedisson; + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson = createInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson.shutdown(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); - redisson = createInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } else { + if (redisson == null) { + redisson = defaultRedisson; + } + redisson.getKeys().flushall(); + } } @After public void after() throws InterruptedException { - redisson.shutdown(); - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + redisson.shutdown(); + RedisRunner.shutDownDefaultRedisTestInstance(); + } } public static Config createConfig() { diff --git a/src/test/java/org/redisson/RedisClientTest.java b/src/test/java/org/redisson/RedisClientTest.java index 80198ed8a..be464ba01 100644 --- a/src/test/java/org/redisson/RedisClientTest.java +++ b/src/test/java/org/redisson/RedisClientTest.java @@ -29,26 +29,40 @@ import io.netty.util.concurrent.FutureListener; import io.netty.util.concurrent.Promise; import java.io.IOException; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; public class RedisClientTest { + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } } @After public void after() throws InterruptedException { - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.shutDownDefaultRedisTestInstance(); + } } -// @After -// public void after() throws InterruptedException, IOException { -// afterClass(); -// beforeClass(); -// } - @Test public void testConnectAsync() throws InterruptedException { RedisClient c = new RedisClient("localhost", 6379); diff --git a/src/test/java/org/redisson/RedisRunner.java b/src/test/java/org/redisson/RedisRunner.java index b67ca271e..12cc037be 100644 --- a/src/test/java/org/redisson/RedisRunner.java +++ b/src/test/java/org/redisson/RedisRunner.java @@ -9,7 +9,6 @@ import java.net.URL; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -18,7 +17,6 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import org.redisson.client.RedisClient; -import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisStrictCommand; import org.redisson.client.protocol.convertor.VoidReplayConvertor; @@ -169,7 +167,6 @@ public class RedisRunner { A } - private static final String redisBinary; private final LinkedHashMap options = new LinkedHashMap<>(); private static RedisRunner.RedisProcess defaultRedisInstance; private static int defaultRedisInstanceExitCode; @@ -179,14 +176,9 @@ public class RedisRunner { private boolean randomDir = false; private ArrayList bindAddr = new ArrayList<>(); private int port = 6379; - - static { - redisBinary = Optional.ofNullable(System.getProperty("redisBinary")) - .orElse("C:\\Devel\\projects\\redis\\Redis-x64-3.0.500\\redis-server.exe"); - } { - this.options.put(REDIS_OPTIONS.BINARY_PATH, redisBinary); + this.options.put(REDIS_OPTIONS.BINARY_PATH, RedissonRuntimeEnvironment.redisBinaryPath); } /** @@ -206,23 +198,24 @@ public class RedisRunner { */ public static RedisProcess runRedisWithConfigFile(String configPath) throws IOException, InterruptedException { URL resource = RedisRunner.class.getResource(configPath); - return runWithOptions(new RedisRunner(), redisBinary, resource.getFile()); + return runWithOptions(new RedisRunner(), RedissonRuntimeEnvironment.redisBinaryPath, resource.getFile()); } private static RedisProcess runWithOptions(RedisRunner runner, String... options) throws IOException, InterruptedException { + System.out.println("REDIS VER: " + RedissonRuntimeEnvironment.redisFullVersion); List launchOptions = Arrays.stream(options) .map(x -> Arrays.asList(x.split(" "))).flatMap(x -> x.stream()) .collect(Collectors.toList()); System.out.println("REDIS LAUNCH OPTIONS: " + Arrays.toString(launchOptions.toArray())); ProcessBuilder master = new ProcessBuilder(launchOptions) .redirectErrorStream(true) - .directory(new File(System.getProperty("java.io.tmpdir"))); + .directory(new File(RedissonRuntimeEnvironment.tempDir)); Process p = master.start(); new Thread(() -> { BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; try { - while (p.isAlive() && (line = reader.readLine()) != null && !"true".equalsIgnoreCase(System.getProperty("travisEnv"))) { + while (p.isAlive() && (line = reader.readLine()) != null && !RedissonRuntimeEnvironment.isTravis) { System.out.println("REDIS PROCESS: " + line); } } catch (IOException ex) { @@ -688,7 +681,7 @@ public class RedisRunner { } private void makeRandomDefaultDir() { - File f = new File(System.getProperty("java.io.tmpdir") + "/" + UUID.randomUUID()); + File f = new File(RedissonRuntimeEnvironment.tempDir + "/" + UUID.randomUUID()); if (f.exists()) { makeRandomDefaultDir(); } else { @@ -730,7 +723,7 @@ public class RedisRunner { } private boolean isWindows() { - return System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH).contains("win"); + return RedissonRuntimeEnvironment.isWindows; } } @@ -762,4 +755,7 @@ public class RedisRunner { return defaultRedisInstanceExitCode; } + public static boolean isDefaultRedisTestInstanceRunning() { + return defaultRedisInstance != null && defaultRedisInstance.redisProcess.isAlive(); + } } diff --git a/src/test/java/org/redisson/RedissonCountDownLatchConcurrentTest.java b/src/test/java/org/redisson/RedissonCountDownLatchConcurrentTest.java index 021998135..7453f84e5 100644 --- a/src/test/java/org/redisson/RedissonCountDownLatchConcurrentTest.java +++ b/src/test/java/org/redisson/RedissonCountDownLatchConcurrentTest.java @@ -6,22 +6,42 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.redisson.core.RCountDownLatch; public class RedissonCountDownLatchConcurrentTest { + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } } @After public void after() throws InterruptedException { - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.shutDownDefaultRedisTestInstance(); + } } @Test diff --git a/src/test/java/org/redisson/RedissonRuntimeEnvironment.java b/src/test/java/org/redisson/RedissonRuntimeEnvironment.java new file mode 100644 index 000000000..63d674b4f --- /dev/null +++ b/src/test/java/org/redisson/RedissonRuntimeEnvironment.java @@ -0,0 +1,29 @@ +package org.redisson; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RedissonRuntimeEnvironment { + + public static final boolean isTravis = "true".equalsIgnoreCase(System.getProperty("travisEnv")); + public static final String redisBinaryPath = System.getProperty("redisBinary", "C:\\Devel\\projects\\redis\\Redis-x64-3.0.500\\redis-server.exe"); + 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"); + } +} diff --git a/src/test/java/org/redisson/RedissonTest.java b/src/test/java/org/redisson/RedissonTest.java index 1e2f53802..fa162a67b 100644 --- a/src/test/java/org/redisson/RedissonTest.java +++ b/src/test/java/org/redisson/RedissonTest.java @@ -11,10 +11,12 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.redisson.RedisRunner.RedisProcess; import org.redisson.client.RedisConnectionException; @@ -28,18 +30,45 @@ import org.redisson.core.NodesGroup; public class RedissonTest { - RedissonClient redisson; + protected RedissonClient redisson; + protected static RedissonClient defaultRedisson; + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson = BaseTest.createInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + defaultRedisson.shutdown(); + } + } @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } else { + if (redisson == null) { + redisson = defaultRedisson; + } + redisson.getKeys().flushall(); + } } @After public void after() throws InterruptedException { - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + redisson.shutdown(); + RedisRunner.shutDownDefaultRedisTestInstance(); + } } - + public static class Dummy { private String field; } diff --git a/src/test/java/org/redisson/RedissonTopicPatternTest.java b/src/test/java/org/redisson/RedissonTopicPatternTest.java index b96078484..611c162ea 100644 --- a/src/test/java/org/redisson/RedissonTopicPatternTest.java +++ b/src/test/java/org/redisson/RedissonTopicPatternTest.java @@ -4,9 +4,11 @@ import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.redisson.core.BasePatternStatusListener; import org.redisson.core.MessageListener; @@ -15,15 +17,33 @@ import org.redisson.core.RPatternTopic; import org.redisson.core.RTopic; public class RedissonTopicPatternTest { - + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } } @After public void after() throws InterruptedException { - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.shutDownDefaultRedisTestInstance(); + } } public static class Message { diff --git a/src/test/java/org/redisson/RedissonTopicTest.java b/src/test/java/org/redisson/RedissonTopicTest.java index 6268a28fe..43665ce02 100644 --- a/src/test/java/org/redisson/RedissonTopicTest.java +++ b/src/test/java/org/redisson/RedissonTopicTest.java @@ -5,9 +5,11 @@ import java.io.Serializable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.redisson.core.BaseStatusListener; import org.redisson.core.MessageListener; @@ -15,15 +17,33 @@ import org.redisson.core.RSet; import org.redisson.core.RTopic; public class RedissonTopicTest { - + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } } @After public void after() throws InterruptedException { - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.shutDownDefaultRedisTestInstance(); + } } public static class Message implements Serializable { diff --git a/src/test/java/org/redisson/RedissonTwoLockedThread.java b/src/test/java/org/redisson/RedissonTwoLockedThread.java index 4b3b4abf4..82fcc0a2f 100644 --- a/src/test/java/org/redisson/RedissonTwoLockedThread.java +++ b/src/test/java/org/redisson/RedissonTwoLockedThread.java @@ -15,6 +15,8 @@ import org.redisson.core.RLock; import java.util.Arrays; import java.util.concurrent.CountDownLatch; +import org.junit.AfterClass; +import org.junit.BeforeClass; @RunWith(Parameterized.class) public class RedissonTwoLockedThread { @@ -29,9 +31,25 @@ public class RedissonTwoLockedThread { private RedissonClient redisson; + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + + @AfterClass + public static void afterClass() throws IOException, InterruptedException { + if (!RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } + } + @Before public void before() throws IOException, InterruptedException { - RedisRunner.startDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.startDefaultRedisTestInstance(); + } Config config = BaseTest.createConfig(); config.setCodec(codec); redisson = Redisson.create(config); @@ -40,7 +58,9 @@ public class RedissonTwoLockedThread { @After public void after() throws InterruptedException { redisson.shutdown(); - RedisRunner.shutDownDefaultRedisTestInstance(); + if (RedissonRuntimeEnvironment.isTravis) { + RedisRunner.shutDownDefaultRedisTestInstance(); + } } @Test(timeout = 3000)