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