Path: blob/master/test/jdk/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java
41155 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 432280625* @summary When an RMI (JRMP) connection is made to a TCP address that is26* listening, so the connection is accepted, but the server never responds27* to the initial JRMP handshake (nor does it terminate the connection),28* the client should not hang forever; instead, it should throw an exception29* after a reasonable timeout interval. The exception should be a30* java.rmi.ConnectException or ConnectIOException, not a MarshalException,31* because it should be clear that no partial call execution has occurred at32* this point (because no data for the invocation has yet been written).33* @author Peter Jones34*35* @modules java.rmi/sun.rmi.registry36* java.rmi/sun.rmi.server37* java.rmi/sun.rmi.transport38* java.rmi/sun.rmi.transport.tcp39* @run main/othervm HandshakeTimeout40*/4142import java.net.ServerSocket;43import java.rmi.ConnectException;44import java.rmi.ConnectIOException;45import java.rmi.MarshalException;46import java.rmi.registry.LocateRegistry;47import java.rmi.registry.Registry;4849public class HandshakeTimeout {5051private static final int TIMEOUT = 10000;5253public static void main(String[] args) throws Exception {5455System.setProperty("sun.rmi.transport.tcp.handshakeTimeout",56String.valueOf(TIMEOUT / 2));5758/*59* Listen on port, but never process connections made to it.60*/61ServerSocket serverSocket = new ServerSocket(0);62int port = serverSocket.getLocalPort();6364/*65* Attempt RMI call to port in separate thread.66*/67Registry registry = LocateRegistry.getRegistry(port);68Connector connector = new Connector(registry);69Thread t = new Thread(connector);70t.setDaemon(true);71t.start();7273/*74* Wait for call attempt to finished, and analyze result.75*/76t.join(TIMEOUT);77synchronized (connector) {78if (connector.success) {79throw new RuntimeException(80"TEST FAILED: remote call succeeded??");81}82if (connector.exception == null) {83throw new RuntimeException(84"TEST FAILED: remote call did not time out");85} else {86System.err.println("remote call failed with exception:");87connector.exception.printStackTrace();88System.err.println();8990if (connector.exception instanceof MarshalException) {91throw new RuntimeException(92"TEST FAILED: MarshalException thrown, expecting " +93"java.rmi.ConnectException or ConnectIOException");94} else if (connector.exception instanceof ConnectException ||95connector.exception instanceof ConnectIOException)96{97System.err.println(98"TEST PASSED: java.rmi.ConnectException or " +99"ConnectIOException thrown");100} else {101throw new RuntimeException(102"TEST FAILED: unexpected Exception thrown",103connector.exception);104}105}106}107}108109private static class Connector implements Runnable {110111private final Registry registry;112113boolean success = false;114Exception exception = null;115116Connector(Registry registry) {117this.registry = registry;118}119120public void run() {121try {122registry.lookup("Dale Cooper");123synchronized (this) {124success = true;125}126} catch (Exception e) {127synchronized (this) {128exception = e;129}130}131}132}133}134135136