From c3c7a984a406e7109be5f3947b307e0bc9ec8091 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 1 May 2012 17:09:39 -0400 Subject: [PATCH] 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. --- .../integration/gemfire/fork/ForkUtil.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java index ea0f580b1c..5e367e8263 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java @@ -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");