test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/StateTest.java
author mli
Thu, 19 Jul 2018 16:22:19 +0800
changeset 51117 c96c7d08ae49
parent 47216 71c04702a3d5
child 59305 219ec11136d9
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
 * Tests that the channel returned by System.inheritedChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * is in blocking mode, bound, and in the case of a SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * connected to a peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The test works by launching a test service (called StateTestService) so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * that it inherits each type of channel. The test service checks the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * socket state and replies back to this class via an out-of-band
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    36
import java.io.IOException;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    37
import java.net.InetSocketAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
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
    39
import java.nio.channels.DatagramChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    40
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
    41
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
    42
import java.nio.channels.ServerSocketChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    43
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    45
import jdk.test.lib.Utils;
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class StateTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static String TEST_SERVICE = "StateTestService";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Reads the test result from the "out-of-band" connection to the test service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The out-of-band connection is just a TCP connection from the service to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * this class. waitForTestResult just waits (with timeout) for the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * to connect. Once connected it waits (with timeout) for the test result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The test result is examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static void waitForTestResult(ServerSocketChannel ssc, boolean expectFail) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Selector sel = ssc.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        SelectionKey sk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * Wait for service to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    71
        long to = Utils.adjustTimeout(15*1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // connection established
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new IOException("Timed out waiting for service to report test result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * Wait for service to report test result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        sk = sc.register(sel, SelectionKey.OP_READ);
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    94
        to = Utils.adjustTimeout(5000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ByteBuffer bb = ByteBuffer.allocateDirect(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                int n = sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    throw new IOException("Premature EOF - no test result from service");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new IOException("Timed out waiting for service to report test result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * Examine the test result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        byte b = bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (expectFail && b == 'P') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.err.println("Test passed - test is expected to fail!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (!expectFail && b != 'P') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.err.println("Test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static void main(String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        boolean expectFail = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         *   [-expectFail] [options...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String options[] = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (args.length > 0 && args[0].equals("-expectFail")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // shift out first arg to create options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            expectFail = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            options = new String[args.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (args.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                System.arraycopy(args, 1, options, 0, args.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Create the listener which will be used to read the test result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * from the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * The port is passed to the service as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int port = ssc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        String arg[] = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        arg[0] = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * Launch service with a SocketChannel (tcp nowait)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        waitForTestResult(ssc, expectFail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * Launch service with a ServerSocketChannel (tcp wait)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * launchWithServerSocketChannel establishes a connection to the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * and the returned SocketChannel is connected to the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        waitForTestResult(ssc, expectFail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * Launch service with a DatagramChannel (udp wait)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        waitForTestResult(ssc, expectFail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new RuntimeException("Test failed - see log for details");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            System.out.println("All tests passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}