Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousServerSocketChannel/Basic.java
41154 views
1
/*
2
* Copyright (c) 2008, 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
/* @test
25
* @bug 4607272 6842687
26
* @summary Unit test for AsynchronousServerSocketChannel
27
* @modules jdk.net
28
* @run main/timeout=180 Basic
29
*/
30
31
import java.nio.channels.*;
32
import java.net.*;
33
import static java.net.StandardSocketOptions.*;
34
import java.io.IOException;
35
import java.util.List;
36
import java.util.Set;
37
import java.util.concurrent.ExecutionException;
38
import java.util.concurrent.Future;
39
import java.util.concurrent.atomic.AtomicReference;
40
import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;
41
import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;
42
import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;
43
44
public class Basic {
45
46
public static void main(String[] args) throws Exception {
47
testBind();
48
testAccept();
49
testSocketOptions();
50
}
51
52
static void testBind() throws Exception {
53
System.out.println("-- bind --");
54
55
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel.open();
56
if (ch.getLocalAddress() != null)
57
throw new RuntimeException("Local address should be 'null'");
58
ch.bind(new InetSocketAddress(0), 20);
59
60
// check local address after binding
61
InetSocketAddress local = (InetSocketAddress)ch.getLocalAddress();
62
if (local.getPort() == 0)
63
throw new RuntimeException("Unexpected port");
64
if (!local.getAddress().isAnyLocalAddress())
65
throw new RuntimeException("Not bound to a wildcard address");
66
67
// try to re-bind
68
try {
69
ch.bind(new InetSocketAddress(0));
70
throw new RuntimeException("AlreadyBoundException expected");
71
} catch (AlreadyBoundException x) {
72
}
73
ch.close();
74
75
// check ClosedChannelException
76
ch = AsynchronousServerSocketChannel.open();
77
ch.close();
78
try {
79
ch.bind(new InetSocketAddress(0));
80
throw new RuntimeException("ClosedChannelException expected");
81
} catch (ClosedChannelException x) {
82
}
83
}
84
85
static void testAccept() throws Exception {
86
System.out.println("-- accept --");
87
88
final AsynchronousServerSocketChannel listener =
89
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
90
91
InetAddress lh = InetAddress.getLocalHost();
92
int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
93
final InetSocketAddress isa = new InetSocketAddress(lh, port);
94
95
// establish a few loopback connections
96
for (int i=0; i<100; i++) {
97
SocketChannel sc = SocketChannel.open(isa);
98
AsynchronousSocketChannel ch = listener.accept().get();
99
sc.close();
100
ch.close();
101
}
102
103
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
104
105
// start accepting
106
listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
107
public void completed(AsynchronousSocketChannel ch, Void att) {
108
try {
109
ch.close();
110
} catch (IOException ignore) { }
111
}
112
public void failed(Throwable exc, Void att) {
113
exception.set(exc);
114
}
115
});
116
117
// check AcceptPendingException
118
try {
119
listener.accept();
120
throw new RuntimeException("AcceptPendingException expected");
121
} catch (AcceptPendingException x) {
122
}
123
124
// asynchronous close
125
listener.close();
126
while (exception.get() == null)
127
Thread.sleep(100);
128
if (!(exception.get() instanceof AsynchronousCloseException))
129
throw new RuntimeException("AsynchronousCloseException expected");
130
131
// once closed when a further attemt should throw ClosedChannelException
132
try {
133
listener.accept().get();
134
throw new RuntimeException("ExecutionException expected");
135
} catch (ExecutionException x) {
136
if (!(x.getCause() instanceof ClosedChannelException))
137
throw new RuntimeException("Cause of ClosedChannelException expected");
138
} catch (InterruptedException x) {
139
}
140
141
}
142
143
static void testSocketOptions() throws Exception {
144
System.out.println("-- socket options --");
145
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel.open();
146
try {
147
// check supported options
148
Set<SocketOption<?>> options = ch.supportedOptions();
149
boolean reuseport = options.contains(SO_REUSEPORT);
150
if (!options.contains(SO_REUSEADDR))
151
throw new RuntimeException("SO_REUSEADDR should be supported");
152
if (!options.contains(SO_REUSEPORT) && reuseport)
153
throw new RuntimeException("SO_REUSEPORT should be supported");
154
if (!options.contains(SO_RCVBUF))
155
throw new RuntimeException("SO_RCVBUF should be supported");
156
157
// allowed to change when not bound
158
ch.setOption(SO_RCVBUF, 256*1024); // can't check
159
int before = ch.getOption(SO_RCVBUF);
160
int after = ch.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
161
if (after < before)
162
throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
163
ch.setOption(SO_REUSEADDR, true);
164
checkOption(ch, SO_REUSEADDR, true);
165
ch.setOption(SO_REUSEADDR, false);
166
checkOption(ch, SO_REUSEADDR, false);
167
168
if (reuseport) {
169
ch.setOption(SO_REUSEPORT, true);
170
checkOption(ch, SO_REUSEPORT, true);
171
ch.setOption(SO_REUSEPORT, false);
172
checkOption(ch, SO_REUSEPORT, false);
173
}
174
List<? extends SocketOption> extOptions = List.of(TCP_KEEPCOUNT,
175
TCP_KEEPIDLE, TCP_KEEPINTERVAL);
176
if (options.containsAll(extOptions)) {
177
ch.setOption(TCP_KEEPIDLE, 1234);
178
checkOption(ch, TCP_KEEPIDLE, 1234);
179
ch.setOption(TCP_KEEPINTERVAL, 123);
180
checkOption(ch, TCP_KEEPINTERVAL, 123);
181
ch.setOption(TCP_KEEPCOUNT, 7);
182
checkOption(ch, TCP_KEEPCOUNT, 7);
183
}
184
} finally {
185
ch.close();
186
}
187
}
188
189
static void checkOption(AsynchronousServerSocketChannel ch,
190
SocketOption name, Object expectedValue)
191
throws IOException
192
{
193
Object value = ch.getOption(name);
194
if (!value.equals(expectedValue))
195
throw new RuntimeException("value not as expected");
196
}
197
}
198
199