changeset 16360:6f246db971c0

8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use Reviewed-by: rriggs
author mli
date Mon, 19 Dec 2016 17:09:10 -0800
parents ec574eb3aee4
children 24f8703890b2
files test/java/rmi/registry/reexport/Reexport.java test/java/rmi/testlibrary/JavaVM.java test/java/rmi/testlibrary/REGISTRY.java test/java/rmi/testlibrary/RegistryRunner.java test/java/rmi/testlibrary/RegistryRunner_Stub.java
diffstat 5 files changed, 177 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/rmi/registry/reexport/Reexport.java	Mon Dec 19 15:01:14 2016 -0800
+++ b/test/java/rmi/registry/reexport/Reexport.java	Mon Dec 19 17:09:10 2016 -0800
@@ -29,7 +29,7 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
- * @build TestLibrary JavaVM RegistryRunner RegistryRunner_Stub
+ * @build TestLibrary REGISTRY RegistryRunner
  * @run main/othervm Reexport
  */
 
@@ -54,23 +54,20 @@
     static public void main(String[] argv) {
 
         Registry reg = null;
-        int regPort = TestLibrary.getUnusedRandomPort();
-
         try {
             System.err.println("\nregression test for 4120329\n");
 
             // establish the registry (we hope)
-            System.err.println("Starting registry on port " + regPort);
-            Reexport.makeRegistry(regPort);
+            makeRegistry();
 
             // Get a handle to the registry
             System.err.println("Creating duplicate registry, this should fail...");
-            reg = createReg(true, regPort);
+            reg = createReg(true);
 
             // Kill the first registry.
             System.err.println("Bringing down the first registry");
             try {
-                Reexport.killRegistry(regPort);
+                killRegistry();
             } catch (Exception foo) {
             }
 
@@ -78,7 +75,7 @@
             System.err.println("Trying again to start our own " +
                                "registry... this should work");
 
-            reg = createReg(false, regPort);
+            reg = createReg(false);
 
             if (reg == null) {
                 TestLibrary.bomb("Could not create registry on second try");
@@ -90,13 +87,12 @@
             TestLibrary.bomb(e);
         } finally {
             // dont leave the registry around to affect other tests.
-            killRegistry(regPort);
-
+            killRegistry();
             reg = null;
         }
     }
 
-    static Registry createReg(boolean remoteOk, int port) {
+    static Registry createReg(boolean remoteOk) {
         Registry reg = null;
 
         try {
@@ -113,43 +109,29 @@
                 TestLibrary.bomb((Exception) e);
             }
         }
-
         return reg;
     }
 
-    public static void makeRegistry(int p) {
-        // sadly, we can't kill a registry if we have too-close control
-        // over it.  We must make it in a subprocess, and then kill the
-        // subprocess when it has served our needs.
-
+    public static void makeRegistry() {
         try {
-            JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p));
-            jvm.start();
-            Reexport.subreg = jvm;
+            subreg = REGISTRY.createREGISTRY();
+            subreg.start();
+            port = subreg.getPort();
+            System.out.println("Starting registry on port " + port);
         } catch (IOException e) {
             // one of these is summarily dropped, can't remember which one
             System.out.println ("Test setup failed - cannot run rmiregistry");
             TestLibrary.bomb("Test setup failed - cannot run test", e);
         }
-        // Slop - wait for registry to come up.  This is stupid.
-        try {
-            Thread.sleep (5000);
-        } catch (Exception whatever) {
+    }
+
+    private static REGISTRY subreg = null;
+    private static int port = -1;
+
+    public static void killRegistry() {
+        if (subreg != null) {
+            subreg.shutdown();
+            subreg = null;
         }
     }
-
-    private static JavaVM subreg = null;
-
-    public static void killRegistry(int port) {
-        if (Reexport.subreg != null) {
-
-            RegistryRunner.requestExit(port);
-
-            try {
-                Reexport.subreg.waitFor();
-            } catch (InterruptedException ie) {
-            }
-        }
-        Reexport.subreg = null;
-    }
 }
--- a/test/java/rmi/testlibrary/JavaVM.java	Mon Dec 19 15:01:14 2016 -0800
+++ b/test/java/rmi/testlibrary/JavaVM.java	Mon Dec 19 17:09:10 2016 -0800
@@ -22,6 +22,7 @@
  */
 
 import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.File;
 import java.io.IOException;
