Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/ConnectState.java
38828 views
1
/*
2
* Copyright (c) 2001, 2012, 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
/* @test
25
* @summary Test socket-channel connection-state transitions
26
* @library ..
27
*/
28
29
import java.io.*;
30
import java.net.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.util.Arrays;
34
import java.util.Collection;
35
import java.util.Collections;
36
import java.util.HashSet;
37
38
39
public class ConnectState {
40
41
static PrintStream log = System.err;
42
43
static InetSocketAddress remote;
44
45
final static int ST_UNCONNECTED = 0;
46
final static int ST_PENDING = 1;
47
final static int ST_CONNECTED = 2;
48
final static int ST_CLOSED = 3;
49
final static int ST_PENDING_OR_CONNECTED = 4;
50
// NO exceptions expected
51
final static Collection<Class<?>> NONE = Collections.emptySet();
52
53
// make a set of expected exception.
54
static Collection<Class<?>> expectedExceptions(Class<?>... expected) {
55
final Collection<Class<?>> exceptions;
56
if (expected.length == 0) {
57
exceptions = NONE;
58
} else if (expected.length == 1) {
59
assert expected[0] != null;
60
exceptions = Collections.<Class<?>>singleton(expected[0]);
61
} else {
62
exceptions = new HashSet<>(Arrays.asList(expected));
63
}
64
return exceptions;
65
}
66
67
static abstract class Test {
68
69
abstract String go(SocketChannel sc) throws Exception;
70
71
static void check(boolean test, String desc) throws Exception {
72
if (!test)
73
throw new Exception("Incorrect state: " + desc);
74
}
75
76
static void check(SocketChannel sc, int state) throws Exception {
77
switch (state) {
78
case ST_UNCONNECTED:
79
check(!sc.isConnected(), "!isConnected");
80
check(!sc.isConnectionPending(), "!isConnectionPending");
81
check(sc.isOpen(), "isOpen");
82
break;
83
case ST_PENDING:
84
check(!sc.isConnected(), "!isConnected");
85
check(sc.isConnectionPending(), "isConnectionPending");
86
check(sc.isOpen(), "isOpen");
87
break;
88
case ST_CONNECTED:
89
check(sc.isConnected(), "isConnected");
90
check(!sc.isConnectionPending(), "!isConnectionPending");
91
check(sc.isOpen(), "isOpen");
92
break;
93
case ST_CLOSED:
94
check(sc.isConnected(), "isConnected");
95
check(!sc.isConnectionPending(), "!isConnectionPending");
96
check(sc.isOpen(), "isOpen");
97
break;
98
case ST_PENDING_OR_CONNECTED:
99
check(sc.isConnected() || sc.isConnectionPending(),
100
"isConnected || isConnectionPending");
101
check(sc.isOpen(), "isOpen");
102
break;
103
}
104
}
105
106
Test(String name, Class<?> exception, int state) throws Exception {
107
this(name, expectedExceptions(exception), state);
108
}
109
110
// On some architecture we may need to accept several exceptions.
111
// For instance on Solaris, when using a server colocated on the
112
// machine we cannot guarantee that we will get a
113
// ConnectionPendingException when connecting twice on the same
114
// non-blocking socket. We may instead get a an
115
// AlreadyConnectedException, which is also valid: it simply means
116
// that the first connection has been immediately accepted.
117
Test(String name, Collection<Class<?>> exceptions, int state)
118
throws Exception {
119
SocketChannel sc = SocketChannel.open();
120
String note;
121
try {
122
try {
123
note = go(sc);
124
} catch (Exception x) {
125
Class<?> expectedExceptionClass = null;
126
for (Class<?> exception : exceptions) {
127
if (exception.isInstance(x)) {
128
log.println(name + ": As expected: "
129
+ x);
130
expectedExceptionClass = exception;
131
check(sc, state);
132
break;
133
}
134
}
135
if (expectedExceptionClass == null
136
&& !exceptions.isEmpty()) {
137
// we had an exception, but it's not of the set of
138
// exceptions we expected.
139
throw new Exception(name
140
+ ": Incorrect exception",
141
x);
142
} else if (exceptions.isEmpty()) {
143
// we didn't expect any exception
144
throw new Exception(name
145
+ ": Unexpected exception",
146
x);
147
}
148
// if we reach here, we have our expected exception
149
assert expectedExceptionClass != null;
150
return;
151
}
152
if (!exceptions.isEmpty()) {
153
throw new Exception(name
154
+ ": Expected exception not thrown: "
155
+ exceptions.iterator().next());
156
}
157
check(sc, state);
158
log.println(name + ": Returned normally"
159
+ ((note != null) ? ": " + note : ""));
160
} finally {
161
if (sc.isOpen())
162
sc.close();
163
}
164
}
165
166
}
167
168
static void tests() throws Exception {
169
log.println(remote);
170
171
new Test("Read unconnected", NotYetConnectedException.class,
172
ST_UNCONNECTED) {
173
@Override
174
String go(SocketChannel sc) throws Exception {
175
ByteBuffer b = ByteBuffer.allocateDirect(1024);
176
sc.read(b);
177
return null;
178
}};
179
180
new Test("Write unconnected", NotYetConnectedException.class,
181
ST_UNCONNECTED) {
182
@Override
183
String go(SocketChannel sc) throws Exception {
184
ByteBuffer b = ByteBuffer.allocateDirect(1024);
185
sc.write(b);
186
return null;
187
}};
188
189
new Test("Simple connect", NONE, ST_CONNECTED) {
190
@Override
191
String go(SocketChannel sc) throws Exception {
192
sc.connect(remote);
193
return null;
194
}};
195
196
new Test("Simple connect & finish", NONE, ST_CONNECTED) {
197
@Override
198
String go(SocketChannel sc) throws Exception {
199
sc.connect(remote);
200
if (!sc.finishConnect())
201
throw new Exception("finishConnect returned false");
202
return null;
203
}};
204
205
new Test("Double connect",
206
AlreadyConnectedException.class, ST_CONNECTED) {
207
@Override
208
String go(SocketChannel sc) throws Exception {
209
sc.connect(remote);
210
sc.connect(remote);
211
return null;
212
}};
213
214
new Test("Finish w/o start",
215
NoConnectionPendingException.class, ST_UNCONNECTED) {
216
@Override
217
String go(SocketChannel sc) throws Exception {
218
sc.finishConnect();
219
return null;
220
}};
221
222
// Note: using our local EchoServer rather than echo on a distant
223
// host - we see that Tries to finish = 0 (instead of ~ 18).
224
new Test("NB simple connect", NONE, ST_CONNECTED) {
225
@Override
226
String go(SocketChannel sc) throws Exception {
227
sc.configureBlocking(false);
228
sc.connect(remote);
229
int n = 0;
230
while (!sc.finishConnect()) {
231
Thread.sleep(10);
232
n++;
233
}
234
sc.finishConnect(); // Check redundant invocation
235
return ("Tries to finish = " + n);
236
}};
237
238
// Note: using our local EchoServer rather than echo on a distant
239
// host - we cannot guarantee that this test will get a
240
// a ConnectionPendingException: it may get an
241
// AlreadyConnectedException, so we should allow for both.
242
new Test("NB double connect",
243
expectedExceptions(ConnectionPendingException.class,
244
AlreadyConnectedException.class),
245
ST_PENDING_OR_CONNECTED) {
246
@Override
247
String go(SocketChannel sc) throws Exception {
248
sc.configureBlocking(false);
249
sc.connect(remote);
250
sc.connect(remote);
251
return null;
252
}};
253
254
new Test("NB finish w/o start",
255
NoConnectionPendingException.class, ST_UNCONNECTED) {
256
@Override
257
String go(SocketChannel sc) throws Exception {
258
sc.configureBlocking(false);
259
sc.finishConnect();
260
return null;
261
}};
262
263
new Test("NB connect, B finish", NONE, ST_CONNECTED) {
264
@Override
265
String go(SocketChannel sc) throws Exception {
266
sc.configureBlocking(false);
267
sc.connect(remote);
268
sc.configureBlocking(true);
269
sc.finishConnect();
270
return null;
271
}};
272
273
}
274
275
public static void main(String[] args) throws Exception {
276
try (TestServers.EchoServer echoServer
277
= TestServers.EchoServer.startNewServer(500)) {
278
remote = new InetSocketAddress(echoServer.getAddress(),
279
echoServer.getPort());
280
tests();
281
}
282
}
283
284
}
285
286