INT-2472 Capture Forked Stderr in Tests

Previously, ForkUtil only captured stdout from the forked
process.

Now both stdout and stderr are captured and echoed to
the corresponding PrintStream of the calling process.

Also removed the 200ms delay between each stdout line
captured.
This commit is contained in:
Gary Russell
2012-05-01 17:09:39 -04:00
parent 14921e7598
commit c3c7a984a4

View File

@@ -21,6 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@@ -59,27 +60,14 @@ public class ForkUtil {
final Process p = proc;
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
final BufferedReader ebr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
final AtomicBoolean run = new AtomicBoolean(true);
Thread reader = new Thread(new Runnable() {
public void run() {
try {
String line = null;
do {
while ((line = br.readLine()) != null) {
System.out.println("[FORK] " + line);
}
Thread.sleep(200);
} while (run.get());
}
catch (Exception ex) {
// ignore and exit
}
}
});
Thread reader = copyStdXxx(br, run, System.out);
Thread errReader = copyStdXxx(ebr, run, System.err);
reader.start();
errReader.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
@@ -101,6 +89,27 @@ public class ForkUtil {
return proc.getOutputStream();
}
private static Thread copyStdXxx(final BufferedReader br,
final AtomicBoolean run, final PrintStream out) {
Thread reader = new Thread(new Runnable() {
public void run() {
try {
String line = null;
do {
while ((line = br.readLine()) != null) {
out.println("[FORK] " + line);
}
} while (run.get());
}
catch (Exception ex) {
// ignore and exit
}
}
});
return reader;
}
public static OutputStream cacheServer() {
return startCacheServer("org.springframework.integration.gemfire.fork.CacheServerProcess");