You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by my...@apache.org on 2011/06/27 17:10:12 UTC

svn commit: r1140199 - in /db/derby/code/branches/10.6: ./ java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java

Author: myrnavl
Date: Mon Jun 27 15:10:12 2011
New Revision: 1140199

URL: http://svn.apache.org/viewvc?rev=1140199&view=rev
Log:
DERBY-5288; running multiple suites.All concurrently should be possible
  merged manual backport of changes for trunk (rev 1138201, 1138795) 
  to 10.7 (rev 1140196) into 10.6.
  merge command: 
    merge -c 1140196 https://svn.apache.org/repos/asf/db/derby/code/branches/10.7

Modified:
    db/derby/code/branches/10.6/   (props changed)
    db/derby/code/branches/10.6/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java

Propchange: db/derby/code/branches/10.6/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jun 27 15:10:12 2011
@@ -1,2 +1,3 @@
+/db/derby/code/branches/10.7:1140196
 /db/derby/code/trunk:938547,938796,938959,939231,940462,940469,941627,942031,942286,942476,942480,942587,944152,946794,948045,948069,951346,951366,952138,952237,952581,954344,954421,954544,954748,955001,955540,955634,956075,956234,956445,956569,956659,957260,957902,958163,958257,958264,958508,958522,958555,958618,958939,959550,961892,962716,963206,963705,964039,964115,964402,965647,966393,967201,967304,980089,980684,986689,986834,987539,989099,990292,997325,998170,999119,999479,999485,1002291,1002682,1002853,1021426,1024511,1024528,1025615,1025795,1028716,1030043,1033485,1033864,1035164,1038514,1040658,1053724,1055169,1062096,1063809,1065061,1067250,1069661,1071886,1078461,1081455,1097247,1103681,1103718
 /db/derby/docs/trunk:954344

Modified: db/derby/code/branches/10.6/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.6/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java?rev=1140199&r1=1140198&r2=1140199&view=diff
==============================================================================
--- db/derby/code/branches/10.6/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java (original)
+++ db/derby/code/branches/10.6/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java Mon Jun 27 15:10:12 2011
@@ -37,17 +37,17 @@ public final class SpawnedProcess {
 
     private final Process javaProcess;
 
-    private final ByteArrayOutputStream err;
+    private final StreamSaver errSaver;
 
-    private final ByteArrayOutputStream out;
+    private final StreamSaver outSaver;
 
     public SpawnedProcess(Process javaProcess, String name) {
         this.javaProcess = javaProcess;
         this.name = name;
 
-        err = streamSaver(javaProcess.getErrorStream(), name
+        errSaver = streamSaver(javaProcess.getErrorStream(), name
                 .concat(":System.err"));
-        out = streamSaver(javaProcess.getInputStream(), name
+        outSaver = streamSaver(javaProcess.getInputStream(), name
                 .concat(":System.out"));
     }
 
@@ -59,14 +59,22 @@ public final class SpawnedProcess {
     }
     
     /**
+     * <p>
      * Get the full server output (stdout) as a string using the default
-     * encoding which is assumed is how it was orginally
-     * written.
+     * encoding which is assumed is how it was originally written.
+     * </p>
+     *
+     * <p>
+     * This method should only be called after the process has completed.
+     * That is, {@link #complete(boolean)} should be called first.
+     * </p>
      */
     public String getFullServerOutput() throws Exception {
-        Thread.sleep(500);
+        // First wait until we've read all the output.
+        outSaver.thread.join();
+
         synchronized (this) {
-            return out.toString(); 
+            return outSaver.stream.toString();
         }
     }
     
@@ -84,7 +92,7 @@ public final class SpawnedProcess {
     {
         byte[] fullData;
         synchronized (this) {
-            fullData = out.toByteArray();
+            fullData = outSaver.stream.toByteArray();
         }
         
         String output = new String(fullData, stdOutReadOffset,
@@ -110,7 +118,10 @@ public final class SpawnedProcess {
         } catch (IllegalThreadStateException e) {
             sb.append("running");
         }
-        
+
+        ByteArrayOutputStream err = errSaver.stream;
+        ByteArrayOutputStream out = outSaver.stream;
+
         synchronized (this) {
             if (err.size() != 0)
             {
@@ -127,7 +138,7 @@ public final class SpawnedProcess {
     }
 
     /**
-     * Complete the method.
+     * Complete the process.
      * @param destroy True to destroy it, false to wait for it to complete.
      */
     public int complete(boolean destroy) throws InterruptedException, IOException {
@@ -135,10 +146,15 @@ public final class SpawnedProcess {
             javaProcess.destroy();
 
         int exitCode = javaProcess.waitFor();
-        Thread.sleep(500);
+
+        // The process has completed. Wait until we've read all output.
+        outSaver.thread.join();
+        errSaver.thread.join();
+
         synchronized (this) {
 
             // Always write the error
+            ByteArrayOutputStream err = errSaver.stream;
             if (err.size() != 0) {
                 System.err.println("START-SPAWNED:" + name + " ERROR OUTPUT:");
                 err.writeTo(System.err);
@@ -147,6 +163,7 @@ 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) {
                 System.out.println("START-SPAWNED:" + name
                         + " STANDARD OUTPUT: exit code=" + exitCode);
@@ -159,7 +176,21 @@ public final class SpawnedProcess {
         return exitCode;
     }
 
-    private ByteArrayOutputStream streamSaver(final InputStream in,
+    /**
+     * Class holding references to a stream that receives the output from a
+     * process and a thread that reads the process output and passes it on
+     * to the stream.
+     */
+    private static class StreamSaver {
+        final ByteArrayOutputStream stream;
+        final Thread thread;
+        StreamSaver(ByteArrayOutputStream stream, Thread thread) {
+            this.stream = stream;
+            this.thread = thread;
+        }
+    }
+
+    private StreamSaver streamSaver(final InputStream in,
             final String name) {
 
         final ByteArrayOutputStream out = new ByteArrayOutputStream() {
@@ -189,10 +220,8 @@ public final class SpawnedProcess {
 
         }, name);
         streamReader.setDaemon(true);
-        streamReader.setPriority(Thread.MIN_PRIORITY);
         streamReader.start();
 
-        return out;
-
+        return new StreamSaver(out, streamReader);
     }
 }