Skip to content

Commit 546d305

Browse files
committed
Improved error handing for failing runs
1 parent a543701 commit 546d305

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/main/java/redis/embedded/RedisInstance.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.InputStream;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.function.Consumer;
@@ -40,9 +41,10 @@ public synchronized void start() throws IOException {
4041
.directory(new File(args.get(0)).getParentFile())
4142
.start();
4243
addShutdownHook("RedisInstanceCleaner", checkedToRuntime(this::stop));
44+
awaitServerReady(process, readyPattern, soutListener, serrListener);
45+
4346
if (serrListener != null)
4447
newDaemonThread(() -> logStream(process.getErrorStream(), serrListener)).start();
45-
awaitServerReady(process, readyPattern, soutListener);
4648
if (soutListener != null)
4749
newDaemonThread(() -> logStream(process.getInputStream(), soutListener)).start();
4850

@@ -52,11 +54,22 @@ public synchronized void start() throws IOException {
5254
}
5355
}
5456

55-
private static void awaitServerReady(final Process process, final Pattern readyPattern,
56-
final Consumer<String> soutListener) throws IOException {
57+
private static void awaitServerReady(final Process process, final Pattern readyPattern
58+
, final Consumer<String> soutListener, final Consumer<String> serrListener) throws IOException {
5759
final StringBuilder log = new StringBuilder();
58-
if (!findMatchInStream(process.getInputStream(), readyPattern, soutListener, log))
59-
throw new IOException("Ready pattern not found in log. Startup log: " + log);
60+
if (!findMatchInStream(process.getInputStream(), readyPattern, soutListener, log)) {
61+
final String stdOut = log.toString();
62+
final String stdErr = readFully(process.getErrorStream(), serrListener);
63+
64+
throw new IOException("Redis-server process appears not to have started. "
65+
+ (isNullOrEmpty(stdOut) ? "No output was found in standard-out." : "stdandard-out contains this: " + stdOut)
66+
+ " "
67+
+ (isNullOrEmpty(stdErr) ? "No output was found in standard-err." : "stdandard-err contains this: " + stdErr)
68+
);
69+
}
70+
}
71+
private static boolean isNullOrEmpty(final String value) {
72+
return value == null || value.isEmpty();
6073
}
6174

6275
public synchronized void stop() throws IOException {

src/main/java/redis/embedded/util/IO.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public static boolean findMatchInStream(final InputStream in, final Pattern patt
7474
return false;
7575
}
7676

77+
public static String readFully(final InputStream in, final Consumer<String> listener) {
78+
final StringBuilder ret = new StringBuilder();
79+
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
80+
String line; while ((line = reader.readLine()) != null) {
81+
if (listener != null) listener.accept(line);
82+
ret.append(line);
83+
}
84+
} catch (final IOException ignored) {}
85+
return ret.toString();
86+
}
87+
7788
public static Stream<String> processToLines(final String command) throws IOException {
7889
final Process proc = Runtime.getRuntime().exec(command);
7990
return new BufferedReader(new InputStreamReader(proc.getInputStream())).lines();

0 commit comments

Comments
 (0)