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/24 03:07:11 UTC

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

Author: myrnavl
Date: Fri Jun 24 01:07:11 2011
New Revision: 1139136

URL: http://svn.apache.org/viewvc?rev=1139136&view=rev
Log:
DERBY-5288; running multiple suites.All concurrently should be possible
  backporting revision 113201 and especially 1138795 from trunk. 

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

Propchange: db/derby/code/branches/10.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Jun 24 01:07:11 2011
@@ -1,2 +1,2 @@
 /db/derby/code/branches/10.7:1061570,1061578,1082235
-/db/derby/code/trunk:1063809,1088633,1091000,1091221,1091285,1092067,1092795,1094315,1094572,1094728,1096741,1096890,1097247,1097249,1097460,1097469,1097471,1101059,1101839,1102620,1102826,1103681,1103718,1103742,1125305,1126358,1126468,1127825,1127883,1129136,1129764,1129797,1130077,1130084,1130632,1130895,1131030,1131272,1132546,1132664,1132860,1132928,1133741,1133752,1136371,1136397,1136844,1138787
+/db/derby/code/trunk:1063809,1088633,1091000,1091221,1091285,1092067,1092795,1094315,1094572,1094728,1096741,1096890,1097247,1097249,1097460,1097469,1097471,1101059,1101839,1102620,1102826,1103681,1103718,1103742,1125305,1126358,1126468,1127825,1127883,1129136,1129764,1129797,1130077,1130084,1130632,1130895,1131030,1131272,1132546,1132664,1132860,1132928,1133741,1133752,1136371,1136397,1136844,1138201,1138787,1138795

Modified: db/derby/code/branches/10.8/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.8/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java?rev=1139136&r1=1139135&r2=1139136&view=diff
==============================================================================
--- db/derby/code/branches/10.8/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java (original)
+++ db/derby/code/branches/10.8/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java Fri Jun 24 01:07:11 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,23 @@ 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)} or {@link #complete(boolean, long)}
+     * 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 +93,7 @@ public final class SpawnedProcess {
     {
         byte[] fullData;
         synchronized (this) {
-            fullData = out.toByteArray();
+            fullData = outSaver.stream.toByteArray();
         }
         
         String output = new String(fullData, stdOutReadOffset,
@@ -110,7 +119,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)
             {
@@ -126,7 +138,8 @@ public final class SpawnedProcess {
        return sb.toString();
     }
 
-    /*Complete the method
+    /**
+     * Complete the process.
      * @param destroy true to destroy it, false to wait indefinitely to complete 
      */
     public int complete(boolean destroy) throws InterruptedException, IOException {
@@ -134,7 +147,7 @@ public final class SpawnedProcess {
     }
     
     /**
-     * Complete the method.
+     * Complete the process.
      * @param destroy True to destroy it, false to wait for it to complete 
      * based on timeout.
      *  
@@ -167,10 +180,15 @@ public final class SpawnedProcess {
             javaProcess.destroy();
 
         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);
@@ -179,6 +197,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);
@@ -191,7 +210,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() {
@@ -221,10 +254,8 @@ public final class SpawnedProcess {
 
         }, name);
         streamReader.setDaemon(true);
-        streamReader.setPriority(Thread.MIN_PRIORITY);
         streamReader.start();
 
-        return out;
-
+        return new StreamSaver(out, streamReader);
     }
 }