test/jdk/jdk/net/RdmaSockets/rsocket/SocketChannel/IOExchanges.java
author bpb
Thu, 07 Feb 2019 11:09:09 -0800
branchrsocket-branch
changeset 57160 c502c299d41e
parent 57156 81e4a12fd1a4
permissions -rw-r--r--
rsocket-branch: correct copyright year
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57160
c502c299d41e rsocket-branch: correct copyright year
bpb
parents: 57156
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
57115
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 Tests various combinations of blocking/nonblocking connections
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @library ../ /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
 * @run testng/othervm IOExchanges
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 * @author chegar
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.net.Inet6Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.net.ProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.nio.channels.SelectionKey;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.nio.channels.Selector;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import java.nio.ByteBuffer;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import org.testng.SkipException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
import org.testng.annotations.BeforeTest;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
import org.testng.annotations.Test;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
import static java.lang.System.out;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
import static java.net.StandardProtocolFamily.INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
import static java.net.StandardProtocolFamily.INET6;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
import static java.nio.channels.SelectionKey.OP_ACCEPT;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
import static java.nio.channels.SelectionKey.OP_READ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
import static java.nio.channels.SelectionKey.OP_WRITE;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
import static org.testng.Assert.assertEquals;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
import static org.testng.Assert.assertTrue;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
public class IOExchanges {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
    // Whether, or not, to use RDMA channels or regular socket channels.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
    // For test assertion purposes during development.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
    static final boolean useRDMA = true;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
    static InetAddress addr;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
    static ProtocolFamily family;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
    @BeforeTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
    public void setup() throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
        if (useRDMA && !RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
            throw new SkipException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
        addr = InetAddress.getLocalHost();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        family = addr instanceof Inet6Address ? INET6 : INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
        out.printf("local address: %s%n", addr);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
        out.printf("useRDMA: %b%n", useRDMA);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
    static SocketChannel openSocketChannel(ProtocolFamily family)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
        return useRDMA ? RdmaSockets.openSocketChannel(family)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
                       : SocketChannel.open();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
    static ServerSocketChannel openServerSocketChannel(ProtocolFamily family)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
        return useRDMA ? RdmaSockets.openServerSocketChannel(family)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
                       : ServerSocketChannel.open();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
    static Selector openSelector( ) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
        return useRDMA ? RdmaSockets.openSelector() : Selector.open();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
    /*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
     The following, non-exhaustive set, of tests exercise different combinations
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
     of blocking and non-blocking accept/connect calls along with I/O
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
     operations, that exchange a single byte. The intent it to test a reasonable
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
     set of blocking and non-blocking scenarios.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
      The individual test method names follow their test scenario.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
       [BAccep|SELNBAccep|SPINNBAccep] - Accept either:
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
                        blocking, select-non-blocking, spinning-non-blocking
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
       [BConn|NBConn] - blocking connect / non-blocking connect
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
       [BIO|NBIO]     - blocking / non-blocking I/O operations (read/write)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
       [WR|RW] - connecting thread write/accepting thread reads first, and vice-versa
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
       [Id]    - unique test Id
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
        BAccep_BConn_BIO_WR_1
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
        BAccep_BConn_BIO_RW_2
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
        SELNBAccep_BConn_BIO_WR_3
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
        SELNBAccep_BConn_BIO_RW_4
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
        SPINNBAccep_BConn_BIO_WR_5
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
        SPINNBAccep_BConn_BIO_RW_6
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
        BAccep_NBConn_BIO_WR_7
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
        BAccep_NBConn_BIO_RW_8
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
        SELNBAccep_NBConn_BIO_WR_9
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
        SELNBAccep_NBConn_BIO_RW_10
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
        SPINNBAccep_NBConn_BIO_WR_11
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
        SPINNBAccep_NBConn_BIO_RW_12
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
        BAccep_BConn_NBIO_WR_1a        << Non-Blocking I/O
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
        BAccep_BConn_NBIO_RW_2a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
        SELNBAccep_BConn_NBIO_WR_3a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
        SELNBAccep_BConn_NBIO_RW_4a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
        SPINNBAccep_BConn_NBIO_WR_5a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
        SPINNBAccep_BConn_NBIO_RW_6a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
        BAccep_NBConn_NBIO_WR_7a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
        BAccep_NBConn_NBIO_RW_8a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
        SELNBAccep_NBConn_NBIO_WR_9a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
        SELNBAccep_NBConn_NBIO_RW_10a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
        SPINBAccep_NBConn_NBIO_WR_11a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
        SPINBAccep_NBConn_NBIO_RW_12a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
    */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
    public void BAccep_BConn_BIO_WR_1() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
            TestThread t = TestThread.of("t1", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   143
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   144
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   145
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x01).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   146
                    assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   147
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   148
                    assertEquals(sc.read(bb.clear()), -1);
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 (SocketChannel sc = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   154
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   155
                assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   156
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   157
                assertEquals(bb.get(0), 0x01);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   158
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   159
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   160
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   161
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   162
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   163
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   164
    public void BAccep_BConn_BIO_RW_2() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   165
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   166
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   167
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   168
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   169
            TestThread t = TestThread.of("t2", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   170
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   171
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   172
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   173
                    assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   174
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   175
                    assertEquals(bb.get(0), 0x02);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   176
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   177
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   178
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   179
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   180
            try (SocketChannel sc = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   181
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x02).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   182
                assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   183
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   184
                assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   185
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   186
            t.awaitCompletion();
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
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   191
    public void SELNBAccep_BConn_BIO_WR_3() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   192
        try (ServerSocketChannel ssc = openServerSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   193
             Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   194
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   195
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   196
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   197
            TestThread t = TestThread.of("t3", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   198
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   199
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   200
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x03).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   201
                    assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   202
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   203
                    assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   204
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   205
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   206
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   207
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   208
            ssc.configureBlocking(false).register(selector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   209
            assertEquals(selector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   210
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   211
            try (SocketChannel sc = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   212
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   213
                assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   214
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   215
                assertEquals(bb.get(0), 0x03);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   216
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   217
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   218
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   219
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   220
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   221
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   222
    public void SELNBAccep_BConn_BIO_RW_4() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   223
        try (ServerSocketChannel ssc = openServerSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   224
             Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   225
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   226
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   227
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   228
            TestThread t = TestThread.of("t4", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   229
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   230
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   231
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   232
                    assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   233
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   234
                    assertEquals(bb.get(0), 0x04);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   235
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   236
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   237
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   238
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   239
            ssc.configureBlocking(false).register(selector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   240
            assertEquals(selector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   241
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   242
            try (SocketChannel sc = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   243
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x04).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   244
                assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   245
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   246
                assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   247
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   248
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   249
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   250
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   251
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   252
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   253
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   254
    public void SPINNBAccep_BConn_BIO_WR_5() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   255
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   256
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   257
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   258
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   259
            TestThread t = TestThread.of("t5", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   260
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   261
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   262
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x05).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   263
                    assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   264
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   265
                    assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   266
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   267
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   268
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   269
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   270
            SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   271
            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   272
                accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   273
                if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   274
                    out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   275
                    break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   276
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   277
                Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   278
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   279
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   280
            try (SocketChannel sc = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   281
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   282
                assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   283
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   284
                assertEquals(bb.get(0), 0x05);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   285
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   286
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   287
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   288
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   289
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   290
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   291
    public void SPINNBAccep_BConn_BIO_RW_6() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   292
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   293
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   294
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   295
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   296
            TestThread t = TestThread.of("t6", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   297
                try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   298
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   299
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   300
                    assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   301
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   302
                    assertEquals(bb.get(0), 0x06);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   303
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   304
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   305
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   306
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   307
            SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   308
            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   309
                accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   310
                if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   311
                    out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   312
                    break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   313
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   314
                Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   315
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   316
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   317
            try (SocketChannel sc = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   318
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x06).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   319
                assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   320
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   321
                assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   322
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   323
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   324
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   325
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   326
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   327
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   328
    // Similar to the previous six scenarios, but with same-thread
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   329
    // non-blocking connect.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   330
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   331
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   332
    public void BAccep_NBConn_BIO_WR_7() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   333
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   334
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   335
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   336
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   337
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   338
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   339
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   340
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   341
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   342
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   343
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   344
                    TestThread t = TestThread.of("t7", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   345
                        ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x07).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   346
                        assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   347
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   348
                        assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   349
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   350
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   351
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   352
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   353
                    assertEquals(sc2.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   354
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   355
                    assertEquals(bb.get(0), 0x07);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   356
                    sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   357
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   358
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   359
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   360
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   361
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   362
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   363
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   364
    public void BAccep_NBConn_BIO_RW_8() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   365
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   366
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   367
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   368
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   369
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   370
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   371
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   372
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   373
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   374
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   375
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   376
                    TestThread t = TestThread.of("t8", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   377
                        ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   378
                        assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   379
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   380
                        assertEquals(bb.get(0), 0x08);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   381
                        sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   382
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   383
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   384
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   385
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x08).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   386
                    assertEquals(sc2.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   387
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   388
                    assertEquals(sc2.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   389
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   390
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   391
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   392
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   393
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   394
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   395
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   396
    public void SELNBAccep_NBConn_BIO_WR_9() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   397
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   398
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   399
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   400
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   401
            try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   402
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   403
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   404
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   405
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   406
                ssc.configureBlocking(false).register(selector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   407
                assertEquals(selector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   408
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   409
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   410
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   411
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   412
                    TestThread t = TestThread.of("t9", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   413
                        ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x09).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   414
                        assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   415
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   416
                        assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   417
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   418
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   419
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   420
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   421
                    assertEquals(sc2.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   422
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   423
                    assertEquals(bb.get(0), 0x09);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   424
                    sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   425
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   426
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   427
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   428
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   429
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   430
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   431
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   432
    public void SELNBAccep_NBConn_BIO_RW_10() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   433
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   434
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   435
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   436
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   437
            try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   438
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   439
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   440
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   441
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   442
                ssc.configureBlocking(false).register(selector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   443
                assertEquals(selector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   444
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   445
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   446
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   447
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   448
                    TestThread t = TestThread.of("t10", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   449
                        ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   450
                        assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   451
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   452
                        assertEquals(bb.get(0), 0x10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   453
                        sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   454
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   455
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   456
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   457
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x10).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   458
                    assertEquals(sc2.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   459
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   460
                    assertEquals(sc2.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   461
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   462
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   463
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   464
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   465
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   466
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   467
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   468
    public void SPINNBAccep_NBConn_BIO_WR_11() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   469
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   470
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   471
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   472
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   473
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   474
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   475
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   476
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   477
                SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   478
                for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   479
                    accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   480
                    if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   481
                        out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   482
                        break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   483
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   484
                    Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   485
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   486
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   487
                try (SocketChannel sc2 = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   488
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   489
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   490
                    TestThread t = TestThread.of("t11", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   491
                        ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x11).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   492
                        assertEquals(sc.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   493
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   494
                        assertEquals(sc.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   495
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   496
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   497
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   498
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   499
                    assertEquals(sc2.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   500
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   501
                    assertEquals(bb.get(0), 0x11);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   502
                    sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   503
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   504
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   505
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   506
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   507
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   508
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   509
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   510
    public void SPINNBAccep_NBConn_BIO_RW_12() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   511
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   512
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   513
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   514
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   515
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   516
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   517
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   518
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   519
                SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   520
                for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   521
                    accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   522
                    if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   523
                        out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   524
                        break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   525
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   526
                    Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   527
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   528
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   529
                try (SocketChannel sc2 = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   530
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   531
                    sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   532
                    TestThread t = TestThread.of("t12", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   533
                        ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   534
                        assertEquals(sc.read(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   535
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   536
                        assertEquals(bb.get(0), 0x12);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   537
                        sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   538
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   539
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   540
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   541
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x12).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   542
                    assertEquals(sc2.write(bb), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   543
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   544
                    assertEquals(sc2.read(bb.clear()), -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   545
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   546
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   547
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   548
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   549
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   550
*/
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   551
    // ---
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   552
    // Similar to the previous twelve scenarios but with non-blocking IO
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   553
    // ---
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   554
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   555
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   556
    public void BAccep_BConn_NBIO_WR_1a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   557
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   558
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   559
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   560
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   561
            TestThread t = TestThread.of("t1a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   562
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   563
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   564
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   565
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x1A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   566
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   567
                    SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   568
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   569
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   570
                    while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   571
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   572
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   573
                    k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   574
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   575
                    bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   576
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   577
                    assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   578
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   579
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   580
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   581
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   582
            try (SocketChannel sc = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   583
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   584
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   585
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   586
                sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   587
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   588
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   589
                while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   590
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   591
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   592
                assertEquals(bb.get(0), 0x1A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   593
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   594
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   595
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   596
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   597
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   598
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   599
    public void BAccep_BConn_NBIO_RW_2a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   600
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   601
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   602
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   603
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   604
            TestThread t = TestThread.of("t2a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   605
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   606
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   607
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   608
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   609
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   610
                    sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   611
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   612
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   613
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   614
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   615
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   616
                    assertEquals(bb.get(0), 0x2A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   617
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   618
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   619
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   620
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   621
            try (SocketChannel sc = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   622
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   623
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x2A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   624
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   625
                SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   626
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   627
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   628
                while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   629
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   630
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   631
                k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   632
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   633
                bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   634
                while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   635
                assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   636
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   637
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   638
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   639
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   640
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   641
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   642
    public void SELNBAccep_BConn_NBIO_WR_3a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   643
        try (ServerSocketChannel ssc = openServerSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   644
             Selector aselector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   645
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   646
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   647
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   648
            TestThread t = TestThread.of("t3a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   649
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   650
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   651
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   652
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x3A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   653
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   654
                    SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   655
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   656
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   657
                    while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   658
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   659
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   660
                    k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   661
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   662
                    bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   663
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   664
                    assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   665
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   666
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   667
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   668
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   669
            ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   670
            assertEquals(aselector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   671
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   672
            try (SocketChannel sc = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   673
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   674
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   675
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   676
                sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   677
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   678
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   679
                while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   680
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   681
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   682
                assertEquals(bb.get(0), 0x3A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   683
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   684
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   685
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   686
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   687
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   688
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   689
    public void SELNBAccep_BConn_NBIO_RW_4a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   690
        try (ServerSocketChannel ssc = openServerSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   691
             Selector aselector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   692
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   693
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   694
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   695
            TestThread t = TestThread.of("t4a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   696
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   697
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   698
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   699
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   700
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   701
                    sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   702
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   703
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   704
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   705
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   706
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   707
                    assertEquals(bb.get(0), 0x4A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   708
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   709
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   710
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   711
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   712
            ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   713
            assertEquals(aselector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   714
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   715
            try (SocketChannel sc = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   716
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   717
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x4A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   718
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   719
                SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   720
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   721
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   722
                while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   723
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   724
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   725
                k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   726
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   727
                bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   728
                while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   729
                assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   730
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   731
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   732
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   733
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   734
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   735
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   736
    public void SPINNBAccep_BConn_NBIO_WR_5a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   737
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   738
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   739
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   740
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   741
            TestThread t = TestThread.of("t5a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   742
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   743
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   744
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   745
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x5A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   746
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   747
                    SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   748
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   749
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   750
                    while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   751
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   752
                    out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   753
                    k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   754
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   755
                    bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   756
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   757
                    assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   758
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   759
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   760
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   761
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   762
            SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   763
            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   764
                accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   765
                if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   766
                    out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   767
                    break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   768
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   769
                Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   770
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   771
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   772
            try (SocketChannel sc = accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   773
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   774
                ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   775
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   776
                sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   777
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   778
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   779
                while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   780
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   781
                out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   782
                assertEquals(bb.get(0), 0x5A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   783
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   784
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   785
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   786
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   787
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   788
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   789
    public void SPINNBAccep_BConn_NBIO_RW_6a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   790
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   791
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   792
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   793
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   794
            TestThread t = TestThread.of("t6a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   795
                try (SocketChannel sc = openSocketChannel(family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   796
                     Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   797
                    assertTrue(sc.connect(new InetSocketAddress(addr, port)));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   798
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   799
                    sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   800
                    sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   801
                    selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   802
                    int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   803
                    while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   804
                    assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   805
                    out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   806
                    assertEquals(bb.get(0), 0x6A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   807
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   808
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   809
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   810
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   811
            SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   812
            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   813
                accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   814
                if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   815
                    out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   816
                    break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   817
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   818
                Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   819
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   820
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   821
            try (SocketChannel sc = accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   822
                 Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   823
                ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x6A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   824
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   825
                SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   826
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   827
                int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   828
                while ((c = sc.write(bb)) < 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   829
                assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   830
                out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   831
                k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   832
                selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   833
                bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   834
                while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   835
                assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   836
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   837
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   838
            t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   839
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   840
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   841
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   842
    // Similar to the previous six scenarios but with same-thread
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   843
    // non-blocking connect.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   844
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   845
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   846
    public void BAccep_NBConn_NBIO_WR_7a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   847
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   848
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   849
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   850
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   851
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   852
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   853
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   854
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   855
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   856
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   857
                    TestThread t = TestThread.of("t7a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   858
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   859
                            ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x7A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   860
                            sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   861
                            SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   862
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   863
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   864
                            while ((c = sc.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   865
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   866
                            out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   867
                            k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   868
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   869
                            bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   870
                            while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   871
                            assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   872
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   873
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   874
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   875
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   876
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   877
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   878
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   879
                        sc2.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   880
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   881
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   882
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   883
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   884
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   885
                        assertEquals(bb.get(0), 0x7A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   886
                        sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   887
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   888
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   889
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   890
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   891
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   892
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   893
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   894
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   895
    public void BAccep_NBConn_NBIO_RW_8a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   896
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   897
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   898
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   899
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   900
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   901
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   902
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   903
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   904
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   905
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   906
                    TestThread t = TestThread.of("t8a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   907
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   908
                            ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   909
                            sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   910
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   911
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   912
                            while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   913
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   914
                            out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   915
                            assertEquals(bb.get(0), (byte)0x8A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   916
                            sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   917
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   918
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   919
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   920
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   921
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x8A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   922
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   923
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   924
                        SelectionKey k = sc2.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   925
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   926
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   927
                        while ((c = sc2.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   928
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   929
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   930
                        k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   931
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   932
                        bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   933
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   934
                        assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   935
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   936
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   937
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   938
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   939
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   940
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   941
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   942
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   943
    public void SELNBAccep_NBConn_NBIO_WR_9a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   944
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   945
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   946
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   947
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   948
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   949
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   950
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   951
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   952
                Selector aselector = openSelector();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   953
                ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   954
                assertEquals(aselector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   955
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   956
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   957
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   958
                    TestThread t = TestThread.of("t9a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   959
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   960
                            ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0x9A).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   961
                            sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   962
                            SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   963
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   964
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   965
                            while ((c = sc.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   966
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   967
                            out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   968
                            k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   969
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   970
                            bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   971
                            while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   972
                            assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   973
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   974
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   975
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   976
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   977
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   978
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   979
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   980
                        sc2.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   981
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   982
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   983
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   984
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   985
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   986
                        assertEquals(bb.get(0), (byte)0x9A);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   987
                        sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   988
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   989
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   990
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   991
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   992
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   993
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   994
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   995
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   996
    public void SELNBAccep_NBConn_NBIO_RW_10a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   997
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   998
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   999
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1000
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1001
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1002
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1003
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1004
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1005
                Selector aselector = openSelector();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1006
                ssc.configureBlocking(false).register(aselector, OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1007
                assertEquals(aselector.select(), 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1008
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1009
                try (SocketChannel sc2 = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1010
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1011
                    TestThread t = TestThread.of("t10a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1012
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1013
                            ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1014
                            sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1015
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1016
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1017
                            while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1018
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1019
                            out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1020
                            assertEquals(bb.get(0), (byte)0xAA);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1021
                            sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1022
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1023
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1024
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1025
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1026
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0xAA).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1027
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1028
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1029
                        SelectionKey k = sc2.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1030
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1031
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1032
                        while ((c = sc2.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1033
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1034
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1035
                        k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1036
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1037
                        bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1038
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1039
                        assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1040
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1041
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1042
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1043
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1044
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1045
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1046
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1047
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1048
    public void SPINBAccep_NBConn_NBIO_WR_11a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1049
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1050
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1051
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1052
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1053
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1054
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1055
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1056
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1057
                SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1058
                for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1059
                    accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1060
                    if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1061
                        out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1062
                        break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1063
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1064
                    Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1065
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1066
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1067
                try (SocketChannel sc2 = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1068
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1069
                    TestThread t = TestThread.of("t11a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1070
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1071
                            ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0xBA).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1072
                            sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1073
                            SelectionKey k = sc.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1074
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1075
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1076
                            while ((c = sc.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1077
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1078
                            out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1079
                            k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1080
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1081
                            bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1082
                            while ((c = sc.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1083
                            assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1084
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1085
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1086
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1087
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1088
                    ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1089
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1090
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1091
                        sc2.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1092
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1093
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1094
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1095
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1096
                        out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1097
                        assertEquals(bb.get(0), (byte)0xBA);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1098
                        sc2.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1099
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1100
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1101
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1102
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1103
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1104
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1105
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1106
    @Test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1107
    public void SPINBAccep_NBConn_NBIO_RW_12a() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1108
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1109
            ssc.bind(new InetSocketAddress(addr, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1110
            final int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1111
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1112
            try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1113
                sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1114
                sc.connect(new InetSocketAddress(addr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1116
                SocketChannel accepted;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1117
                for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1118
                    accepted = ssc.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1119
                    if (accepted != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1120
                        out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1121
                        break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1122
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1123
                    Thread.onSpinWait();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1124
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1125
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1126
                try (SocketChannel sc2 = accepted) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1127
                    assertTrue(sc.finishConnect());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1128
                    TestThread t = TestThread.of("t10a", () -> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1129
                        try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1130
                            ByteBuffer bb = ByteBuffer.allocate(10);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1131
                            sc.register(selector, OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1132
                            selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1133
                            int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1134
                            while ((c = sc.read(bb)) == 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1135
                            assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1136
                            out.printf("read:  0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1137
                            assertEquals(bb.get(0), (byte)0xCA);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1138
                            sc.shutdownOutput();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1139
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1140
                    });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1141
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1142
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1143
                    ByteBuffer bb = ByteBuffer.allocate(10).put((byte)0xCA).flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1144
                    sc2.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1145
                    try (Selector selector = openSelector()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1146
                        SelectionKey k = sc2.register(selector, OP_WRITE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1147
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1148
                        int c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1149
                        while ((c = sc2.write(bb)) < 1) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1150
                        assertEquals(c, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1151
                        out.printf("wrote: 0x%x%n", bb.get(0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1152
                        k.interestOps(OP_READ);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1153
                        selector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1154
                        bb.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1155
                        while ((c = sc2.read(bb)) == 0) ;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1156
                        assertEquals(c, -1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1157
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1158
                    t.awaitCompletion();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1159
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1160
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1161
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1162
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1163
*/
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1164
    // --
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1165
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1166
    static class TestThread extends Thread {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1167
        private final UncheckedRunnable runnable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1168
        private volatile Throwable throwable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1169
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1170
        TestThread(UncheckedRunnable runnable, String name) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1171
            super(name);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1172
            this.runnable = runnable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1173
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1174
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1175
        @Override
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1176
        public void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1177
            try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1178
                runnable.run();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1179
            } catch (Throwable t) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1180
                out.printf("[%s] caught unexpected: %s%n", getName(), t);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1181
                throwable = t;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1182
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1183
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1184
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1185
        interface UncheckedRunnable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1186
            void run() throws Throwable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1187
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1188
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1189
        static TestThread of(String name, UncheckedRunnable runnable) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1190
            return new TestThread(runnable, name);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1191
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1192
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1193
        void awaitCompletion() throws Throwable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1194
            this.join();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1195
            if (throwable != null)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1196
                throw throwable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1197
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1198
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
  1199
}