db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1138795 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java
Date Thu, 23 Jun 2011 09:44:26 GMT
Author: kahatlen
Date: Thu Jun 23 09:44:25 2011
New Revision: 1138795

URL: http://svn.apache.org/viewvc?rev=1138795&view=rev
Log:
DERBY-5288: running multiple suites.All concurrently should be possible

Wait for the threads that read stdout and stderr to finish before trying
to fetch the output from SpawnedProcess.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java

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=1138795&r1=1138794&r2=1138795&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 Thu
Jun 23 09:44:25 2011
@@ -37,18 +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) 
-                    throws InterruptedException {
+    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"));
     }
 
@@ -60,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();
         }
     }
     
@@ -85,7 +93,7 @@ public final class SpawnedProcess {
     {
         byte[] fullData;
         synchronized (this) {
-            fullData = out.toByteArray();
+            fullData = outSaver.stream.toByteArray();
         }
         
         String output = new String(fullData, stdOutReadOffset,
@@ -111,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)
             {
@@ -127,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 {
@@ -135,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.
      *  
@@ -168,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);
@@ -180,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);
@@ -192,8 +210,22 @@ public final class SpawnedProcess {
         return exitCode;
     }
 
-    private ByteArrayOutputStream streamSaver(final InputStream in,
-            final String name) throws InterruptedException {
+    /**
+     * 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() {
             public void reset() {
@@ -223,9 +255,7 @@ public final class SpawnedProcess {
         }, name);
         streamReader.setDaemon(true);
         streamReader.start();
-        streamReader.join(500);
-
-        return out;
 
+        return new StreamSaver(out, streamReader);
     }
 }



Mime
View raw message