test/jdk/jdk/net/RdmaSockets/rsocket/Selector/Connect.java
author bpb
Wed, 06 Feb 2019 12:48:01 -0800
branchrsocket-branch
changeset 57156 81e4a12fd1a4
parent 57115 512e7cc6ccce
child 57160 c502c299d41e
permissions -rw-r--r--
rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57156
81e4a12fd1a4 rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
bpb
parents: 57115
diff changeset
     2
 * Copyright (c) 2019, 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
/* @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 making lots of Selectors
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 Connect
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.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.net.InetSocketAddress;
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.util.Iterator;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.util.Set;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import static java.net.StandardProtocolFamily.INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
public class Connect {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
    static int success = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    static final int LIMIT = 30;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
        try (TestServers.DayTimeServer daytimeServer
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
                = TestServers.DayTimeServer.startNewServer(50)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
            scaleTest(daytimeServer);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
    static void scaleTest(TestServers.DayTimeServer daytimeServer)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
        throws Exception
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
        InetAddress myAddress = daytimeServer.getAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
        InetSocketAddress isa = new InetSocketAddress(myAddress, daytimeServer.getPort());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        for (int j=0; j<LIMIT; j++) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
            SocketChannel sc = RdmaSockets.openSocketChannel(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 connected = sc.connect(isa);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
            System.out.println("connected = " + connected);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
            if (!connected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
                Selector selector = RdmaSockets.openSelector();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
                sc.register(selector, SelectionKey.OP_CONNECT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
                while (!connected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
                    int keysAdded = selector.select(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                    if (keysAdded > 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
                        Set<SelectionKey> readyKeys = selector.selectedKeys();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
                        Iterator<SelectionKey> i = readyKeys.iterator();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
                        while (i.hasNext()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
                            SelectionKey sk = i.next();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
                            SocketChannel nextReady = (SocketChannel)sk.channel();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
                            connected = nextReady.finishConnect();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
                        readyKeys.clear();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
                selector.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            readAndClose(sc);
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
    static void readAndClose(SocketChannel sc) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
        ByteBuffer bb = ByteBuffer.allocateDirect(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
        int n = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
        while (n == 0) // Note this is not a rigorous check for done reading
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
            n = sc.read(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
        sc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
        success++;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
        System.out.println("success count = " + success);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
}