Skip to content

Commit

Permalink
redis sentinel test init; ok pattern introduced for sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
turu committed Jan 22, 2015
1 parent 2bf4aec commit c4368ff
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
36 changes: 26 additions & 10 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
@@ -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<String> args = Collections.emptyList();
private volatile boolean active = false;
private Process redisProcess;

private final ExecutorService executor = Executors.newSingleThreadExecutor();

@Override
public boolean isActive() {
return active;
Expand All @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/embedded/RedisSentinel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args) {
this.args = new ArrayList<>(args);
}

public static RedisSentinelBuilder builder() { return new RedisSentinelBuilder(); }

@Override
protected String redisReadyPattern() {
return REDIS_READY_PATTERN;
}
}
4 changes: 2 additions & 2 deletions src/main/java/redis/embedded/RedisSentinelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,9 +129,9 @@ private List<String> buildCommandArgs() {
Preconditions.checkNotNull(sentinelConf);

List<String> args = new ArrayList<>();
args.add(executable.getAbsolutePath());
args.add(sentinelConf);
args.add("--sentinel");
args.add(executable.getAbsolutePath());

if (port != null) {
args.add("--port");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/embedded/RedisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
}
21 changes: 12 additions & 9 deletions src/test/java/redis/embedded/RedisSentinelTest.java
Original file line number Diff line number Diff line change
@@ -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 {

}
}

0 comments on commit c4368ff

Please sign in to comment.