jdk/test/java/rmi/transport/handshakeTimeout/HandshakeTimeout.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4322806
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary When an RMI (JRMP) connection is made to a TCP address that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * listening, so the connection is accepted, but the server never responds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * to the initial JRMP handshake (nor does it terminate the connection),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * the client should not hang forever; instead, it should throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * after a reasonable timeout interval.  The exception should be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * java.rmi.ConnectException or ConnectIOException, not a MarshalException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * because it should be clear that no partial call execution has occurred at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * this point (because no data for the invocation has yet been written).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @build HandshakeTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @run main/othervm HandshakeTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.ConnectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.ConnectIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.registry.LocateRegistry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class HandshakeTimeout {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int PORT = 2020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int TIMEOUT = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
	System.setProperty("sun.rmi.transport.tcp.handshakeTimeout",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
			   String.valueOf(TIMEOUT / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	 * Listen on port, but never process connections made to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	ServerSocket serverSocket = new ServerSocket(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	 * Attempt RMI call to port in separate thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	Registry registry = LocateRegistry.getRegistry(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	Connector connector = new Connector(registry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	Thread t = new Thread(connector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	 * Wait for call attempt to finished, and analyze result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	t.join(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	synchronized (connector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	    if (connector.success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
		throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
		    "TEST FAILED: remote call succeeded??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	    if (connector.exception == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
		throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
		    "TEST FAILED: remote call did not time out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
		System.err.println("remote call failed with exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
		connector.exception.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
		System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
		if (connector.exception instanceof MarshalException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
		    System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
			"TEST FAILED: MarshalException thrown, expecting " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
			"java.rmi.ConnectException or ConnectIOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
		} else if (connector.exception instanceof ConnectException ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
			   connector.exception instanceof ConnectIOException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
		{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
		    System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
			"TEST PASSED: java.rmi.ConnectException or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
			"ConnectIOException thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
		} else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
		    throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
			"TEST FAILED: unexpected Exception thrown",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
			connector.exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static class Connector implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
	private final Registry registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Exception exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	Connector(Registry registry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
	    this.registry = registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
	public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
		registry.lookup("Dale Cooper");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
		synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
		    success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
	    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
		synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
		    exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}