jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java

Print this page
rev 8840 : 6461635: [TESTBUG] BasicTests.sh test fails intermittently
Summary: Added -Xshare:off and ported from script to java
Contributed-by: mattias.tobiasson@oracle.com

@@ -35,43 +35,25 @@
 public class ProcessThread extends TestThread {
 
     /**
      * Creates a new {@code ProcessThread} object.
      *
-     * @param cmd The list of program and its arguments to pass to {@link ProcessBuilder}
-     */
-    public ProcessThread(List<String> cmd) {
-        super(new ProcessRunnable(cmd));
-    }
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
-     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
-     */
-    public ProcessThread(String... cmd) {
-        super(new ProcessRunnable(cmd));
-    }
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
      * @param threadName The name of thread
-     * @param cmd The list of program and its arguments to pass to {@link ProcessBuilder}
+     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
      */
-    public ProcessThread(String threadName, List<String> cmd) {
-        super(new ProcessRunnable(cmd), threadName);
+    public ProcessThread(String threadName, String... cmd) {
+        super(new ProcessRunnable(new ProcessBuilder(cmd)), threadName);
     }
 
     /**
      * Creates a new {@code ProcessThread} object.
      *
-     * @param threadName The name of thread
-     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
+     * @param threadName The name of thread.
+     * @param pb The ProcessBuilder to execute.
      */
-    public ProcessThread(String threadName, String... cmd) {
-        super(new ProcessRunnable(cmd), threadName);
+    public ProcessThread(String threadName, ProcessBuilder pb) {
+        super(new ProcessRunnable(pb), threadName);
     }
 
     /**
      * Stops {@link Process} started by {@code ProcessRunnable}.
      *

@@ -80,37 +62,34 @@
     public void stopProcess() throws InterruptedException {
         ((ProcessRunnable) getRunnable()).stopProcess();
     }
 
     /**
+     * @return The process output, or null if the process has not yet completed.
+     */
+    public OutputAnalyzer getOutput() {
+        return ((ProcessRunnable) getRunnable()).getOutput();
+    }
+
+    /**
      * {@link Runnable} interface for starting and stopping {@link Process}.
      */
     static class ProcessRunnable extends XRun {
 
         private final ProcessBuilder processBuilder;
         private final CountDownLatch latch;
         private volatile Process process;
+        private volatile OutputAnalyzer output;
 
         /**
          * Creates a new {@code ProcessRunnable} object.
          *
-         * @param cmd The list of program and its arguments to to pass to {@link ProcessBuilder}
-         */
-        public ProcessRunnable(List<String> cmd) {
-            super();
-            this.processBuilder = new ProcessBuilder(cmd);
-            this.latch = new CountDownLatch(1);
-        }
-
-        /**
-         * Creates a new {@code ProcessRunnable} object.
-         *
-         * @param cmd The string array of program and its arguments to to pass to {@link ProcessBuilder}
+         * @param pb The {@link ProcessBuilder} to run.
          */
-        public ProcessRunnable(String... cmd) {
+        public ProcessRunnable(ProcessBuilder pb) {
             super();
-            this.processBuilder = new ProcessBuilder(cmd);
+            this.processBuilder = pb;
             this.latch = new CountDownLatch(1);
         }
 
         /**
          * Starts the process in {@code ProcessThread}.

@@ -123,16 +102,20 @@
             this.process = processBuilder.start();
             // Release when process is started
             latch.countDown();
 
             // Will block...
-            OutputAnalyzer output = new OutputAnalyzer(this.process);
-
-            assertTrue(output.getOutput().isEmpty(), "Should get an empty output, got: "
-                        + Utils.NEW_LINE + output.getOutput());
-            assertNotEquals(output.getExitValue(), 0,
-                    "Process exited with unexpected exit code");
+            try {
+                output = new OutputAnalyzer(this.process);
+            } catch (Throwable t) {
+                String name = Thread.currentThread().getName();
+                System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
+                throw t;
+            } finally {
+                String logMsg = ProcessTools.getProcessLog(processBuilder, output);
+                System.out.println(logMsg);
+            }
         }
 
         /**
          * Stops the process.
          *

@@ -140,12 +123,21 @@
          */
         public void stopProcess() throws InterruptedException {
             // Wait until process is started
             latch.await();
             if (this.process != null) {
+                System.out.println("ProcessThread.stopProcess() will kill process");
                 this.process.destroy();
             }
         }
 
+        /**
+         * Returns the OutputAnalyzer with stdout/stderr from the process.
+         * @return The process output, or null if process not completed.
+         * @throws InterruptedException
+         */
+        public OutputAnalyzer getOutput() {
+            return output;
+        }
     }
 
 }