test/jdk/jdk/net/RdmaSockets/rsocket/Socket/BasicSocketTest.java
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @summary Test for basic functionality for RdmaSocket and RdmaServerSocket
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 main/othervm BasicSocketTest
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.InputStream;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.io.OutputStream;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.net.ServerSocket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.net.Socket;
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 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
public class BasicSocketTest implements Runnable {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
    static ServerSocket ss;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    static Socket s1, s2;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
    static InetAddress iaddr;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
    static int port = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    static ServerSocketChannel ssc;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
    static SocketChannel sc1, sc2;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
    static String message = "This is a message!";
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
    static int length = -1;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
    public static void main(String args[]) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
        iaddr = InetAddress.getLocalHost();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
            ss = RdmaSockets.openServerSocket(StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
            s1 = RdmaSockets.openSocket(StandardProtocolFamily.INET);         
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
            ss.bind(new InetSocketAddress(iaddr, port));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
            Thread t = new Thread(new BasicSocketTest());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
            t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
            s2 = ss.accept();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
            InputStream is = s2.getInputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
            length = message.length();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
            int num = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            byte[] buf = new byte[length];
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            while (num < length) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                int l = is.read(buf);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
                num += l;
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
            String result = new String(buf);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
            if(!result.equals(message))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
                throw new RuntimeException("Test Failed!");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
        } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
            e.printStackTrace();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
            throw new RuntimeException("Test Failed!");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
            ss.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            s1.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
            s2.close();
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
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
    public void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            s1.connect(new InetSocketAddress(iaddr, ss.getLocalPort()));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
            OutputStream os = s1.getOutputStream();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
            os.write(message.getBytes());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
        } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
            e.printStackTrace();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
            throw new RuntimeException("Test Failed!");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
}