test/jdk/java/nio/channels/SocketChannel/AdaptSocket.java
author mli
Fri, 16 Mar 2018 18:15:31 +0800
changeset 49255 acdb8531cc8b
parent 47216 71c04702a3d5
child 49702 09c01737ad27
permissions -rw-r--r--
8199215: Re-examine getFreePort method in test infrastructure library Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 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
/* @test
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
    25
 * @bug 8156002
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for socket-channel adaptors
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    27
 * @library .. /test/lib
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    28
 * @build jdk.test.lib.Utils TestServers
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    29
 * @run main AdaptSocket
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
    35
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class AdaptSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static java.io.PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    42
    static void test(TestServers.DayTimeServer dayTimeServer,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    43
                     int timeout,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    44
                     boolean shouldTimeout)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        InetSocketAddress isa
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    50
            = new InetSocketAddress(dayTimeServer.getAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    51
                                    dayTimeServer.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Socket so = sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        out.println("opened: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        out.println("        " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        //out.println("opts:   " + sc.options());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        so.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        so.setKeepAlive(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        so.setSoLinger(true, 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        so.setOOBInline(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        so.setReceiveBufferSize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        so.setSendBufferSize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        //out.println("        " + sc.options());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (timeout == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            so.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                so.connect(isa, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    out.println("Connection timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        out.println("connected: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        out.println("           " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        byte[] bb = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int n = so.getInputStream().read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String s = new String(bb, 0, n - 2, "US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        out.println(isa + " says: \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        so.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.println("ishut: " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        so.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        out.println("oshut: " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        so.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        out.println("closed: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        out.println("        " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static String dataString = "foo\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void testRead(Socket so, boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String data = "foo\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        so.getOutputStream().write(dataString.getBytes("US-ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        InputStream is = so.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            byte[] b = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int n = is.read(b);
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   107
            if (shouldTimeout) {
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   108
                throw new Exception("Should time out, but not, data: " + Arrays.toString(b));
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   109
            }
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   110
            if (n != 5) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new Exception("Incorrect number of bytes read: " + n);
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   112
            }
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   113
            if (!dataString.equals(new String(b, 0, n, "US-ASCII"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                throw new Exception("Incorrect data read: " + n);
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   115
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                out.println("Read timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   125
    static void testRead(TestServers.EchoServer echoServer,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   126
                         int timeout,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   127
                         boolean shouldTimeout)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        InetSocketAddress isa
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   133
            = new InetSocketAddress(echoServer.getAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   134
                                    echoServer.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        sc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Socket so = sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        out.println("connected: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        out.println("           " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (timeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            so.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        out.println("timeout: " + so.getSoTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        testRead(so, shouldTimeout);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   146
        for (int i = 0; i < 4; i++) {
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   147
            out.println("loop: " + i);
13028
f72c7e618ed0 7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents: 7668
diff changeset
   148
            testRead(so, shouldTimeout);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   149
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   156
        try (TestServers.DayTimeServer dayTimeServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   157
                = TestServers.DayTimeServer.startNewServer()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   158
            test(dayTimeServer, 0, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   159
            test(dayTimeServer, 1000, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   160
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   162
        try (TestServers.DayTimeServer lingerDayTimeServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   163
                = TestServers.DayTimeServer.startNewServer(100)) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   164
            // this test no longer really test the connection timeout
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   165
            // since there is no way to prevent the server from eagerly
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   166
            // accepting connection...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   167
            test(lingerDayTimeServer, 10, true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   168
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   170
        try (TestServers.EchoServer echoServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   171
                = TestServers.EchoServer.startNewServer()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   172
            testRead(echoServer, 0, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   173
            testRead(echoServer, 8000, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   174
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   176
        try (TestServers.NoResponseServer noResponseServer
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   177
                = TestServers.NoResponseServer.startNewServer()) {
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 14415
diff changeset
   178
            testRead(noResponseServer, 10, true);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   179
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   180
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}