test/java/rmi/testlibrary/JavaVM.java

Print this page

        

@@ -24,10 +24,13 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.StringTokenizer;
+import java.util.concurrent.TimeoutException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * RMI regression test utility class that uses Runtime.exec to spawn a
  * java process that will run a named java class.
  */

@@ -171,10 +174,51 @@
         errPipe.join();
         return status;
     }
 
     /**
+     * Causes the current thread to wait the subprocess to exit, if necessary,
+     * until the subprocess represented by this Process object has terminated,
+     * or the specified waiting time elapses.
+     * @param timeout the maximum milliseconds to wait
+     * @return true if the JavaVM has exited and false if the waiting time
+     *         elapsed before the subprocess has exited.
+     * @throws InterruptedException if the current thread is interrupted
+     *         while waiting.
+     * @throws TimeoutException if subprocess does not end after timeout
+     *         milliseconds passed
+     */
+    public int waitFor(long timeout)
+            throws InterruptedException, TimeoutException {
+        long startTime = System.currentTimeMillis();
+        long rem = timeout;
+
+        do {
+            try {
+                int status = vm.exitValue();
+                outPipe.join();
+                errPipe.join();
+                return status;
+            } catch (IllegalThreadStateException ex) {
+                if (rem > 0) {
+                    Thread.sleep(Math.min(rem, 100));
+                }
+            }
+            rem = timeout - (System.currentTimeMillis() - startTime);
+        } while (rem > 0);
+        new Thread(){
+            @Override
+            public void run() {
+                try {
+                    JavaVM.this.waitFor();
+                } catch (InterruptedException ignore) {}
+            }
+        }.start();
+        throw new TimeoutException();
+    }
+
+    /**
      * Starts the subprocess, waits for it to exit, and returns its exit status.
      */
     public int execute() throws IOException, InterruptedException {
         start();
         return waitFor();