test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/CloseTest.java
author mli
Thu, 19 Jul 2018 16:22:19 +0800
changeset 51117 c96c7d08ae49
parent 47216 71c04702a3d5
permissions -rw-r--r--
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed Reviewed-by: alanb, simonis
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * This unit test checks that closing the channel returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * System.inheritedChannel closes the underlying network socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The test launches the "echo service" with arguments to instruct the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * service to close the channel after it receives a small message. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * service then delays/lingers for 15 seconds before shuting down. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * prove that the close works we check that we see EOF (meaning the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * peer has closed the connection) in less than 15 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.ByteBuffer;
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    37
import java.nio.channels.SelectionKey;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    38
import java.nio.channels.Selector;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    39
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    41
import jdk.test.lib.Utils;
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class CloseTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String msg = "HELLO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // Launch the service with arguments to tell it to close
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // the connection after reading 5 bytes ("HELLO"). After
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // closing the connection the service should hang around
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // for 15 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String service_args[] = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        service_args[0] = String.valueOf(msg.length());
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    55
        service_args[1] = String.valueOf( Utils.adjustTimeout(15*1000) );
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
        SocketChannel sc = Launcher.launchWithSocketChannel("EchoService", service_args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // send message - service will echo the message and close the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        sc.write(ByteBuffer.wrap(msg.getBytes("UTF-8")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // read the reply (with timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ByteBuffer bb = ByteBuffer.allocateDirect(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Selector sel = sc.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    70
        long to = Utils.adjustTimeout(12*1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                int n = sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                throw new RuntimeException("Timed out waiting for connection to close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // finally check that the reply length is okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (bb.remaining() < msg.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new RuntimeException("Premature EOF from echo service");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("Test passed - service closed connection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}