test/jdk/jdk/net/RdmaSockets/rsocket/SocketChannel/BasicSocketChannelTest.java
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @summary Test for basic functionality for RdmaSocketChannel
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 *         and RdmaServerSocketChannel
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
 * @library ../ /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 * @run main/othervm BasicSocketChannelTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.io.InputStream;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.io.OutputStream;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.io.UncheckedIOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.net.*;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.nio.channels.ClosedChannelException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import java.nio.ByteBuffer;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import static java.lang.String.format;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
import static java.lang.System.out;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
import static java.net.StandardProtocolFamily.INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
import static java.net.StandardProtocolFamily.INET6;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
import static java.nio.charset.StandardCharsets.UTF_8;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
public class BasicSocketChannelTest {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
    static final String MESSAGE = "This is a MESSAGE!";
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
    static final int MESSAGE_LENGTH = MESSAGE.length();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
    public static void main(String args[]) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
        InetAddress iaddr = InetAddress.getLocalHost();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
        ProtocolFamily family = iaddr instanceof Inet6Address ? INET6 : INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
        out.printf("local address: %s%n", iaddr);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
        testChannel(iaddr, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
        testSocket(iaddr, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
    // Tests SocketChannel and ServerSocketChannel
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
    static void testChannel(InetAddress iaddr, ProtocolFamily family) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
        out.printf("--- testChannel iaddr:%s, family:%s%n", iaddr, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
            ssc.bind(new InetSocketAddress(iaddr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            Thread t = new Thread("Channel") {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                public void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
                    try (SocketChannel sc1 = RdmaSockets.openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
                        sc1.connect(new InetSocketAddress(iaddr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
                        if (!sc1.isConnected())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
                            throw new RuntimeException("Unconnected channel:" + sc1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
                        ByteBuffer input = ByteBuffer.allocate(MESSAGE_LENGTH);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
                        input.put(MESSAGE.getBytes(UTF_8));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
                        input.flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
                        int inputNum = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
                        while (inputNum < MESSAGE_LENGTH) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
                            inputNum += sc1.write(input);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
                        sc1.shutdownInput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
                        assertInputShutdown(sc1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
                        sc1.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
                        assertOutputShutdown(sc1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
                    } catch (IOException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                        throw new UncheckedIOException(e);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
            try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
                ByteBuffer output = ByteBuffer.allocate(MESSAGE_LENGTH + 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
                while (sc2.read(output) != -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
                output.flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
                String result = UTF_8.decode(output).toString();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
                if (!result.equals(MESSAGE)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
                    String msg = format("Expected [%s], received [%s]", MESSAGE, result);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
                    throw new RuntimeException("Test Failed! " + msg);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
                sc2.shutdownInput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
                assertInputShutdown(sc2);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
                sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
                assertOutputShutdown(sc2);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
            t.join();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
            out.printf("passed%n");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
    // Tests SocketChannel.socket() and ServerSocketChannel.socket()
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
    static void testSocket(InetAddress iaddr, ProtocolFamily family) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
        out.printf("--- testSocket iaddr:%s, family:%s%n", iaddr, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
        try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
            ssc.socket().bind(new InetSocketAddress(iaddr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
            Thread t = new Thread("Socket") {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
                public void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
                    try (SocketChannel sc1 = RdmaSockets.openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
                         Socket client = sc1.socket()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
                        client.connect(new InetSocketAddress(iaddr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
                        if (!client.isConnected())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
                            throw new RuntimeException("Test Failed!");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
                        InputStream is = client.getInputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
                        OutputStream os = client.getOutputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
                        os.write(MESSAGE.getBytes(UTF_8));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
                        client.shutdownInput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
                        assertInputShutdown(sc1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
                        assertInputShutdown(client, is);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   143
                        client.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   144
                        assertOutputShutdown(sc1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   145
                        assertOutputShutdown(client, os);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   146
                    } catch (IOException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   147
                        throw new UncheckedIOException(e);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   148
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   149
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   150
            };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   151
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   152
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   153
            try (Socket conn = ssc.socket().accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   154
                InputStream is = conn.getInputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   155
                OutputStream os = conn.getOutputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   156
                byte[] buf = is.readAllBytes();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   157
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   158
                String result = new String(buf, UTF_8);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   159
                if (!result.equals(MESSAGE)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   160
                    String msg = format("Expected [%s], received [%s]", MESSAGE, result);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   161
                    throw new RuntimeException("Test Failed! " + msg);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   162
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   163
                conn.shutdownInput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   164
                assertInputShutdown(conn, is);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   165
                conn.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   166
                assertOutputShutdown(conn, os);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   167
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   168
            t.join();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   169
            out.printf("passed%n");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   170
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   171
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   172
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   173
    static void assertInputShutdown(SocketChannel sc) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   174
        ByteBuffer bb = ByteBuffer.allocate(1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   175
        int r = sc.read(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   176
        if (r != -1)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   177
            throw new RuntimeException(format("Unexpected read of %d bytes", r));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   178
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   179
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   180
            sc.socket().getInputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   181
        } catch (IOException expected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   182
            String msg = expected.getMessage();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   183
            if (!msg.contains("input"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   184
                throw new RuntimeException("Expected to find \"input\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   185
            if (!msg.contains("shutdown"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   186
                throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   187
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   188
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   189
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   190
    static void assertOutputShutdown(SocketChannel sc) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   191
        ByteBuffer bb = ByteBuffer.allocate(1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   192
        bb.put((byte)0x05);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   193
        bb.flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   194
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   195
            sc.write(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   196
            throw new RuntimeException("Unexpected write of bytes");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   197
        } catch (ClosedChannelException expected) { }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   198
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   199
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   200
            sc.socket().getOutputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   201
        } catch (IOException expected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   202
            String msg = expected.getMessage();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   203
            if (!msg.contains("output"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   204
                throw new RuntimeException("Expected to find \"output\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   205
            if (!msg.contains("shutdown"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   206
                throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   207
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   208
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   209
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   210
    static void assertInputShutdown(Socket s, InputStream is) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   211
        if (!s.isInputShutdown()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   212
            throw new RuntimeException("Unexpected open input: " + s);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   213
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   214
        int r;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   215
        if ((r = is.read()) != -1)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   216
            throw new RuntimeException(format("Unexpected read of %d", r));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   217
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   218
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   219
            s.getInputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   220
        } catch (IOException expected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   221
            String msg = expected.getMessage();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   222
            if (!msg.contains("input"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   223
                throw new RuntimeException("Expected to find \"input\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   224
            if (!msg.contains("shutdown"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   225
                throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   226
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   227
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   228
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   229
    static void assertOutputShutdown(Socket s, OutputStream os) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   230
        if (!s.isOutputShutdown()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   231
            throw new RuntimeException("Unexpected open output: " + s);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   232
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   233
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   234
            os.write((byte)0x07);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   235
            throw new RuntimeException("Unexpected write of bytes");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   236
        } catch (ClosedChannelException expected) { }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   237
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   238
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   239
            s.getOutputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   240
        } catch (IOException expected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   241
            String msg = expected.getMessage();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   242
            if (!msg.contains("output"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   243
                throw new RuntimeException("Expected to find \"output\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   244
            if (!msg.contains("shutdown"))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   245
                throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   246
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   247
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   248
}