test/jdk/jdk/net/RdmaSockets/rsocket/Selector/BasicAccept.java
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @summary Test RdmaSelector with RdmaServerSocketChannels
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 /test/jdk/java/nio/channels 
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 BasicAccept
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.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.nio.ByteBuffer;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.nio.channels.SelectionKey;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.nio.channels.Selector;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import java.nio.channels.spi.SelectorProvider;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import java.util.Iterator;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import java.util.Set;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
public class BasicAccept {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    static void server(ServerSocketChannel ssc) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
        Selector acceptSelector = RdmaSockets.openSelector();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
            ssc.configureBlocking(false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
            SelectionKey acceptKey
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
                = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
                int n = acceptSelector.select();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
                if (Thread.interrupted())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
                    break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
                if (n == 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
                    continue;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
                Set<SelectionKey> readyKeys = acceptSelector.selectedKeys();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
                Iterator<SelectionKey> i = readyKeys.iterator();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
                while (i.hasNext()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
                    SelectionKey sk = i.next();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
                    i.remove();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
                    ServerSocketChannel nextReady
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
                        = (ServerSocketChannel)sk.channel();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
                    SocketChannel sc = nextReady.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
                    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
                    sc.write(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
                    sc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
            acceptSelector.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
    private static class Server extends TestThread {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
        final ServerSocketChannel ssc;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        Server() throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
            super("Server", System.err);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
            this.ssc = RdmaSockets.openServerSocketChannel(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
                StandardProtocolFamily.INET)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
                .bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
        int port() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            return ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
        void go() throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
            try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                server(ssc);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
            } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
                ssc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
    static void client(int port) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
        // Get a connection from the server
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
        InetAddress lh = InetAddress.getLocalHost();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
        InetSocketAddress isa
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
            = new InetSocketAddress(lh, port);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
        int connectFailures = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
        boolean result = false;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
        SocketChannel sc = RdmaSockets.openSocketChannel(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
            StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
        for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
            try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
                result = sc.connect(isa);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
                break;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
            } catch (java.net.ConnectException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
                connectFailures++;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
                if (connectFailures > 30)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
                    throw new RuntimeException("Cannot connect");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
                Thread.currentThread().sleep(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
                sc = RdmaSockets.openSocketChannel(StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
        if (result) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
            System.err.println("Connected");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
            // Only happens when server and client are on separate machines
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
            System.err.println("Connection pending...");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
            connectFailures = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
            while (!result) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
                try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
                    result = sc.finishConnect();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
                    if (!result)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
                        System.err.println("Not finished");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
                    Thread.sleep(50);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
                } catch (java.net.ConnectException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
                    Thread.sleep(100);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
                    connectFailures++;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
                    if (connectFailures > 30)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
                        throw new RuntimeException("Cannot finish connecting");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
            System.err.println("Finished connecting");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   143
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   144
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   145
        ByteBuffer bb = ByteBuffer.allocateDirect(1024);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   146
        if (sc.read(bb) < 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   147
            throw new RuntimeException("Failed to read from server");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   148
        if (bb.get(0) != 42)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   149
            throw new RuntimeException("Read wrong byte from server");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   150
        System.err.println("Read from server");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   151
        sc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   152
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   153
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   154
    public static void main(String[] args) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   155
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   156
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   157
        
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   158
        Server server = new Server();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   159
        server.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   160
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   161
            client(server.port());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   162
        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   163
            server.interrupt();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   164
            server.finish(2000);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   165
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   166
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   167
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   168
}