From c4368ffbece38f57b260fd1183ee5231cc83bc42 Mon Sep 17 00:00:00 2001 From: "piot.turek" Date: Thu, 22 Jan 2015 18:25:09 +0100 Subject: [PATCH] redis sentinel test init; ok pattern introduced for sentinel --- .../redis/embedded/AbstractRedisInstance.java | 36 +++++++++++++------ .../java/redis/embedded/RedisSentinel.java | 7 ++++ .../redis/embedded/RedisSentinelBuilder.java | 4 +-- src/main/java/redis/embedded/RedisServer.java | 12 +++++++ .../redis/embedded/RedisSentinelTest.java | 21 ++++++----- 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/main/java/redis/embedded/AbstractRedisInstance.java b/src/main/java/redis/embedded/AbstractRedisInstance.java index 18764d4b..e410d978 100644 --- a/src/main/java/redis/embedded/AbstractRedisInstance.java +++ b/src/main/java/redis/embedded/AbstractRedisInstance.java @@ -1,21 +1,21 @@ package redis.embedded; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.Collections; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * Created by piotrturek on 22/01/15. */ abstract class AbstractRedisInstance implements RedisInstance { - private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*"; protected List args = Collections.emptyList(); private volatile boolean active = false; private Process redisProcess; + private final ExecutorService executor = Executors.newSingleThreadExecutor(); + @Override public boolean isActive() { return active; @@ -27,29 +27,45 @@ public synchronized void start() throws IOException { throw new RuntimeException("This redis server instance is already running..."); } redisProcess = createRedisProcessBuilder().start(); + logErrors(); awaitRedisServerReady(); active = true; } - private void awaitRedisServerReady() throws IOException { + private void logErrors() { + final InputStream errorStream = redisProcess.getErrorStream(); + executor.submit(() -> { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) { + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + }); + + } + + private void awaitRedisServerReady() throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()))) { String outputLine; do { outputLine = reader.readLine(); - if (outputLine == null) { //Something goes wrong. Stream is ended before server was activated. throw new RuntimeException("Can't start redis server. Check logs for details."); } - } while (!outputLine.matches(REDIS_READY_PATTERN)); + } while (!outputLine.matches(redisReadyPattern())); } } - private ProcessBuilder createRedisProcessBuilder() { + protected abstract String redisReadyPattern(); + + private ProcessBuilder createRedisProcessBuilder() { File executable = new File(args.get(0)); ProcessBuilder pb = new ProcessBuilder(args); pb.directory(executable.getParentFile()); - pb.redirectErrorStream(); return pb; } diff --git a/src/main/java/redis/embedded/RedisSentinel.java b/src/main/java/redis/embedded/RedisSentinel.java index 07e640d5..82b9ac47 100644 --- a/src/main/java/redis/embedded/RedisSentinel.java +++ b/src/main/java/redis/embedded/RedisSentinel.java @@ -7,9 +7,16 @@ * Created by piotrturek on 22/01/15. */ public class RedisSentinel extends AbstractRedisInstance { + private static final String REDIS_READY_PATTERN = ".*Sentinel runid is.*"; + public RedisSentinel(List args) { this.args = new ArrayList<>(args); } public static RedisSentinelBuilder builder() { return new RedisSentinelBuilder(); } + + @Override + protected String redisReadyPattern() { + return REDIS_READY_PATTERN; + } } diff --git a/src/main/java/redis/embedded/RedisSentinelBuilder.java b/src/main/java/redis/embedded/RedisSentinelBuilder.java index 21306d55..d9d9c4d3 100644 --- a/src/main/java/redis/embedded/RedisSentinelBuilder.java +++ b/src/main/java/redis/embedded/RedisSentinelBuilder.java @@ -19,7 +19,7 @@ public class RedisSentinelBuilder { private static final String MASTER_MONITOR_LINE = "sentinel monitor %s 127.0.0.1 %d 1"; private static final String DOWN_AFTER_LINE = "sentinel down-after-milliseconds %s %d"; private static final String FAILOVER_LINE = "sentinel failover-timeout %s %d"; - private static final String PARALLEL_SYNCS_LINE = "parallel-syncs %s %d"; + private static final String PARALLEL_SYNCS_LINE = "sentinel parallel-syncs %s %d"; private File executable; private Integer port; @@ -129,9 +129,9 @@ private List buildCommandArgs() { Preconditions.checkNotNull(sentinelConf); List args = new ArrayList<>(); + args.add(executable.getAbsolutePath()); args.add(sentinelConf); args.add("--sentinel"); - args.add(executable.getAbsolutePath()); if (port != null) { args.add("--port"); diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index ff7e4f07..eb115742 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -9,6 +9,13 @@ import java.util.List; public class RedisServer extends AbstractRedisInstance { + private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*"; + private static final int DEFAULT_REDIS_PORT = 6379; + + public RedisServer() throws IOException { + this(DEFAULT_REDIS_PORT); + } + public RedisServer(Integer port) throws IOException { File executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); this.args = Arrays.asList( @@ -31,4 +38,9 @@ public RedisServer(File executable, Integer port) { public static RedisServerBuilder builder() { return new RedisServerBuilder(); } + + @Override + protected String redisReadyPattern() { + return REDIS_READY_PATTERN; + } } diff --git a/src/test/java/redis/embedded/RedisSentinelTest.java b/src/test/java/redis/embedded/RedisSentinelTest.java index 6bac326b..30ecc2c8 100644 --- a/src/test/java/redis/embedded/RedisSentinelTest.java +++ b/src/test/java/redis/embedded/RedisSentinelTest.java @@ -1,17 +1,20 @@ package redis.embedded; -import org.junit.After; -import org.junit.Before; +import org.junit.Test; public class RedisSentinelTest { + private RedisSentinel sentinel; + private RedisServer server; - @Before - public void setUp() throws Exception { - + @Test(timeout = 3000L) + public void testSimpleRun() throws Exception { + server = new RedisServer(); + sentinel = RedisSentinel.builder().build(); + sentinel.start(); + server.start(); + Thread.sleep(1000L); + server.stop(); + sentinel.stop(); } - @After - public void tearDown() throws Exception { - - } } \ No newline at end of file