test/jdk/java/rmi/registry/reexport/Reexport.java
changeset 49570 25515c7e96b0
parent 47216 71c04702a3d5
equal deleted inserted replaced
49569:d4d2f634b72f 49570:25515c7e96b0
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @library ../../testlibrary
    27  * @library ../../testlibrary
    28  * @modules java.rmi/sun.rmi.registry
    28  * @modules java.rmi/sun.rmi.registry
    29  *          java.rmi/sun.rmi.server
    29  *          java.rmi/sun.rmi.server
    30  *          java.rmi/sun.rmi.transport
    30  *          java.rmi/sun.rmi.transport
    31  *          java.rmi/sun.rmi.transport.tcp
    31  *          java.rmi/sun.rmi.transport.tcp
    32  * @build TestLibrary RegistryVM RegistryRunner
    32  * @build TestLibrary
    33  * @run main/othervm Reexport
    33  * @run main/othervm Reexport
    34  */
    34  */
    35 
    35 
    36 /*
    36 /*
    37  * If a VM could not create an RMI registry because another registry
    37  * If a VM could not create an RMI registry because the registry port
    38  * usually in another process, was using the registry port, the next
    38  * was already occupied by this or other processes, the next
    39  * time the VM tried to create a registry (after the other registry
    39  * time the VM tried to create a registry (after the other registry
    40  * was brought down) the attempt would fail.  The second try to create
    40  * was brought down) the attempt would fail.  The second try to create
    41  * a registry would fail because the registry ObjID would still be in
    41  * a registry would fail because the registry ObjID would still be in
    42  * use when it should never have been allocated.
    42  * use when it should never have been allocated.
    43  *
    43  *
    44  * The test creates this conflict using Runtime.exec and ensures that
    44  * The test creates this conflict starting a dummy tcp server and ensures
    45  * a registry can still be created after the conflict is resolved.
    45  * that a registry can still be created after the conflict is resolved.
    46  */
    46  */
    47 
    47 
    48 import java.io.*;
    48 import java.io.IOException;
    49 import java.rmi.*;
    49 import java.net.InetSocketAddress;
    50 import java.rmi.registry.*;
    50 import java.nio.channels.ServerSocketChannel;
    51 import java.rmi.server.*;
    51 import java.rmi.registry.LocateRegistry;
       
    52 import java.rmi.registry.Registry;
    52 
    53 
    53 public class Reexport {
    54 public class Reexport {
    54     static public void main(String[] argv) {
    55     static public void main(String[] argv) throws IOException {
    55 
    56 
    56         Registry reg = null;
    57         for (int loop = 0; loop < 10; loop++) {
    57         try {
    58             System.err.println("\nat loop: " + loop);
    58             System.err.println("\nregression test for 4120329\n");
    59             int port = -1;
       
    60             try (ServerSocketChannel server = ServerSocketChannel.open();) {
       
    61                 server.bind(null);
       
    62                 InetSocketAddress addr = (InetSocketAddress)server.getLocalAddress();
       
    63                 port = addr.getPort();
    59 
    64 
    60             // establish the registry (we hope)
    65                 System.err.println("Creating duplicate registry, this should fail...");
    61             makeRegistry();
    66                 createReg(port, true);
    62 
    67             }
    63             // Get a handle to the registry
       
    64             System.err.println("Creating duplicate registry, this should fail...");
       
    65             reg = createReg(true);
       
    66 
       
    67             // Kill the first registry.
       
    68             System.err.println("Bringing down the first registry");
       
    69             try {
    68             try {
    70                 killRegistry();
    69                 if (createReg(port, false) == null) {
    71             } catch (Exception foo) {
    70                     TestLibrary.bomb("Could not create registry on second try");
       
    71                 }
       
    72                 System.err.println("Test passed");
       
    73                 return;
       
    74             } catch (Exception e) {
       
    75                 String err = e.getMessage();
       
    76                 if (err.contains("Address already in use")
       
    77                         || err.contains("Port already in use")) {
       
    78                     continue;
       
    79                 }
       
    80                 TestLibrary.bomb(e);
    72             }
    81             }
    73 
       
    74             // start another registry now that the first is gone; this should work
       
    75             System.err.println("Trying again to start our own " +
       
    76                                "registry... this should work");
       
    77 
       
    78             reg = createReg(false);
       
    79 
       
    80             if (reg == null) {
       
    81                 TestLibrary.bomb("Could not create registry on second try");
       
    82             }
       
    83 
       
    84             System.err.println("Test passed");
       
    85 
       
    86         } catch (Exception e) {
       
    87             TestLibrary.bomb(e);
       
    88         } finally {
       
    89             // dont leave the registry around to affect other tests.
       
    90             killRegistry();
       
    91             reg = null;
       
    92         }
    82         }
       
    83         TestLibrary.bomb("Test failed");
    93     }
    84     }
    94 
    85 
    95     static Registry createReg(boolean remoteOk) {
    86     static Registry createReg(int port, boolean expectException) {
    96         Registry reg = null;
    87         Registry reg = null;
    97 
    88 
    98         try {
    89         try {
    99             reg = LocateRegistry.createRegistry(port);
    90             reg = LocateRegistry.createRegistry(port);
   100             if (remoteOk) {
    91             if (expectException) {
   101                 TestLibrary.bomb("Remote registry is up, an Exception is expected!");
    92                 TestLibrary.bomb("Registry is up, an Exception is expected!");
   102             }
    93             }
   103         } catch (Throwable e) {
    94         } catch (Throwable e) {
   104             if (remoteOk) {
    95             if (expectException) {
   105                 System.err.println("EXPECTING PORT IN USE EXCEPTION:");
    96                 System.err.println("EXPECTING PORT IN USE EXCEPTION:");
   106                 System.err.println(e.getMessage());
    97                 System.err.println(e.getMessage());
   107                 e.printStackTrace();
    98                 e.printStackTrace();
   108             } else {
    99             } else {
   109                 TestLibrary.bomb((Exception) e);
   100                 TestLibrary.bomb((Exception) e);
   110             }
   101             }
   111         }
   102         }
   112         return reg;
   103         return reg;
   113     }
   104     }
   114 
       
   115     public static void makeRegistry() {
       
   116         try {
       
   117             subreg = RegistryVM.createRegistryVM();
       
   118             subreg.start();
       
   119             port = subreg.getPort();
       
   120             System.out.println("Starting registry on port " + port);
       
   121         } catch (IOException e) {
       
   122             // one of these is summarily dropped, can't remember which one
       
   123             System.out.println ("Test setup failed - cannot run rmiregistry");
       
   124             TestLibrary.bomb("Test setup failed - cannot run test", e);
       
   125         }
       
   126     }
       
   127 
       
   128     private static RegistryVM subreg = null;
       
   129     private static int port = -1;
       
   130 
       
   131     public static void killRegistry() {
       
   132         if (subreg != null) {
       
   133             subreg.cleanup();
       
   134             subreg = null;
       
   135         }
       
   136     }
       
   137 }
   105 }