1 /*
   2  * Copyright (c) 1998, 2012, 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  * @library ../../testlibrary
  30  * @build AddrInUse
  31  * @run main/othervm AddrInUse
  32  */
  33 
  34 import java.net.ServerSocket;
  35 import java.rmi.registry.LocateRegistry;
  36 import java.rmi.server.ExportException;
  37 
  38 public class AddrInUse implements Runnable {
  39 
  40     private static int port = -1;
  41     private static final long TIMEOUT = 10000;
  42 
  43     private boolean exportSucceeded = false;
  44     private Throwable exportException = null;
  45 
  46     public void run() {
  47 
  48         /*
  49          * Attempt to create (i.e. export) a registry on the port that
  50          * has already been bound, and record the result.
  51          */
  52         try {
  53             LocateRegistry.createRegistry(port);
  54             synchronized (this) {
  55                 exportSucceeded = true;
  56                 notifyAll();
  57             }
  58         } catch (Throwable t) {
  59             synchronized (this) {
  60                 exportException = t;
  61                 notifyAll();
  62             }
  63         }
  64     }
  65 
  66     public static void main(String[] args) throws Exception {
  67         System.err.println("\nRegression test for bug 4111507\n");
  68 
  69         /*
  70          * Bind a server socket to a port.
  71          */
  72         ServerSocket server = new ServerSocket(0);
  73         port = server.getLocalPort();
  74         System.err.println("Created a ServerSocket on port " + port + "...");
  75 
  76         /*
  77          * Start a thread that creates a registry on the same port,
  78          * and analyze the result.
  79          */
  80         System.err.println("create a registry on the same port...");
  81         System.err.println("(should cause an ExportException)");
  82         AddrInUse obj = new AddrInUse();
  83         synchronized (obj) {
  84             (new Thread(obj, "AddrInUse")).start();
  85 
  86             /*
  87              * Don't wait forever (original bug is that the export
  88              * hangs).
  89              */
  90             obj.wait(TIMEOUT);
  91 
  92             if (obj.exportSucceeded) {
  93                 throw new RuntimeException(
  94                     "TEST FAILED: export on already-bound port succeeded");
  95             } else if (obj.exportException != null) {
  96                 obj.exportException.printStackTrace();
  97                 if (obj.exportException instanceof ExportException) {
  98                     System.err.println("TEST PASSED");
  99                 } else {
 100                     throw new RuntimeException(
 101                         "TEST FAILED: unexpected exception occurred",
 102                         obj.exportException);
 103                 }
 104             } else {
 105                 throw new RuntimeException("TEST FAILED: export timed out");
 106             }
 107         }
 108     }
 109 }