author | mli |
Fri, 16 Mar 2018 18:15:31 +0800 | |
changeset 49255 | acdb8531cc8b |
parent 47216 | 71c04702a3d5 |
child 49702 | 09c01737ad27 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
25 |
* @bug 8156002 |
2 | 26 |
* @summary Unit test for socket-channel adaptors |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
27 |
* @library .. /test/lib |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
28 |
* @build jdk.test.lib.Utils TestServers |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
29 |
* @run main AdaptSocket |
2 | 30 |
*/ |
31 |
||
32 |
import java.io.*; |
|
33 |
import java.net.*; |
|
34 |
import java.nio.channels.*; |
|
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
35 |
import java.util.Arrays; |
2 | 36 |
|
37 |
||
38 |
public class AdaptSocket { |
|
39 |
||
40 |
static java.io.PrintStream out = System.out; |
|
41 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
42 |
static void test(TestServers.DayTimeServer dayTimeServer, |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
43 |
int timeout, |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
44 |
boolean shouldTimeout) |
2 | 45 |
throws Exception |
46 |
{ |
|
47 |
out.println(); |
|
48 |
||
49 |
InetSocketAddress isa |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
50 |
= new InetSocketAddress(dayTimeServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
51 |
dayTimeServer.getPort()); |
2 | 52 |
SocketChannel sc = SocketChannel.open(); |
53 |
Socket so = sc.socket(); |
|
54 |
out.println("opened: " + so); |
|
55 |
out.println(" " + sc); |
|
56 |
||
57 |
//out.println("opts: " + sc.options()); |
|
58 |
so.setTcpNoDelay(true); |
|
59 |
//so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT); |
|
60 |
so.setKeepAlive(true); |
|
61 |
so.setSoLinger(true, 42); |
|
62 |
so.setOOBInline(true); |
|
63 |
so.setReceiveBufferSize(512); |
|
64 |
so.setSendBufferSize(512); |
|
65 |
//out.println(" " + sc.options()); |
|
66 |
||
67 |
if (timeout == 0) |
|
68 |
so.connect(isa); |
|
69 |
else { |
|
70 |
try { |
|
71 |
so.connect(isa, timeout); |
|
72 |
} catch (SocketTimeoutException x) { |
|
73 |
if (shouldTimeout) { |
|
74 |
out.println("Connection timed out, as expected"); |
|
75 |
return; |
|
76 |
} else { |
|
77 |
throw x; |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
out.println("connected: " + so); |
|
82 |
out.println(" " + sc); |
|
83 |
byte[] bb = new byte[100]; |
|
84 |
int n = so.getInputStream().read(bb); |
|
85 |
String s = new String(bb, 0, n - 2, "US-ASCII"); |
|
86 |
out.println(isa + " says: \"" + s + "\""); |
|
87 |
so.shutdownInput(); |
|
88 |
out.println("ishut: " + sc); |
|
89 |
so.shutdownOutput(); |
|
90 |
out.println("oshut: " + sc); |
|
91 |
so.close(); |
|
92 |
out.println("closed: " + so); |
|
93 |
out.println(" " + sc); |
|
94 |
} |
|
95 |
||
96 |
static String dataString = "foo\r\n"; |
|
97 |
||
98 |
static void testRead(Socket so, boolean shouldTimeout) |
|
99 |
throws Exception |
|
100 |
{ |
|
101 |
String data = "foo\r\n"; |
|
102 |
so.getOutputStream().write(dataString.getBytes("US-ASCII")); |
|
103 |
InputStream is = so.getInputStream(); |
|
104 |
try { |
|
105 |
byte[] b = new byte[100]; |
|
106 |
int n = is.read(b); |
|
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
107 |
if (shouldTimeout) { |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
108 |
throw new Exception("Should time out, but not, data: " + Arrays.toString(b)); |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
109 |
} |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
110 |
if (n != 5) { |
2 | 111 |
throw new Exception("Incorrect number of bytes read: " + n); |
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
112 |
} |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
113 |
if (!dataString.equals(new String(b, 0, n, "US-ASCII"))) { |
2 | 114 |
throw new Exception("Incorrect data read: " + n); |
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
115 |
} |
2 | 116 |
} catch (SocketTimeoutException x) { |
117 |
if (shouldTimeout) { |
|
118 |
out.println("Read timed out, as expected"); |
|
119 |
return; |
|
120 |
} |
|
121 |
throw x; |
|
122 |
} |
|
123 |
} |
|
124 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
125 |
static void testRead(TestServers.EchoServer echoServer, |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
126 |
int timeout, |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
127 |
boolean shouldTimeout) |
2 | 128 |
throws Exception |
129 |
{ |
|
130 |
out.println(); |
|
131 |
||
132 |
InetSocketAddress isa |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
133 |
= new InetSocketAddress(echoServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
134 |
echoServer.getPort()); |
2 | 135 |
SocketChannel sc = SocketChannel.open(); |
136 |
sc.connect(isa); |
|
137 |
Socket so = sc.socket(); |
|
138 |
out.println("connected: " + so); |
|
139 |
out.println(" " + sc); |
|
140 |
||
141 |
if (timeout > 0) |
|
142 |
so.setSoTimeout(timeout); |
|
143 |
out.println("timeout: " + so.getSoTimeout()); |
|
144 |
||
145 |
testRead(so, shouldTimeout); |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
146 |
for (int i = 0; i < 4; i++) { |
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
147 |
out.println("loop: " + i); |
13028
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
7668
diff
changeset
|
148 |
testRead(so, shouldTimeout); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
149 |
} |
2 | 150 |
|
151 |
sc.close(); |
|
152 |
} |
|
153 |
||
154 |
public static void main(String[] args) throws Exception { |
|
155 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
156 |
try (TestServers.DayTimeServer dayTimeServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
157 |
= TestServers.DayTimeServer.startNewServer()) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
158 |
test(dayTimeServer, 0, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
159 |
test(dayTimeServer, 1000, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
160 |
} |
2 | 161 |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
162 |
try (TestServers.DayTimeServer lingerDayTimeServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
163 |
= TestServers.DayTimeServer.startNewServer(100)) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
164 |
// this test no longer really test the connection timeout |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
165 |
// since there is no way to prevent the server from eagerly |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
166 |
// accepting connection... |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
167 |
test(lingerDayTimeServer, 10, true); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
168 |
} |
2 | 169 |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
170 |
try (TestServers.EchoServer echoServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
171 |
= TestServers.EchoServer.startNewServer()) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
172 |
testRead(echoServer, 0, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
173 |
testRead(echoServer, 8000, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
174 |
} |
2 | 175 |
|
37876
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
176 |
try (TestServers.NoResponseServer noResponseServer |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
177 |
= TestServers.NoResponseServer.startNewServer()) { |
8c1ab93d657a
8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents:
14415
diff
changeset
|
178 |
testRead(noResponseServer, 10, true); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
179 |
} |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
14342
diff
changeset
|
180 |
} |
2 | 181 |
} |