test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoTest.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
 * Used in conjunction to EchoService to test System.inheritedChannel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The first test is the TCP echo test. A service is launched with a TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * socket and a TCP message is sent to the service. The test checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * the message is correctly echoed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The second test is a UDP echo test. A service is launched with a UDP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * socket and a UDP packet is sent to the service. The test checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the packet is correctly echoed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    38
import java.io.IOException;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    39
import java.net.DatagramPacket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.ByteBuffer;
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    41
import java.nio.channels.DatagramChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    42
import java.nio.channels.SelectionKey;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    43
import java.nio.channels.Selector;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 7668
diff changeset
    44
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    47
import jdk.test.lib.Utils;
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class EchoTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String ECHO_SERVICE = "EchoService";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Sends a message with random bytes to the service, and then waits for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * a reply (with timeout). Once the reply is received it is checked to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * that it matches the original message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static void TCPEchoTest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        SocketChannel sc = Launcher.launchWithSocketChannel(ECHO_SERVICE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String msg = "Where's that damn torpedo?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int repeat = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int size = msg.length() * repeat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // generate bytes into a buffer and send it to the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ByteBuffer bb1 = ByteBuffer.allocate(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Random gen = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i=0; i<repeat; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            bb1.put(msg.getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        bb1.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sc.write(bb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // now we put the channel into non-blocking mode and we read the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // reply from the service into a second buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ByteBuffer bb2 = ByteBuffer.allocate(size+100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Selector sel = sc.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int nread = 0;
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    85
        long to = Utils.adjustTimeout(5000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        while (nread < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                int n = sc.read(bb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    nread += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    break;              // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // and compare the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        boolean err = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (nread != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            bb1.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            bb2.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            while (bb1.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (bb1.get() != bb2.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    err = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // if error print out the response from the service (could be a stack trace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.err.println("Bad response or premature EOF, bytes read: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            bb2.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            while (bb2.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                char c = (char)bb2.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.print(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new RuntimeException("Bad response or premature EOF from service");
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Send a UDP packet to the service, wait for a reply (with timeout). Finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * check that the packet is the same length as the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void UDPEchoTest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        DatagramChannel dc = Launcher.launchWithDatagramChannel(ECHO_SERVICE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String msg = "I was out saving the galaxy when your grandfather was in diapers";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ByteBuffer bb = ByteBuffer.wrap(msg.getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // and receive the echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        byte b[] = new byte[msg.length() + 100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        DatagramPacket pkt2 = new DatagramPacket(b, b.length);
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
   149
        dc.socket().setSoTimeout((int)Utils.adjustTimeout(5000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        dc.socket().receive(pkt2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (pkt2.getLength() != msg.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new RuntimeException("Received packet of incorrect length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static void main(String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // TCP echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            TCPEchoTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println("TCP echo test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            System.err.println(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // UDP echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            UDPEchoTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.out.println("UDP echo test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.err.println(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new RuntimeException("Test failed - see log for details");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}