8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process
authorjbachorik
Fri, 17 Oct 2014 10:53:30 +0200
changeset 27189 b90845965ee9
parent 27188 a1aa62221649
child 27190 d3a9a634cc0b
8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process Reviewed-by: sla, dholmes
jdk/test/ProblemList.txt
jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java
jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java
jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java
jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java
jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java
jdk/test/lib/testlibrary/OutputAnalyzerTest.java
jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java
jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java
jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java
jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java
jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java
jdk/test/sun/management/jdp/DynamicLauncher.java
jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java
jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java
jdk/test/sun/tools/jhat/HatHeapDump1Test.java
jdk/test/sun/tools/jmap/BasicJMapTest.java
jdk/test/sun/tools/jps/JpsHelper.java
jdk/test/sun/tools/jps/TestJpsClass.java
jdk/test/sun/tools/jps/TestJpsJar.java
jdk/test/sun/tools/jps/TestJpsJarRelative.java
jdk/test/sun/tools/jstack/BasicJStackTest.java
jdk/test/sun/tools/jstatd/JstatdTest.java
jdk/test/sun/tools/jstatd/TestJstatdUsage.java
--- a/jdk/test/ProblemList.txt	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/ProblemList.txt	Fri Oct 17 10:53:30 2014 +0200
@@ -134,8 +134,6 @@
 # 8044591
 com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java   generic-all
 com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java          generic-all
-# 8056143
-java/lang/management/MemoryMXBean/LowMemoryTest.java                                      generic-all
 
 # 8058492
 java/lang/management/ThreadMXBean/FindDeadlocks.java                                      generic-all
@@ -308,7 +306,7 @@
 sun/tools/jstatd/TestJstatdPort.java                    generic-all
 sun/tools/jstatd/TestJstatdPortAndServer.java                    generic-all
 
-# 8046285 8027668 
+# 8046285 8027668
 sun/tools/jstatd/TestJstatdExternalRegistry.java                generic-all
 
 # 6456333
--- a/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java	Fri Oct 17 10:53:30 2014 +0200
@@ -23,21 +23,14 @@
  */
 
 
-import jdk.testlibrary.JDKToolLauncher;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessTools;
 
-import java.io.IOException;
-import java.nio.file.Path;
-
 public class TestDaemonThreadLauncher {
-
-    private static ProcessBuilder processBuilder = new ProcessBuilder();
-
     public static void main(String args[]) throws Exception {
         for(int i=0; i<50; i++) {
             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-javaagent:DummyAgent.jar", "TestDaemonThread", ".");
-            OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
+            OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
             analyzer.shouldNotContain("ASSERTION FAILED");
         }
     }
--- a/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@
                 Utils.addTestJavaOpts(testArgs.split("\\s+")));
         System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
 
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(pb);
         System.out.println("testjvm.stdout:" + output.getStdout());
         System.out.println("testjvm.stderr:" + output.getStderr());
 
--- a/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
                 Utils.addTestJavaOpts(testArgs.split("\\s+")));
         System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
 
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(pb);
         System.out.println("testjvm.stdout:" + output.getStdout());
         System.out.println("testjvm.stderr:" + output.getStderr());
 
--- a/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@
                 Utils.addTestJavaOpts(testArgs.split("\\s+")));
         System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
 
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(pb);
         System.out.println("testjvm.stdout:" + output.getStdout());
         System.out.println("testjvm.stderr:" + output.getStderr());
 
--- a/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,6 +21,7 @@
  * questions.
  */
 