@@ -37,6 +38,26 @@
  */
 public class JavaVM {
 
+    static class CachedOutputStream extends OutputStream {
+        ByteArrayOutputStream ba;
+        OutputStream os;
+
+        public CachedOutputStream(OutputStream os) {
+            this.os = os;
+            this.ba = new ByteArrayOutputStream();
+        }
+
+        public void write(int b) throws IOException {
+            ba.write(b);
+            os.write(b);
+        }
+
+        public void reset() throws IOException {
+            os.flush();
+            ba.reset();
+        }
+    }
+
     public static final long POLLTIME_MS = 100L;
 
     protected Process vm = null;
@@ -44,8 +65,8 @@
     private String classname = "";
     protected String args = "";
     protected String options = "";
-    private OutputStream outputStream = System.out;
-    private OutputStream errorStream = System.err;
+    protected CachedOutputStream outputStream = new CachedOutputStream(System.out);
+    protected CachedOutputStream errorStream = new CachedOutputStream(System.err);
     private String policyFileName = null;
     private StreamPipe outPipe;
     private StreamPipe errPipe;
@@ -76,8 +97,8 @@
                   String options, String args,
                   OutputStream out, OutputStream err) {
         this(classname, options, args);
-        this.outputStream = out;
-        this.errorStream = err;
+        this.outputStream = new CachedOutputStream(out);
+        this.errorStream = new CachedOutputStream(err);
     }
 
     // Prepends passed opts array to current options
