Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @library ../../testlibrary36* @build TestLibrary37* @run main/othervm HandshakeTimeout38*/3940import java.net.ServerSocket;41import java.rmi.ConnectException;42import java.rmi.ConnectIOException;43import java.rmi.MarshalException;44import java.rmi.registry.LocateRegistry;45import java.rmi.registry.Registry;4647public class HandshakeTimeout {4849private static final int PORT = TestLibrary.getUnusedRandomPort();50private static final int TIMEOUT = 10000;5152public static void main(String[] args) throws Exception {5354System.setProperty("sun.rmi.transport.tcp.handshakeTimeout",55String.valueOf(TIMEOUT / 2));5657/*58* Listen on port, but never process connections made to it.59*/60ServerSocket serverSocket = new ServerSocket(PORT);6162/*63* Attempt RMI call to port in separate thread.64*/65Registry registry = LocateRegistry.getRegistry(PORT);66Connector connector = new Connector(registry);67Thread t = new Thread(connector);68t.setDaemon(true);69t.start();7071/*72* Wait for call attempt to finished, and analyze result.73*/74t.join(TIMEOUT);75synchronized (connector) {76if (connector.success) {77throw new RuntimeException(78"TEST FAILED: remote call succeeded??");79}80if (connector.exception == null) {81throw new RuntimeException(82"TEST FAILED: remote call did not time out");83} else {84System.err.println("remote call failed with exception:");85connector.exception.printStackTrace();86System.err.println();8788if (connector.exception instanceof MarshalException) {89System.err.println(90"TEST FAILED: MarshalException thrown, expecting " +91"java.rmi.ConnectException or ConnectIOException");92} else if (connector.exception instanceof ConnectException ||93connector.exception instanceof ConnectIOException)94{95System.err.println(96"TEST PASSED: java.rmi.ConnectException or " +97"ConnectIOException thrown");98} else {99throw new RuntimeException(100"TEST FAILED: unexpected Exception thrown",101connector.exception);102}103}104}105}106107private static class Connector implements Runnable {108109private final Registry registry;110111boolean success = false;112Exception exception = null;113114Connector(Registry registry) {115this.registry = registry;116}117118public void run() {119try {120registry.lookup("Dale Cooper");121synchronized (this) {122success = true;123}124} catch (Exception e) {125synchronized (this) {126exception = e;127}128}129}130}131}132133134