+package jdk.testlibrary;
 
 /*
  * @test
@@ -28,13 +29,13 @@
  *     such as printing additional diagnostic info
  *     (exit code, stdout, stderr, command line, etc.)
  * @library /testlibrary
+ * @build jdk.testlibrary.*
+ * @run main jdk.testlibrary.OutputAnalyzerReportingTest
  */
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 
-import jdk.testlibrary.OutputAnalyzer;
-
 public class OutputAnalyzerReportingTest {
 
     public static void main(String[] args) throws Exception {
--- a/jdk/test/lib/testlibrary/OutputAnalyzerTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/OutputAnalyzerTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -20,15 +20,16 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+package jdk.testlibrary;
 
 /*
  * @test
  * @summary Test the OutputAnalyzer utility class
  * @library /testlibrary
+ * @build jdk.testlibrary.*
+ * @run main jdk.testlibrary.OutputAnalyzerTest
  */
 
-import jdk.testlibrary.OutputAnalyzer;
-
 public class OutputAnalyzerTest {
 
     public static void main(String args[]) throws Exception {
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -108,7 +108,7 @@
         }
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,9 +25,13 @@
 
 import static jdk.testlibrary.Asserts.*;
 
+import java.io.ByteArrayOutputStream;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -35,34 +39,37 @@
  * Utility class for verifying output and exit value from a {@code Process}.
  */
 public final class OutputAnalyzer {
-
+    private final OutputBuffer output;
     private final String stdout;
     private final String stderr;
     private final int exitValue;
 
     /**
      * Create an OutputAnalyzer, a utility class for verifying output and exit
-     * value from a Process
+     * value from a Process.
+     * <p>
+     * OutputAnalyzer should never be instantiated directly -
+     * use {@linkplain ProcessTools#executeProcess(p)} instead
      *
      * @param process
      *            Process to analyze
      * @throws IOException
      *             If an I/O error occurs.
      */
-    public OutputAnalyzer(Process process) throws IOException {
-        OutputBuffer output = ProcessTools.getOutput(process);
-        exitValue = process.exitValue();
-        this.stdout = output.getStdout();
-        this.stderr = output.getStderr();
+    OutputAnalyzer(Process process) throws IOException {
+        output = new OutputBuffer(process);
+        exitValue = -1;
+        this.stdout = null;
+        this.stderr = null;
     }
 
     /**
-     * Create an OutputAnalyzer, a utility class for verifying output
+     * Create an OutputAnalyzer, a utility class for verifying output.
      *
      * @param buf
      *            String buffer to analyze
      */
-    public OutputAnalyzer(String buf) {
+    OutputAnalyzer(String buf) {
         this(buf, buf);
     }
 
@@ -74,7 +81,8 @@
      * @param stderr
      *            stderr buffer to analyze
      */
-    public OutputAnalyzer(String stdout, String stderr) {
+    OutputAnalyzer(String stdout, String stderr) {
+        this.output = null;
         this.stdout = stdout;
         this.stderr = stderr;
         exitValue = -1;
@@ -90,8 +98,8 @@
      *             If the string was not found
      */
     public void shouldContain(String expectedString) {
-        if (!stdout.contains(expectedString)
-                && !stderr.contains(expectedString)) {
+        if (!getStdout().contains(expectedString)
+                && !getStderr().contains(expectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + expectedString
                     + "' missing from stdout/stderr \n");
@@ -107,7 +115,7 @@
      *             If the string was not found
      */
     public void stdoutShouldContain(String expectedString) {
-        if (!stdout.contains(expectedString)) {
+        if (!getStdout().contains(expectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + expectedString
                     + "' missing from stdout \n");
@@ -123,7 +131,7 @@
      *             If the string was not found
      */
     public void stderrShouldContain(String expectedString) {
-        if (!stderr.contains(expectedString)) {
+        if (!getStderr().contains(expectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + expectedString
                     + "' missing from stderr \n");
@@ -140,12 +148,12 @@
      *             If the string was found
      */
     public void shouldNotContain(String notExpectedString) {
-        if (stdout.contains(notExpectedString)) {
+        if (getStdout().contains(notExpectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + notExpectedString
                     + "' found in stdout \n");
         }
-        if (stderr.contains(notExpectedString)) {
+        if (getStderr().contains(notExpectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + notExpectedString
                     + "' found in stderr \n");
@@ -162,7 +170,7 @@
      *             If the string was found
      */
     public void stdoutShouldNotContain(String notExpectedString) {
-        if (stdout.contains(notExpectedString)) {
+        if (getStdout().contains(notExpectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + notExpectedString
                     + "' found in stdout \n");
@@ -179,7 +187,7 @@
      *             If the string was found
      */
     public void stderrShouldNotContain(String notExpectedString) {
-        if (stderr.contains(notExpectedString)) {
+        if (getStderr().contains(notExpectedString)) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + notExpectedString
                     + "' found in stderr \n");
@@ -196,9 +204,9 @@
      */
     public void shouldMatch(String pattern) {
         Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
-                .matcher(stdout);
+                .matcher(getStdout());
         Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
-                .matcher(stderr);
+                .matcher(getStderr());
         if (!stdoutMatcher.find() && !stderrMatcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern
@@ -215,7 +223,7 @@
      */
     public void stdoutShouldMatch(String pattern) {
         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
-                stdout);
+                getStdout());
         if (!matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern
@@ -232,7 +240,7 @@
      */
     public void stderrShouldMatch(String pattern) {
         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
-                stderr);
+                getStderr());
         if (!matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern
@@ -250,13 +258,13 @@
      */
     public void shouldNotMatch(String pattern) {
         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
-                stdout);
+                getStdout());
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern + "' found in stdout: '"
                     + matcher.group() + "' \n");
         }
-        matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
+        matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(getStderr());
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern + "' found in stderr: '"
@@ -274,7 +282,7 @@
      */
     public void stdoutShouldNotMatch(String pattern) {
         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
-                stdout);
+                getStdout());
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern + "' found in stdout \n");
@@ -291,7 +299,7 @@
      */
     public void stderrShouldNotMatch(String pattern) {
         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
-                stderr);
+                getStderr());
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + pattern + "' found in stderr \n");
@@ -310,9 +318,9 @@
      */
     public String firstMatch(String pattern, int group) {
         Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
-                .matcher(stderr);
+                .matcher(getStderr());
         Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
-                .matcher(stdout);
+                .matcher(getStdout());
         if (stderrMatcher.find()) {
             return stderrMatcher.group(group);
         }
@@ -357,7 +365,7 @@
      * exit code Note: the command line is printed by the ProcessTools
      */
     private void reportDiagnosticSummary() {
-        String msg = " stdout: [" + stdout + "];\n" + " stderr: [" + stderr
+        String msg = " stdout: [" + getStdout() + "];\n" + " stderr: [" + getStderr()
                 + "]\n" + " exitValue = " + getExitValue() + "\n";
 
         System.err.println(msg);
@@ -369,7 +377,7 @@
      * @return Content of the output buffer
      */
     public String getOutput() {
-        return stdout + stderr;
+        return getStdout() + getStderr();
     }
 
     /**
@@ -378,7 +386,7 @@
      * @return Content of the stdout buffer
      */
     public String getStdout() {
-        return stdout;
+        return output == null ? stdout : output.getStdout();
     }
 
     /**
@@ -387,7 +395,7 @@
      * @return Content of the stderr buffer
      */
     public String getStderr() {
-        return stderr;
+        return output == null ? stderr : output.getStderr();
     }
 
     /**
@@ -396,7 +404,7 @@
      * @return Process exit value
      */
     public int getExitValue() {
-        return exitValue;
+        return output == null ? exitValue : output.getExitValue();
     }
 
     /**
@@ -492,5 +500,4 @@
 
         return matchedCount;
     }
-
 }
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,9 +23,23 @@
 
 package jdk.testlibrary;
 
-public class OutputBuffer {
-    private final String stdout;
-    private final String stderr;
+import java.io.ByteArrayOutputStream;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+class OutputBuffer {
+    private static class OutputBufferException extends RuntimeException {
+        public OutputBufferException(Throwable cause) {
+            super(cause);
+        }
+    }
+
+    private final Process p;
+    private final Future<Void> outTask;
+    private final Future<Void> errTask;
+    private final ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
+    private final ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
 
     /**
      * Create an OutputBuffer, a class for storing and managing stdout and
@@ -36,9 +50,15 @@
      * @param stderr
      *            stderr result
      */
-    public OutputBuffer(String stdout, String stderr) {
-        this.stdout = stdout;
-        this.stderr = stderr;
+    OutputBuffer(Process p) {
+        this.p = p;
+        StreamPumper outPumper = new StreamPumper(p.getInputStream(),
+                stdoutBuffer);
+        StreamPumper errPumper = new StreamPumper(p.getErrorStream(),
+                stderrBuffer);
+
+        outTask = outPumper.process();
+        errTask = errPumper.process();
     }
 
     /**
@@ -47,7 +67,15 @@
      * @return stdout result
      */
     public String getStdout() {
-        return stdout;
+        try {
+            outTask.get();
+            return stdoutBuffer.toString();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new OutputBufferException(e);
+        } catch (ExecutionException | CancellationException e) {
+            throw new OutputBufferException(e);
+        }
     }
 
     /**
@@ -56,6 +84,23 @@
      * @return stderr result
      */
     public String getStderr() {
-        return stderr;
+        try {
+            errTask.get();
+            return stderrBuffer.toString();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new OutputBufferException(e);
+        } catch (ExecutionException | CancellationException e) {
+            throw new OutputBufferException(e);
+        }
+    }
+
+    public int getExitValue() {
+        try {
+            return p.waitFor();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new OutputBufferException(e);
+        }
     }
 }
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -154,12 +154,14 @@
 
             // Will block...
             try {
+                this.process.waitFor();
                 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 {
+                this.process.destroyForcibly().waitFor();
                 String logMsg = ProcessTools.getProcessLog(processBuilder, output);
                 System.out.println(logMsg);
             }
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,6 @@
 
 package jdk.testlibrary;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.lang.management.ManagementFactory;
@@ -34,7 +33,6 @@
 import java.util.Collections;
 import java.util.concurrent.CountDownLatch;
 import java.util.Map;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -214,55 +212,6 @@
     }
 
     /**
-     * Pumps stdout and stderr from running the process into a String.
-     *
-     * @param processBuilder
-     *            ProcessHandler to run.
-     * @return Output from process.
-     * @throws IOException
-     *             If an I/O error occurs.
-     */
-    public static OutputBuffer getOutput(ProcessBuilder processBuilder)
-            throws IOException {
-        return getOutput(processBuilder.start());
-    }
-
-    /**
-     * Pumps stdout and stderr the running process into a String.
-     *
-     * @param process
-     *            Process to pump.
-     * @return Output from process.
-     * @throws IOException
-     *             If an I/O error occurs.
-     */
-    public static OutputBuffer getOutput(Process process) throws IOException {
-        ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
-        ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
-        StreamPumper outPumper = new StreamPumper(process.getInputStream(),
-                stdoutBuffer);
-        StreamPumper errPumper = new StreamPumper(process.getErrorStream(),
-                stderrBuffer);
-
-        Future<Void> outTask = outPumper.process();
-        Future<Void> errTask = errPumper.process();
-
-        try {
-            process.waitFor();
-            outTask.get();
-            errTask.get();
-        } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-            return null;
-        } catch (ExecutionException e) {
-            throw new IOException(e);
-        }
-
-        return new OutputBuffer(stdoutBuffer.toString(),
-                stderrBuffer.toString());
-    }
-
-    /**
      * Get the process id of the current running Java process
      *
      * @return Process id
@@ -343,34 +292,51 @@
      * The command line will be like:
      * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
      *
+     * The jvm process will have exited before this method returns.
+     *
      * @param cmds User specifed arguments.
      * @return The output from the process.
      */
-    public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
+    public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception {
         ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
         return executeProcess(pb);
     }
 
     /**
      * Executes a process, waits for it to finish and returns the process output.
+     * The process will have exited before this method returns.
      * @param pb The ProcessBuilder to execute.
-     * @return The output from the process.
+     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
      */
-    public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Throwable {
+    public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
         OutputAnalyzer output = null;
+        Process p = null;
+        boolean failed = false;
         try {
-            output = new OutputAnalyzer(pb.start());
+            p = pb.start();
+            output = new OutputAnalyzer(p);
+            p.waitFor();
+
             return output;
         } catch (Throwable t) {
+            failed = true;
             System.out.println("executeProcess() failed: " + t);
             throw t;
         } finally {
-            System.out.println(getProcessLog(pb, output));
+            if (p != null) {
+                p.destroyForcibly().waitFor();
+            }
+            if (failed) {
+                System.err.println(getProcessLog(pb, output));
+            }
         }
     }
 
     /**
      * Executes a process, waits for it to finish and returns the process output.
+     *
+     * The process will have exited before this method returns.
+     *
      * @param cmds The command line to execute.
      * @return The output from the process.
      */
@@ -394,6 +360,7 @@
         logMsg.append("exitvalue: " + exitValue + nl);
         logMsg.append("stderr: " + stderr + nl);
         logMsg.append("stdout: " + stdout + nl);
+
         return logMsg.toString();
     }
 
--- a/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java	Fri Oct 17 10:53:30 2014 +0200
@@ -34,7 +34,7 @@
 import java.util.UUID;
 import java.util.concurrent.Semaphore;
 
-import jdk.testlibrary.OutputBuffer;
+import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessTools;
 import sun.jvmstat.monitor.MonitorException;
 import sun.jvmstat.monitor.MonitoredHost;
@@ -305,13 +305,13 @@
             Runtime.getRuntime().removeShutdownHook(shutdownHook);
         }
 
-        private void executeJava() throws Exception, IOException {
+        private void executeJava() throws Throwable {
             String className = JavaProcess.class.getName();
             String classPath = System.getProperty("test.classes");
             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
                 "-Dtest.timeout.factor=" + System.getProperty("test.timeout.factor", "1.0"),
                 "-cp", classPath, className, mainArgsIdentifier);
-            OutputBuffer ob = ProcessTools.getOutput(pb.start());
+            OutputAnalyzer ob = ProcessTools.executeProcess(pb);
             System.out.println("Java Process " + getMainArgsIdentifier() + " stderr:"
                     + ob.getStderr());
             System.err.println("Java Process " + getMainArgsIdentifier() + " stdout:"
--- a/jdk/test/sun/management/jdp/DynamicLauncher.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/management/jdp/DynamicLauncher.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -63,7 +63,7 @@
     protected OutputAnalyzer runVM() throws Exception {
         String[] options = this.options();
         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(options);
-        OutputAnalyzer out = new OutputAnalyzer(pb.start());
+        OutputAnalyzer out = ProcessTools.executeProcess(pb);
         System.out.println(out.getStdout());
         System.err.println(out.getStderr());
         return out;
--- a/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -176,16 +176,15 @@
                     command.toArray(new String[command.size()]));
 
             System.out.println("test cmdline: " + Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-            Process p = processBuilder.start();
-            OutputAnalyzer output = new OutputAnalyzer(p);
+            OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 
             System.out.println("test output:");
             System.out.println(output.getOutput());
 
-            if ((p.exitValue() == 0)  ||
+            if ((output.getExitValue() == 0)  ||
                 !output.getOutput().contains("Exception thrown by the agent : " +
                         "java.rmi.server.ExportException: Port already in use")) {
-                return p.exitValue();
+                return output.getExitValue();
             }
         }
 
--- a/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -188,15 +188,14 @@
 
             ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(command.toArray(new String[command.size()]));
 
-            Process p = processBuilder.start();
-            OutputAnalyzer output = new OutputAnalyzer(p);
+            OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 
             System.out.println("test output:");
             System.out.println(output.getOutput());
 
             if (!output.getOutput().contains("Exception thrown by the agent : " +
                     "java.rmi.server.ExportException: Port already in use")) {
-                return p.exitValue();
+                return output.getExitValue();
             }
         }
         throw new Error("Cannot find free port");
--- a/jdk/test/sun/tools/jhat/HatHeapDump1Test.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jhat/HatHeapDump1Test.java	Fri Oct 17 10:53:30 2014 +0200
@@ -52,7 +52,7 @@
                 "-Xverify:all",
                 "-agentlib:hprof=heap=dump,format=b,file=" + dumpFile.getAbsolutePath(),
                 className);
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
         output.shouldHaveExitValue(0);
         output.shouldContain("Dumping Java heap ... done");
@@ -79,7 +79,7 @@
         ProcessBuilder processBuilder = new ProcessBuilder();
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
--- a/jdk/test/sun/tools/jmap/BasicJMapTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jmap/BasicJMapTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -91,7 +91,7 @@
 
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
--- a/jdk/test/sun/tools/jps/JpsHelper.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jps/JpsHelper.java	Fri Oct 17 10:53:30 2014 +0200
@@ -36,6 +36,7 @@
 import jdk.testlibrary.JDKToolLauncher;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.Utils;
+import jdk.testlibrary.ProcessTools;
 
 /**
  * The helper class for running jps utility and verifying output from it
@@ -160,7 +161,7 @@
 
         ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
--- a/jdk/test/sun/tools/jps/TestJpsClass.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jps/TestJpsClass.java	Fri Oct 17 10:53:30 2014 +0200
@@ -55,7 +55,7 @@
         cmd.add("monkey");
 
         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()]));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
         output.shouldHaveExitValue(0);
     }
--- a/jdk/test/sun/tools/jps/TestJpsJar.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jps/TestJpsJar.java	Fri Oct 17 10:53:30 2014 +0200
@@ -55,7 +55,7 @@
         cmd.add("monkey");
 
         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()]));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
         output.shouldHaveExitValue(0);
     }
--- a/jdk/test/sun/tools/jps/TestJpsJarRelative.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jps/TestJpsJarRelative.java	Fri Oct 17 10:53:30 2014 +0200
@@ -55,7 +55,7 @@
         cmd.add("monkey");
 
         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()]));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
         output.shouldHaveExitValue(0);
     }
--- a/jdk/test/sun/tools/jstack/BasicJStackTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jstack/BasicJStackTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -66,7 +66,7 @@
 
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
--- a/jdk/test/sun/tools/jstatd/JstatdTest.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jstatd/JstatdTest.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,6 @@
 import jdk.testlibrary.JDKToolLauncher;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessThread;
-import jdk.testlibrary.TestThread;
 import jdk.testlibrary.Utils;
 import jdk.testlibrary.ProcessTools;
 
@@ -145,8 +144,8 @@
         return output;
     }
 
-    private OutputAnalyzer waitForJstatdRMI(ProcessBuilder pb) throws IOException, InterruptedException {
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+    private OutputAnalyzer waitForJstatdRMI(ProcessBuilder pb) throws Exception {
+        OutputAnalyzer output = ProcessTools.executeProcess(pb);
 
         String remoteHost = (serverName != null) ? serverName : "JStatRemoteHost";
         while (output.getExitValue() != 0) {
@@ -155,7 +154,7 @@
             if (out.contains("RMI Registry not available") ||
                 out.contains("RMI Server " + remoteHost + " not available")) {
                 Thread.sleep(100);
-                output = new OutputAnalyzer(pb.start());
+                output = ProcessTools.executeProcess(pb);
             } else {
                 output.shouldHaveExitValue(0);
             }
--- a/jdk/test/sun/tools/jstatd/TestJstatdUsage.java	Wed Oct 22 09:13:52 2014 +0200
+++ b/jdk/test/sun/tools/jstatd/TestJstatdUsage.java	Fri Oct 17 10:53:30 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
 
 import jdk.testlibrary.JDKToolLauncher;
 import jdk.testlibrary.OutputAnalyzer;
+import jdk.testlibrary.ProcessTools;
 
 /*
  * @test
@@ -42,7 +43,7 @@
         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstatd");
         launcher.addToolArg(option);
         ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
-        OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 
         output.shouldContain("usage: jstatd [-nr] [-p port] [-n rminame]");
         output.shouldHaveExitValue(1);