author | mli |
Thu, 19 Jul 2018 16:22:19 +0800 | |
changeset 51117 | c96c7d08ae49 |
parent 47216 | 71c04702a3d5 |
child 59305 | 219ec11136d9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51117
c96c7d08ae49
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 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 |
/* |
|
25 |
* |
|
26 |
* |
|
27 |
* Tests that the channel returned by System.inheritedChannel() |
|
28 |
* is in blocking mode, bound, and in the case of a SocketChannel |
|
29 |
* connected to a peer. |
|
30 |
* |
|
31 |
* The test works by launching a test service (called StateTestService) so |
|
32 |
* that it inherits each type of channel. The test service checks the |
|
33 |
* socket state and replies back to this class via an out-of-band |
|
34 |
* channel. |
|
35 |
*/ |
|
45578
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
36 |
import java.io.IOException; |
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
37 |
import java.net.InetSocketAddress; |
2 | 38 |
import java.nio.ByteBuffer; |
45578
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
39 |
import java.nio.channels.DatagramChannel; |
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
40 |
import java.nio.channels.SelectionKey; |
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
41 |
import java.nio.channels.Selector; |
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
42 |
import java.nio.channels.ServerSocketChannel; |
cd8f28842351
8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents:
5506
diff
changeset
|
43 |
import java.nio.channels.SocketChannel; |
2 | 44 |
|
51117
c96c7d08ae49
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents:
47216
diff
changeset
|
45 |
import jdk.test.lib.Utils; |
c96c7d08ae49
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents:
47216
diff
changeset
|
46 |
|
2 | 47 |
public class StateTest { |
48 |
||
49 |
private static int failures = 0; |
|
50 |
||
51 |
private static String TEST_SERVICE = "StateTestService"; |
|
52 |
||
53 |
/* |
|
54 |
* Reads the test result from the "out-of-band" connection to the test service. |
|
55 |
* |
|
56 |
* The out-of-band connection is just a TCP connection from the service to |
|
57 |
* this class. waitForTestResult just waits (with timeout) for the service |
|
58 |
* to connect. Once connected it waits (with timeout) for the test result. |
|
59 |
* The test result is examined. |
|
60 |
*/ |
|
61 |
private static void waitForTestResult(ServerSocketChannel ssc, boolean expectFail) throws IOException { |
|
62 |
Selector sel = ssc.provider().openSelector(); |
|
63 |
SelectionKey sk; |
|
64 |
SocketChannel sc; |
|
65 |
||
66 |
/* |
|
67 |
* Wait for service to connect |
|
68 |
*/ |
|
69 |
ssc.configureBlocking(false); |
|
70 |
sk = ssc.register(sel, SelectionKey.OP_ACCEPT); |
|
51117
c96c7d08ae49
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents:
47216
diff
changeset
|
71 |
long to = Utils.adjustTimeout(15*1000); |
2 | 72 |
sc = null; |
73 |
for (;;) { |
|
74 |
long st = System.currentTimeMillis(); |
|
75 |
sel.select(to); |
|
76 |
if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) { |
|
77 |
// connection established |
|
78 |
break; |
|
79 |
} |
|
80 |
sel.selectedKeys().remove(sk); |
|
81 |
to -= System.currentTimeMillis() - st; |
|
82 |
if (to <= 0) { |
|
83 |
throw new IOException("Timed out waiting for service to report test result"); |
|
84 |
} |
|
85 |
} |
|
86 |
sk.cancel(); |
|
87 |
ssc.configureBlocking(false); |
|
88 |
||
89 |
/* |
|
90 |
* Wait for service to report test result |
|
91 |
*/ |
|
92 |
sc.configureBlocking(false); |
|
93 |
sk = sc.register(sel, SelectionKey.OP_READ); |
|
51117
c96c7d08ae49
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents:
47216
diff
changeset
|
94 |
to = Utils.adjustTimeout(5000); |
2 | 95 |
ByteBuffer bb = ByteBuffer.allocateDirect(20); |
96 |
for (;;) { |
|
97 |
long st = System.currentTimeMillis(); |
|
98 |
sel.select(to); |
|
99 |
if (sk.isReadable()) { |
|
100 |
int n = sc.read(bb); |
|
101 |
if (n > 0) { |
|
102 |
break; |
|
103 |
} |
|
104 |
if (n < 0) { |
|
105 |
throw new IOException("Premature EOF - no test result from service"); |
|
106 |
} |
|
107 |
} |
|
108 |
sel.selectedKeys().remove(sk); |
|
109 |
to -= System.currentTimeMillis() - st; |
|
110 |
if (to <= 0) { |
|
111 |
throw new IOException("Timed out waiting for service to report test result"); |
|
112 |
} |
|
113 |
} |
|
114 |
sk.cancel(); |
|
115 |
sc.close(); |
|
116 |
sel.close(); |
|
117 |
||
118 |
/* |
|
119 |
* Examine the test result |
|
120 |
*/ |
|
121 |
bb.flip(); |
|
122 |
byte b = bb.get(); |
|
123 |
||
124 |
if (expectFail && b == 'P') { |
|
125 |
System.err.println("Test passed - test is expected to fail!!!"); |
|
126 |
failures++; |
|
127 |
} |
|
128 |
if (!expectFail && b != 'P') { |
|
129 |
System.err.println("Test failed!"); |
|
130 |
failures++; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
public static void main(String args[]) throws IOException { |
|
135 |
boolean expectFail = false; |
|
136 |
||
137 |
/* |
|
138 |
* [-expectFail] [options...] |
|
139 |
*/ |
|
140 |
String options[] = args; |
|
141 |
if (args.length > 0 && args[0].equals("-expectFail")) { |
|
142 |
// shift out first arg to create options |
|
143 |
expectFail = true; |
|
144 |
options = new String[args.length-1]; |
|
145 |
if (args.length > 1) { |
|
146 |
System.arraycopy(args, 1, options, 0, args.length-1); |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
/* |
|
151 |
* Create the listener which will be used to read the test result |
|
152 |
* from the service. |
|
153 |
*/ |
|
154 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
155 |
ssc.socket().bind(new InetSocketAddress(0)); |
|
156 |
||
157 |
/* |
|
158 |
* The port is passed to the service as an argument. |
|
159 |
*/ |
|
160 |
int port = ssc.socket().getLocalPort(); |
|
161 |
String arg[] = new String[1]; |
|
162 |
arg[0] = String.valueOf(port); |
|
163 |
||
164 |
/* |
|
165 |
* Launch service with a SocketChannel (tcp nowait) |
|
166 |
*/ |
|
167 |
SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg); |
|
168 |
waitForTestResult(ssc, expectFail); |
|
169 |
sc.close(); |
|
170 |
||
171 |
/* |
|
172 |
* Launch service with a ServerSocketChannel (tcp wait) |
|
173 |
* launchWithServerSocketChannel establishes a connection to the service |
|
174 |
* and the returned SocketChannel is connected to the service. |
|
175 |
*/ |
|
176 |
sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg); |
|
177 |
waitForTestResult(ssc, expectFail); |
|
178 |
sc.close(); |
|
179 |
||
180 |
/* |
|
181 |
* Launch service with a DatagramChannel (udp wait) |
|
182 |
*/ |
|
183 |
DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg); |
|
184 |
waitForTestResult(ssc, expectFail); |
|
185 |
dc.close(); |
|
186 |
||
187 |
if (failures > 0) { |
|
188 |
throw new RuntimeException("Test failed - see log for details"); |
|
189 |
} else { |
|
190 |
System.out.println("All tests passed."); |
|
191 |
} |
|
192 |
} |
|
193 |
} |