test/jdk/jdk/net/RdmaSockets/rsocket/Selector/BasicConnect.java
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/* @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @summary Test for nonblocking connect and finishConnect
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 * @library .. /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @build jdk.test.lib.Utils TestServers
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 main/othervm BasicConnect
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.nio.ByteBuffer;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.nio.channels.SelectionKey;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.nio.channels.Selector;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.nio.channels.spi.SelectorProvider;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.util.Iterator;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
/**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
 * Typically there would be more than one channel registered to select
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
 * on, this test is just a very simple version with only one channel
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
 * registered for the connectSelector.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
public class BasicConnect {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
        Selector connectSelector =
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
            RdmaSockets.openSelector();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
            TestServers.EchoServer echoServer
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
                = TestServers.EchoServer.startNewServer(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
            InetSocketAddress isa
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
                = new InetSocketAddress(echoServer.getAddress(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
                                        echoServer.getPort());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
            SocketChannel sc = RdmaSockets.openSocketChannel(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
                StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
            sc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
            boolean result = sc.connect(isa);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
            if (result) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
                System.out.println("Socket immediately connected on "
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
                        + System.getProperty("os.name")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
                        + ": " + sc);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            while (!result) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
                SelectionKey connectKey = sc.register(connectSelector,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                                                      SelectionKey.OP_CONNECT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
                int keysAdded = connectSelector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
                if (keysAdded > 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
                    Set readyKeys = connectSelector.selectedKeys();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
                    Iterator i = readyKeys.iterator();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
                    while (i.hasNext()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
                        SelectionKey sk = (SelectionKey)i.next();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
                        i.remove();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
                        SocketChannel nextReady = (SocketChannel)sk.channel();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
                        result = nextReady.finishConnect();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
                        if (result)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
                            sk.cancel();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
                }
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
            byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                                     (byte)0xba, (byte)0xbe };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
            ByteBuffer bb = ByteBuffer.wrap(bs);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
            sc.configureBlocking(true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            sc.write(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
            bb.rewind();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
            ByteBuffer bb2 = ByteBuffer.allocateDirect(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
            int n = sc.read(bb2);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
            bb2.flip();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
            sc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
            connectSelector.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
            if (!bb.equals(bb2))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
                throw new Exception("Echoed bytes incorrect: Sent "
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
                                    + bb + ", got " + bb2);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
        } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
            e.printStackTrace();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
            throw new RuntimeException("Test Failed!");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
        } 
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
}