@@ -117,6 +138,8 @@
      * Exec the VM as specified in this object's constructor.
      */
     private void start0() throws IOException {
+        outputStream.reset();
+        errorStream.reset();
 
         if (vm != null)
             throw new IllegalStateException("JavaVM already started");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/rmi/testlibrary/REGISTRY.java	Mon Dec 19 17:09:10 2016 -0800
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * Class to run and control rmiregistry in a sub-process.
+ *
+ * We can't kill a registry if we have too-close control
+ * over it.  We must make it in a subprocess, and then kill the
+ * subprocess when it has served our needs.
+ */
+public class REGISTRY extends JavaVM {
+
+    private static double startTimeout = 20_000 * TestLibrary.getTimeoutFactor();
+
+    private int port = -1;
+
+    private REGISTRY(OutputStream out, OutputStream err,
+                    String options, int port) {
+        super("RegistryRunner", options, Integer.toString(port), out, err);
+        this.port = port;
+    }
+
+    public static REGISTRY createREGISTRY() {
+        return createREGISTRY(System.out, System.err, "", 0);
+    }
+
+    public static REGISTRY createREGISTRY(OutputStream out, OutputStream err,
+                                    String options, int port) {
+        options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"
+                 + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"
+                 + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"
+                 + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";
+        REGISTRY reg = new REGISTRY(out, err, options, port);
+        return reg;
+    }
+
+    /**
+     * Starts the registry in a sub-process and waits up to
+     * the given timeout period to confirm that it's running,
+     * and get the port where it's running.
+     */
+    public void start() throws IOException {
+        super.start();
+        long startTime = System.currentTimeMillis();
+        long deadline = TestLibrary.computeDeadline(startTime, (long)startTimeout);
+        while (true) {
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException ignore) { }
+
+            String output = outputStream.ba.toString();
+            port = RegistryRunner.getRegistryPort(output);
+            if (port != -1) {
+                break;
+            }
+            if (System.currentTimeMillis() > deadline) {
+                TestLibrary.bomb("Failed to start registry, giving up after " +
+                    (System.currentTimeMillis() - startTime) + "ms.", null);
+            }
+        }
+    }
+
+    /**
+     * Shuts down the registry.
+     */
+    public void shutdown() {
+        RegistryRunner.requestExit(port);
+    }
+
+    /**
+     * Gets the port where the registry is serving.
+     */
+    public int getPort() {
+        return port;
+    }
+}
--- a/test/java/rmi/testlibrary/RegistryRunner.java	Mon Dec 19 15:01:14 2016 -0800
+++ b/test/java/rmi/testlibrary/RegistryRunner.java	Mon Dec 19 17:09:10 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, 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
@@ -28,13 +28,16 @@
 import java.rmi.server.*;
 
 /**
- * Class to run a registry whos VM can be told to exit remotely; using
+ * Class to run a registry whose VM can be told to exit remotely; using
  * the rmiregistry in this fashion makes tests more robust under
  * windows where Process.destroy() seems not to be 100% reliable.
  */
 public class RegistryRunner extends UnicastRemoteObject
     implements RemoteExiter
 {
+    private static final String PORT_LABEL_START = "RegistryRunner.port.start:";
+    private static final String PORT_LABEL_END = "RegistryRunner.port.end";
+
     private static Registry registry = null;
     private static RemoteExiter exiter = null;
 
@@ -59,16 +62,16 @@
     public static void requestExit(int port) {
 
         try {
-            RemoteExiter exiter =
+            RemoteExiter e =
                 (RemoteExiter)
                 Naming.lookup("rmi://localhost:" +
                               port +
                               "/RemoteExiter");
             try {
-                exiter.exit();
+                e.exit();
             } catch (RemoteException re) {
             }
-            exiter = null;
+            e = null;
         } catch (java.net.MalformedURLException mfue) {
             // will not happen
         } catch (NotBoundException nbe) {
@@ -79,7 +82,21 @@
         }
     }
 
+    public static int getRegistryPort(String output) {
+        int idxStart = output.indexOf(PORT_LABEL_START);
+        int idxEnd = output.indexOf(PORT_LABEL_END);
+        if (idxStart == -1 || idxEnd == -1) {
+            return -1;
+        }
+        idxStart = idxStart+PORT_LABEL_START.length();
+        String portStr = output.substring(idxStart, idxEnd);
+        int port = Integer.valueOf(portStr);
+        System.err.println("registry is running at port: " + port);
+        return port;
+    }
+
     public static void main(String[] args) {
+
         try {
             if (args.length == 0) {
                 System.err.println("Usage: <port>");
@@ -88,17 +105,23 @@
             int port = -1;
             try {
                 port = Integer.parseInt(args[0]);
-            } catch (NumberFormatException nfe) {
-            }
+            } catch (NumberFormatException ignore) { }
 
             // create a registry
             registry = LocateRegistry.createRegistry(port);
+            if (port == 0) {
+                port = TestLibrary.getRegistryPort(registry);
+            }
 
             // create a remote object to tell this VM to exit
             exiter = new RegistryRunner();
             Naming.rebind("rmi://localhost:" + port +
                           "/RemoteExiter", exiter);
 
+            // this output is important for REGISTRY to get the port
+            // where rmiregistry is serving
+            System.out.println(PORT_LABEL_START + port + PORT_LABEL_END);
+
         } catch (Exception e) {
             System.err.println(e.getMessage());
             e.printStackTrace();
--- a/test/java/rmi/testlibrary/RegistryRunner_Stub.java	Mon Dec 19 15:01:14 2016 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 1999, 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
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-// Stub class generated by rmic, do not edit.
-// Contents subject to change without notice.
-
-public final class RegistryRunner_Stub
-    extends java.rmi.server.RemoteStub
-    implements RemoteExiter, java.rmi.Remote
-{
-    private static final long serialVersionUID = 2;
-
-    private static java.lang.reflect.Method $method_exit_0;
-
-    static {
-        try {
-            $method_exit_0 = RemoteExiter.class.getMethod("exit", new java.lang.Class[] {});
-        } catch (java.lang.NoSuchMethodException e) {
-            throw new java.lang.NoSuchMethodError(
-                "stub class initialization failed");
-        }
-    }
-
-    // constructors
-    public RegistryRunner_Stub(java.rmi.server.RemoteRef ref) {
-        super(ref);
-    }
-
-    // methods from remote interfaces
-
-    // implementation of exit()
-    public void exit()
-        throws java.rmi.RemoteException
-    {
-        try {
-            ref.invoke(this, $method_exit_0, null, -6307240473358936408L);
-        } catch (java.lang.RuntimeException e) {
-            throw e;
-        } catch (java.rmi.RemoteException e) {
-            throw e;
-        } catch (java.lang.Exception e) {
-            throw new java.rmi.UnexpectedException("undeclared checked exception", e);
-        }
-    }
-}