Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix tests on Windows #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AbstractRedisInstance implements Redis {
private Process redisProcess;
private final int port;

private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final ExecutorService executor = Executors.newFixedThreadPool(2);

protected AbstractRedisInstance(int port) {
this.port = port;
Expand All @@ -35,8 +35,8 @@ public synchronized void start() throws EmbeddedRedisException {
}
try {
redisProcess = createRedisProcessBuilder().start();
logErrors();
awaitRedisServerReady();
logErrors();
active = true;
} catch (IOException e) {
throw new EmbeddedRedisException("Failed to start Redis instance", e);
Expand All @@ -45,9 +45,12 @@ public synchronized void start() throws EmbeddedRedisException {

private void logErrors() {
final InputStream errorStream = redisProcess.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream));
Runnable printReaderTask = new PrintReaderRunnable(reader);
executor.submit(printReaderTask);
Runnable errorStreamGobbler = new StreamGobbler(errorStream, System.err, "");
executor.submit(errorStreamGobbler);

final InputStream inputStream = redisProcess.getInputStream();
Runnable inputStreamGobbler = new StreamGobbler(inputStream, System.err, "");
executor.submit(inputStreamGobbler);
}

private void awaitRedisServerReady() throws IOException {
Expand All @@ -60,9 +63,10 @@ private void awaitRedisServerReady() throws IOException {
//Something goes wrong. Stream is ended before server was activated.
throw new RuntimeException("Can't start redis server. Check logs for details.");
}
System.out.println(outputLine);
} while (!outputLine.matches(redisReadyPattern()));
} finally {
IOUtils.closeQuietly(reader);
// IOUtils.closeQuietly(reader);
}
}

Expand All @@ -78,6 +82,9 @@ private ProcessBuilder createRedisProcessBuilder() {
@Override
public synchronized void stop() throws EmbeddedRedisException {
if (active) {
IOUtils.closeQuietly(redisProcess.getInputStream());
IOUtils.closeQuietly(redisProcess.getOutputStream());
IOUtils.closeQuietly(redisProcess.getErrorStream());
redisProcess.destroy();
tryWaitFor();
active = false;
Expand All @@ -97,29 +104,29 @@ public List<Integer> ports() {
return Arrays.asList(port);
}

private static class PrintReaderRunnable implements Runnable {
private final BufferedReader reader;
private static class StreamGobbler implements Runnable {

private PrintReaderRunnable(BufferedReader reader) {
this.reader = reader;
}
private final InputStream is;
private final PrintStream os;
private final String prefix;

public void run() {
try {
readLines();
} finally {
IOUtils.closeQuietly(reader);
}
private StreamGobbler(InputStream is, PrintStream os, String prefix) {
this.is = is;
this.os = os;
this.prefix = prefix;
}

public void readLines() {
@Override
public void run() {
try {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
os.println(prefix + line);
}
} catch (IOException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Expand Down