test/jdk/java/rmi/registry/reexport/Reexport.java
author mli
Wed, 11 Apr 2018 09:08:43 +0800
changeset 49570 25515c7e96b0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use Reviewed-by: rriggs, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
     1
/*
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
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 4120329
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary RMI registry creation is impossible if first attempt fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../testlibrary
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 15005
diff changeset
    28
 * @modules java.rmi/sun.rmi.registry
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 15005
diff changeset
    29
 *          java.rmi/sun.rmi.server
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 15005
diff changeset
    30
 *          java.rmi/sun.rmi.transport
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 15005
diff changeset
    31
 *          java.rmi/sun.rmi.transport.tcp
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    32
 * @build TestLibrary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main/othervm Reexport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    36
/*
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    37
 * If a VM could not create an RMI registry because the registry port
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    38
 * was already occupied by this or other processes, the next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * time the VM tried to create a registry (after the other registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * was brought down) the attempt would fail.  The second try to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a registry would fail because the registry ObjID would still be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * use when it should never have been allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    44
 * The test creates this conflict starting a dummy tcp server and ensures
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    45
 * that a registry can still be created after the conflict is resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    48
import java.io.IOException;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    49
import java.net.InetSocketAddress;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    50
import java.nio.channels.ServerSocketChannel;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    51
import java.rmi.registry.LocateRegistry;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    52
import java.rmi.registry.Registry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class Reexport {
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    55
    static public void main(String[] argv) throws IOException {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    56
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    57
        for (int loop = 0; loop < 10; loop++) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    58
            System.err.println("\nat loop: " + loop);
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    59
            int port = -1;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    60
            try (ServerSocketChannel server = ServerSocketChannel.open();) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    61
                server.bind(null);
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    62
                InetSocketAddress addr = (InetSocketAddress)server.getLocalAddress();
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    63
                port = addr.getPort();
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    64
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    65
                System.err.println("Creating duplicate registry, this should fail...");
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    66
                createReg(port, true);
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    67
            }
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    68
            try {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    69
                if (createReg(port, false) == null) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    70
                    TestLibrary.bomb("Could not create registry on second try");
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    71
                }
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    72
                System.err.println("Test passed");
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    73
                return;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    74
            } catch (Exception e) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    75
                String err = e.getMessage();
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    76
                if (err.contains("Address already in use")
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    77
                        || err.contains("Port already in use")) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    78
                    continue;
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    79
                }
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    80
                TestLibrary.bomb(e);
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    81
            }
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    82
        }
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    83
        TestLibrary.bomb("Test failed");
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    84
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    86
    static Registry createReg(int port, boolean expectException) {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    87
        Registry reg = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    89
        try {
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    90
            reg = LocateRegistry.createRegistry(port);
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    91
            if (expectException) {
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    92
                TestLibrary.bomb("Registry is up, an Exception is expected!");
42760
902ae4f76a3e 8171133: java/rmi/registry/reexport/Reexport.java, there is a missing case check in createReg(..)
mli
parents: 30820
diff changeset
    93
            }
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    94
        } catch (Throwable e) {
49570
25515c7e96b0 8188897: java/rmi/registry/reexport/Reexport.java failed with Port already in use
mli
parents: 47216
diff changeset
    95
            if (expectException) {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    96
                System.err.println("EXPECTING PORT IN USE EXCEPTION:");
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    97
                System.err.println(e.getMessage());
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    98
                e.printStackTrace();
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    99
            } else {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   100
                TestLibrary.bomb((Exception) e);
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   101
            }
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   102
        }
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   103
        return reg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}