57115
|
1 |
/*
|
57160
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
57115
|
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 |
/* @test
|
|
25 |
* @bug 8195160
|
|
26 |
* @summary Unit test for socket channels
|
|
27 |
* @requires (os.family == "linux")
|
|
28 |
* @library .. /test/lib
|
|
29 |
* @build jdk.test.lib.Utils TestServers
|
|
30 |
* @build RsocketTest
|
|
31 |
* @run main/othervm Connect
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.net.ConnectException;
|
|
35 |
import java.net.InetSocketAddress;
|
|
36 |
import java.net.StandardProtocolFamily;
|
|
37 |
import java.nio.ByteBuffer;
|
|
38 |
import java.nio.channels.Selector;
|
|
39 |
import java.nio.channels.SelectionKey;
|
|
40 |
import java.nio.channels.SocketChannel;
|
|
41 |
import java.util.Set;
|
|
42 |
import jdk.net.RdmaSockets;
|
|
43 |
|
|
44 |
import jtreg.SkippedException;
|
|
45 |
|
|
46 |
public class Connect {
|
|
47 |
|
|
48 |
private static final long INCREMENTAL_DELAY = 30L * 1000L;
|
|
49 |
|
|
50 |
public static void main(String args[]) throws Exception {
|
|
51 |
if (!RsocketTest.isRsocketAvailable())
|
|
52 |
throw new SkippedException("rsocket is not available");
|
|
53 |
|
|
54 |
try (TestServers.EchoServer echoServer
|
|
55 |
= TestServers.EchoServer.startNewServer(1000)) {
|
|
56 |
test1(echoServer);
|
|
57 |
}
|
|
58 |
try {
|
|
59 |
test1(TestServers.RefusingServer.newRefusingServer());
|
|
60 |
throw new Exception("Refused connection throws no exception");
|
|
61 |
} catch (ConnectException ce) {
|
|
62 |
// Correct result
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
static void test1(TestServers.AbstractServer server) throws Exception {
|
|
67 |
Selector selector;
|
|
68 |
SocketChannel sc;
|
|
69 |
SelectionKey sk;
|
|
70 |
InetSocketAddress isa = new InetSocketAddress(
|
|
71 |
server.getAddress(), server.getPort());
|
|
72 |
sc = RdmaSockets.openSocketChannel(StandardProtocolFamily.INET);
|
|
73 |
sc.configureBlocking(false);
|
|
74 |
|
|
75 |
selector = RdmaSockets.openSelector();
|
|
76 |
sk = sc.register(selector, SelectionKey.OP_CONNECT);
|
|
77 |
if (sc.connect(isa)) {
|
|
78 |
System.err.println("Connected immediately!");
|
|
79 |
sc.close();
|
|
80 |
selector.close();
|
|
81 |
return;
|
|
82 |
} else {
|
|
83 |
ByteBuffer buf = ByteBuffer.allocateDirect(100);
|
|
84 |
buf.asCharBuffer().put(new String(
|
|
85 |
"The quick brown fox jumped over the lazy dog."
|
|
86 |
).toCharArray());
|
|
87 |
buf.flip();
|
|
88 |
long startTime = System.currentTimeMillis();
|
|
89 |
while(true) {
|
|
90 |
selector.select(INCREMENTAL_DELAY);
|
|
91 |
Set selectedKeys = selector.selectedKeys();
|
|
92 |
|
|
93 |
if(selectedKeys.isEmpty()) {
|
|
94 |
System.err.println("Elapsed time without response: " +
|
|
95 |
(System.currentTimeMillis() -
|
|
96 |
startTime) / 1000L + " seconds.");
|
|
97 |
}
|
|
98 |
else if(!selectedKeys.contains(sk))
|
|
99 |
{
|
|
100 |
System.err.println("Got wrong event about selection key.");
|
|
101 |
} else {
|
|
102 |
System.err.println("Got event for our selection key.");
|
|
103 |
if(sk.isConnectable()) {
|
|
104 |
if(sc.finishConnect()) {
|
|
105 |
if(sc.isConnected()) {
|
|
106 |
System.err.println("Successful connect.");
|
|
107 |
sk.interestOps(SelectionKey.OP_WRITE);
|
|
108 |
sc.write(buf);
|
|
109 |
} else {
|
|
110 |
System.err.println(
|
|
111 |
"Finish connect completed incorrectly.");
|
|
112 |
}
|
|
113 |
} else {
|
|
114 |
System.err.println(
|
|
115 |
"key incorrectly indicated socket channel connectable.");
|
|
116 |
}
|
|
117 |
}
|
|
118 |
if(sk.isWritable() && (buf.remaining() > 0)) {
|
|
119 |
sc.write(buf);
|
|
120 |
}
|
|
121 |
if(buf.remaining() == 0) {
|
|
122 |
System.err.println(
|
|
123 |
"SUCCESS! buffer contents were sent.");
|
|
124 |
sc.close();
|
|
125 |
selector.close();
|
|
126 |
return;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
selectedKeys.clear();
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|