Return-Path: X-Original-To: apmail-db-derby-commits-archive@www.apache.org Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7FFC79D41 for ; Fri, 10 Feb 2012 05:39:48 +0000 (UTC) Received: (qmail 20029 invoked by uid 500); 10 Feb 2012 05:39:46 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 19946 invoked by uid 500); 10 Feb 2012 05:39:35 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 19931 invoked by uid 99); 10 Feb 2012 05:39:27 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 05:39:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 05:39:23 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8927D23888FE; Fri, 10 Feb 2012 05:39:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1242681 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit: BaseTestCase.java SpawnedProcess.java Date: Fri, 10 Feb 2012 05:39:02 -0000 To: derby-commits@db.apache.org From: kristwaa@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120210053902.8927D23888FE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kristwaa Date: Fri Feb 10 05:39:01 2012 New Revision: 1242681 URL: http://svn.apache.org/viewvc?rev=1242681&view=rev Log: DERBY-5608: BaseTestCase.readProcessOutput should read getInputStream() and getErrorStream() in separate threads Use SpawnedProcess to read stdout/stderr from the subprocess, since it already has code to do this in separate threads. Patch file: derby-5608-1a-use_spawnedprocess.diff Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=1242681&r1=1242680&r2=1242681&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java Fri Feb 10 05:39:01 2012 @@ -28,12 +28,9 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; import java.io.Reader; import java.io.PrintStream; import java.io.PrintWriter; @@ -758,60 +755,33 @@ public abstract class BaseTestCase return ("IBM Corporation".equals( getSystemProperty("java.vendor"))); } - - /** - * Reads output from a process and returns it as a string. - * This will block until the process terminates. - * - * @param pr a running process - * @return output of the process, both STDOUT and STDERR - * @throws InterruptedException - */ - public static String readProcessOutput(Process pr) throws InterruptedException { - InputStream is = pr.getInputStream(); - InputStream es = pr.getErrorStream(); - if (is == null) { - fail("Unexpectedly receiving no text from the process"); - } - String output = ""; - try { - output += " " + inputStreamToString(is) + "\n"; - output += "" + inputStreamToString(es) + "\n"; - } catch (Exception e) { - fail("Exception accessing inputstream from process", e); - } - - // wait until the process exits - pr.waitFor(); - - return output; - } - /** - * Read contents of an input stream to a String + * Reads output from a process and returns it as a string. + *

+ * This will block until the process terminates. * - * @param is - * @return String with input stream contents - * @throws IOException - */ - private static String inputStreamToString(InputStream is) throws IOException { - - String isout = ""; - char[] ca = new char[1024]; - // Create an InputStreamReader with default encoding; we're hoping - // this to be en. - InputStreamReader inStream; - inStream = new InputStreamReader(is); - - // keep reading from the stream until all done - int charsRead; - while ((charsRead = inStream.read(ca, 0, ca.length)) != -1) { - isout = isout + new String(ca, 0, charsRead); - } - return isout; + * @param pr a running process + * @return Output of the process, both STDOUT and STDERR. + * @throws InterruptedException if interrupted while waiting for the + * subprocess or one of the output collector threads to terminate + */ + public static String readProcessOutput(Process pr) + throws InterruptedException { + SpawnedProcess wrapper = new SpawnedProcess(pr, "readProcessOutput"); + wrapper.suppressOutputOnComplete(); + try { + wrapper.complete(false); + } catch (IOException ioe) { + fail("process completion method failed", ioe); + } + String output = " " + wrapper.getFullServerOutput() + + "\n"; + output += "" + wrapper.getFullServerError() + + "\n"; + return output; } - + /** * Remove the directory and its contents. * @param path Path of the directory Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java?rev=1242681&r1=1242680&r2=1242681&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java Fri Feb 10 05:39:01 2012 @@ -41,6 +41,8 @@ public final class SpawnedProcess { private final StreamSaver outSaver; + private boolean suppressOutput; + public SpawnedProcess(Process javaProcess, String name) { this.javaProcess = javaProcess; this.name = name; @@ -52,6 +54,17 @@ public final class SpawnedProcess { } /** + * Causes output obtained from the subprocess to be suppressed when + * executing the {@code complete}-methods. + * + * @see #getFullServerOutput() to obtain suppressed output from stdout + * @see #getFullServerError() to obtain suppressed output from stderr + */ + public void suppressOutputOnComplete() { + suppressOutput = true; + } + + /** * Get the Java Process object */ public Process getProcess() { @@ -70,7 +83,7 @@ public final class SpawnedProcess { * should be called first. *

*/ - public String getFullServerOutput() throws Exception { + public String getFullServerOutput() throws InterruptedException { // First wait until we've read all the output. outSaver.thread.join(); @@ -80,6 +93,23 @@ public final class SpawnedProcess { } /** + * Get the full server error output (stderr) as a string using the default + * encoding which is assumed is how it was originally written. + *

+ * This method should only be called after the process has completed. + * That is, {@link #complete(boolean)} or {@link #complete(boolean, long)} + * should be called first. + */ + public String getFullServerError() throws InterruptedException { + // First wait until we've read all the output on stderr. + errSaver.thread.join(); + + synchronized (this) { + return errSaver.stream.toString(); + } + } + + /** * Position offset for getNextServerOutput(). */ int stdOutReadOffset; @@ -191,7 +221,7 @@ public final class SpawnedProcess { // Always write the error ByteArrayOutputStream err = errSaver.stream; - if (err.size() != 0) { + if (!suppressOutput && err.size() != 0) { System.err.println("START-SPAWNED:" + name + " ERROR OUTPUT:"); err.writeTo(System.err); System.err.println("END-SPAWNED :" + name + " ERROR OUTPUT:"); @@ -200,7 +230,8 @@ public final class SpawnedProcess { // Only write the error if it appeared the server // failed in some way. ByteArrayOutputStream out = outSaver.stream; - if ((destroy || exitCode != 0) && out.size() != 0) { + if (!suppressOutput && (destroy || exitCode != 0) && + out.size() != 0) { System.out.println("START-SPAWNED:" + name + " STANDARD OUTPUT: exit code=" + exitCode); out.writeTo(System.out);