test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 2008, 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 4322806
  26  * @summary When an RMI (JRMP) connection is made to a TCP address that is
  27  * listening, so the connection is accepted, but the server never responds
  28  * to the initial JRMP handshake (nor does it terminate the connection),
  29  * the client should not hang forever; instead, it should throw an exception
  30  * after a reasonable timeout interval.  The exception should be a
  31  * java.rmi.ConnectException or ConnectIOException, not a MarshalException,
  32  * because it should be clear that no partial call execution has occurred at
  33  * this point (because no data for the invocation has yet been written).
  34  * @author Peter Jones
  35  *
  36  * @build HandshakeTimeout

  37  * @run main/othervm HandshakeTimeout
  38  */
  39 
  40 import java.net.ServerSocket;
  41 import java.rmi.ConnectException;
  42 import java.rmi.ConnectIOException;
  43 import java.rmi.MarshalException;
  44 import java.rmi.registry.LocateRegistry;
  45 import java.rmi.registry.Registry;
  46 
  47 public class HandshakeTimeout {
  48 
  49     private static final int PORT = 2020;
  50     private static final int TIMEOUT = 10000;
  51 
  52     public static void main(String[] args) throws Exception {
  53 
  54         System.setProperty("sun.rmi.transport.tcp.handshakeTimeout",
  55                            String.valueOf(TIMEOUT / 2));
  56 
  57         /*
  58          * Listen on port, but never process connections made to it.
  59          */
  60         ServerSocket serverSocket = new ServerSocket(PORT);
  61 
  62         /*
  63          * Attempt RMI call to port in separate thread.
  64          */
  65         Registry registry = LocateRegistry.getRegistry(PORT);
  66         Connector connector = new Connector(registry);
  67         Thread t = new Thread(connector);
  68         t.setDaemon(true);
  69         t.start();


   1 /*
   2  * Copyright (c) 2001, 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 4322806
  26  * @summary When an RMI (JRMP) connection is made to a TCP address that is
  27  * listening, so the connection is accepted, but the server never responds
  28  * to the initial JRMP handshake (nor does it terminate the connection),
  29  * the client should not hang forever; instead, it should throw an exception
  30  * after a reasonable timeout interval.  The exception should be a
  31  * java.rmi.ConnectException or ConnectIOException, not a MarshalException,
  32  * because it should be clear that no partial call execution has occurred at
  33  * this point (because no data for the invocation has yet been written).
  34  * @author Peter Jones
  35  *
  36  * @library ../../testlibrary
  37  * @build HandshakeTimeout TestLibrary
  38  * @run main/othervm HandshakeTimeout
  39  */
  40 
  41 import java.net.ServerSocket;
  42 import java.rmi.ConnectException;
  43 import java.rmi.ConnectIOException;
  44 import java.rmi.MarshalException;
  45 import java.rmi.registry.LocateRegistry;
  46 import java.rmi.registry.Registry;
  47 
  48 public class HandshakeTimeout {
  49 
  50     private static final int PORT = TestLibrary.getUnusedRandomPort();
  51     private static final int TIMEOUT = 10000;
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         System.setProperty("sun.rmi.transport.tcp.handshakeTimeout",
  56                            String.valueOf(TIMEOUT / 2));
  57 
  58         /*
  59          * Listen on port, but never process connections made to it.
  60          */
  61         ServerSocket serverSocket = new ServerSocket(PORT);
  62 
  63         /*
  64          * Attempt RMI call to port in separate thread.
  65          */
  66         Registry registry = LocateRegistry.getRegistry(PORT);
  67         Connector connector = new Connector(registry);
  68         Thread t = new Thread(connector);
  69         t.setDaemon(true);
  70         t.start();