1 /*
   2  * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4111507
  26  * @summary retryServerSocket should not retry on BindException
  27  * @author Ann Wollrath
  28  *
  29  * @build AddrInUse
  30  * @run main/othervm AddrInUse
  31  */
  32 
  33 import java.net.ServerSocket;
  34 import java.rmi.registry.LocateRegistry;
  35 import java.rmi.server.ExportException;
  36 
  37 public class AddrInUse implements Runnable {
  38 
  39     private static final int PORT = 9999;
  40     private static final long TIMEOUT = 10000;
  41 
  42     private boolean exportSucceeded = false;
  43     private Throwable exportException = null;
  44 
  45     public void run() {
  46 
  47         /*
  48          * Attempt to create (i.e. export) a registry on the port that
  49          * has already been bound, and record the result.
  50          */
  51         try {
  52             LocateRegistry.createRegistry(PORT);
  53             synchronized (this) {
  54                 exportSucceeded = true;
  55                 notifyAll();
  56             }
  57         } catch (Throwable t) {
  58             synchronized (this) {
  59                 exportException = t;
  60                 notifyAll();
  61             }
  62         }
  63     }
  64 
  65     public static void main(String[] args) throws Exception {
  66         System.err.println("\nRegression test for bug 4111507\n");
  67 
  68         /*
  69          * Bind a server socket to a port.
  70          */
  71         System.err.println("create a ServerSocket on port " + PORT + "...");
  72         ServerSocket server = new ServerSocket(PORT);
  73 
  74         /*
  75          * Start a thread that creates a registry on the same port,
  76          * and analyze the result.
  77          */
  78         System.err.println("create a registry on the same port...");
  79         System.err.println("(should cause an ExportException)");
  80         AddrInUse obj = new AddrInUse();
  81         synchronized (obj) {
  82             (new Thread(obj, "AddrInUse")).start();
  83 
  84             /*
  85              * Don't wait forever (original bug is that the export
  86              * hangs).
  87              */
  88             obj.wait(TIMEOUT);
  89 
  90             if (obj.exportSucceeded) {
  91                 throw new RuntimeException(
  92                     "TEST FAILED: export on already-bound port succeeded");
  93             } else if (obj.exportException != null) {
  94                 obj.exportException.printStackTrace();
  95                 if (obj.exportException instanceof ExportException) {
  96                     System.err.println("TEST PASSED");
  97                 } else {
  98                     throw new RuntimeException(
  99                         "TEST FAILED: unexpected exception occurred",
 100                         obj.exportException);
 101                 }
 102             } else {
 103                 throw new RuntimeException("TEST FAILED: export timed out");
 104             }
 105         }
 106     }
 107 }