Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/etc/AdaptorCloseAndInterrupt.java
41153 views
1
/*
2
* Copyright (c) 2012, 2019, 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 7184932 8232673
26
* @summary Test asynchronous close and interrupt of timed socket adapter methods
27
* @key randomness intermittent
28
*/
29
30
import java.io.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.nio.channels.spi.AbstractSelectableChannel;
34
import java.net.*;
35
import java.util.concurrent.Callable;
36
import java.util.concurrent.Executors;
37
import java.util.concurrent.ScheduledExecutorService;
38
import java.util.concurrent.TimeUnit;
39
import java.util.concurrent.atomic.AtomicBoolean;
40
import java.util.Random;
41
42
43
public class AdaptorCloseAndInterrupt {
44
private static final ScheduledExecutorService pool =
45
Executors.newScheduledThreadPool(1);
46
final ServerSocketChannel listener;
47
final DatagramChannel peer;
48
final int port;
49
50
final AtomicBoolean isClosed = new AtomicBoolean();
51
final AtomicBoolean isInterrupted = new AtomicBoolean();
52
53
public AdaptorCloseAndInterrupt() {
54
listener = null;
55
peer = null;
56
port = -1;
57
}
58
59
public AdaptorCloseAndInterrupt(ServerSocketChannel listener) {
60
this.listener = listener;
61
this.port = listener.socket().getLocalPort();
62
this.peer = null;
63
}
64
65
public AdaptorCloseAndInterrupt(DatagramChannel listener) {
66
this.peer = listener;
67
this.port = peer.socket().getLocalPort();
68
this.listener = null;
69
}
70
71
public static void main(String args[]) throws Exception {
72
try {
73
try (ServerSocketChannel listener = ServerSocketChannel.open()) {
74
listener.socket().bind(null);
75
new AdaptorCloseAndInterrupt(listener).scReadAsyncClose();
76
new AdaptorCloseAndInterrupt(listener).scReadAsyncInterrupt();
77
}
78
79
try (DatagramChannel peer = DatagramChannel.open()) {
80
peer.socket().bind(null);
81
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose(0);
82
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose(30_000);
83
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt(0);
84
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt(30_000);
85
}
86
87
new AdaptorCloseAndInterrupt().ssAcceptAsyncClose();
88
new AdaptorCloseAndInterrupt().ssAcceptAsyncInterrupt();
89
} finally {
90
pool.shutdown();
91
}
92
System.out.println("Test Passed");
93
}
94
95
void scReadAsyncClose() throws IOException {
96
try {
97
SocketChannel sc = SocketChannel.open(new InetSocketAddress(
98
InetAddress.getLoopbackAddress(), port));
99
sc.socket().setSoTimeout(30*1000);
100
101
doAsyncClose(sc);
102
103
try {
104
sc.socket().getInputStream().read(new byte[100]);
105
System.err.format("close() was invoked: %s%n", isClosed.get());
106
throw new RuntimeException("read should not have completed");
107
} catch (ClosedChannelException expected) {}
108
109
if (!sc.socket().isClosed())
110
throw new RuntimeException("socket is not closed");
111
} finally {
112
// accept connection and close it.
113
listener.accept().close();
114
}
115
}
116
117
void scReadAsyncInterrupt() throws IOException {
118
try {
119
final SocketChannel sc = SocketChannel.open(new InetSocketAddress(
120
InetAddress.getLoopbackAddress(), port));
121
sc.socket().setSoTimeout(30*1000);
122
123
doAsyncInterrupt();
124
125
try {
126
sc.socket().getInputStream().read(new byte[100]);
127
throw new RuntimeException("read should not have completed");
128
} catch (ClosedByInterruptException expected) {
129
System.out.format("interrupt() was invoked: %s%n",
130
isInterrupted.get());
131
System.out.format("scReadAsyncInterrupt was interrupted: %s%n",
132
Thread.currentThread().interrupted());
133
}
134
135
if (!sc.socket().isClosed())
136
throw new RuntimeException("socket is not closed");
137
} finally {
138
// accept connection and close it.
139
listener.accept().close();
140
}
141
}
142
143
void dcReceiveAsyncClose(int timeout) throws IOException {
144
DatagramChannel dc = DatagramChannel.open();
145
dc.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
146
dc.socket().setSoTimeout(timeout);
147
148
doAsyncClose(dc);
149
150
try {
151
dc.socket().receive(new DatagramPacket(new byte[100], 100));
152
System.err.format("close() was invoked: %s%n", isClosed.get());
153
throw new RuntimeException("receive should not have completed");
154
} catch (SocketException expected) { }
155
156
if (!dc.socket().isClosed())
157
throw new RuntimeException("socket is not closed");
158
}
159
160
void dcReceiveAsyncInterrupt(int timeout) throws IOException {
161
DatagramChannel dc = DatagramChannel.open();
162
dc.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
163
dc.socket().setSoTimeout(timeout);
164
165
doAsyncInterrupt();
166
167
try {
168
dc.socket().receive(new DatagramPacket(new byte[100], 100));
169
throw new RuntimeException("receive should not have completed");
170
} catch (SocketException expected) {
171
System.out.format("interrupt() was invoked: %s%n",
172
isInterrupted.get());
173
System.out.format("dcReceiveAsyncInterrupt was interrupted: %s%n",
174
Thread.currentThread().interrupted());
175
} catch (SocketTimeoutException unexpected) {
176
System.err.format("Receive thread interrupt invoked: %s%n",
177
isInterrupted.get());
178
System.err.format("Receive thread was interrupted: %s%n",
179
Thread.currentThread().isInterrupted());
180
throw unexpected;
181
}
182
183
if (!dc.socket().isClosed())
184
throw new RuntimeException("socket is not closed");
185
}
186
187
void ssAcceptAsyncClose() throws IOException {
188
ServerSocketChannel ssc = ServerSocketChannel.open();
189
ssc.socket().bind(null);
190
ssc.socket().setSoTimeout(30*1000);
191
192
doAsyncClose(ssc);
193
194
try {
195
ssc.socket().accept();
196
System.err.format("close() was invoked: %s%n", isClosed.get());
197
throw new RuntimeException("accept should not have completed");
198
} catch (ClosedChannelException expected) {}
199
200
if (!ssc.socket().isClosed())
201
throw new RuntimeException("socket is not closed");
202
}
203
204
void ssAcceptAsyncInterrupt() throws IOException {
205
ServerSocketChannel ssc = ServerSocketChannel.open();
206
ssc.socket().bind(null);
207
ssc.socket().setSoTimeout(30*1000);
208
209
doAsyncInterrupt();
210
211
try {
212
ssc.socket().accept();
213
throw new RuntimeException("accept should not have completed");
214
} catch (ClosedByInterruptException expected) {
215
System.out.format("interrupt() was invoked: %s%n",
216
isInterrupted.get());
217
System.out.format("ssAcceptAsyncInterrupt was interrupted: %s%n",
218
Thread.currentThread().interrupted());
219
}
220
221
if (!ssc.socket().isClosed())
222
throw new RuntimeException("socket is not closed");
223
}
224
225
void doAsyncClose(final AbstractSelectableChannel sc) {
226
AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {
227
public Void call() throws Exception {
228
sc.close();
229
isClosed.set(true);
230
return null;
231
}
232
}, new Random().nextInt(1000), TimeUnit.MILLISECONDS);
233
}
234
235
void doAsyncInterrupt() {
236
final Thread current = Thread.currentThread();
237
AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {
238
public Void call() throws Exception {
239
current.interrupt();
240
isInterrupted.set(true);
241
return null;
242
}
243
}, new Random().nextInt(1000), TimeUnit.MILLISECONDS);
244
}
245
246
}
247
248