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/AsyncCloseChannel.java
38828 views
1
/*
2
* Copyright (c) 2006, 2008, 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 6285901 6501089
26
* @summary Check no data is written to wrong socket channel during async closing.
27
* @author Xueming Shen
28
*/
29
30
import java.io.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.net.*;
34
35
public class AsyncCloseChannel {
36
static volatile boolean failed = false;
37
static volatile boolean keepGoing = true;
38
static int maxAcceptCount = 100;
39
static volatile int acceptCount = 0;
40
static String host = "127.0.0.1";
41
static int sensorPort;
42
static int targetPort;
43
44
public static void main(String args[]) throws Exception {
45
if (System.getProperty("os.name").startsWith("Windows")) {
46
System.err.println("WARNING: Still does not work on Windows!");
47
return;
48
}
49
Thread ss = new SensorServer(); ss.start();
50
Thread ts = new TargetServer(); ts.start();
51
52
sensorPort = ((ServerThread)ss).server.getLocalPort();
53
targetPort = ((ServerThread)ts).server.getLocalPort();
54
55
Thread sc = new SensorClient(); sc.start();
56
Thread tc = new TargetClient(); tc.start();
57
58
while(acceptCount < maxAcceptCount && !failed) {
59
Thread.sleep(10);
60
}
61
keepGoing = false;
62
try {
63
ss.interrupt();
64
ts.interrupt();
65
sc.interrupt();
66
tc.interrupt();
67
} catch (Exception e) {}
68
if (failed)
69
throw new RuntimeException("AsyncCloseChannel2 failed after <"
70
+ acceptCount + "> times of accept!");
71
}
72
73
static class SensorServer extends ServerThread {
74
public void runEx() throws Exception {
75
while(keepGoing) {
76
try {
77
final Socket s = server.accept();
78
new Thread() {
79
public void run() {
80
try {
81
int c = s.getInputStream().read();
82
if(c != -1) {
83
// No data is ever written to the peer's socket!
84
System.err.println("Oops: read a character: "
85
+ (char) c);
86
failed = true;
87
}
88
} catch (IOException ex) {
89
ex.printStackTrace();
90
} finally {
91
closeIt(s);
92
}
93
}
94
}.start();
95
} catch (IOException ex) {
96
System.err.println("Exception on sensor server " + ex.getMessage());
97
}
98
}
99
}
100
}
101
102
static class TargetServer extends ServerThread {
103
public void runEx() throws Exception {
104
while (keepGoing) {
105
try {
106
final Socket s = server.accept();
107
acceptCount++;
108
new Thread() {
109
public void run() {
110
boolean empty = true;
111
try {
112
for(;;) {
113
int c = s.getInputStream().read();
114
if(c == -1) {
115
if(!empty)
116
break;
117
}
118
empty = false;
119
}
120
} catch (IOException ex) {
121
ex.printStackTrace();
122
} finally {
123
closeIt(s);
124
}
125
}
126
}.start();
127
} catch (IOException ex) {
128
System.err.println("Exception on target server " + ex.getMessage());
129
}
130
}
131
}
132
}
133
134
static class SensorClient extends Thread {
135
private static boolean wake;
136
private static SensorClient theClient;
137
public void run() {
138
while (keepGoing) {
139
Socket s = null;
140
try {
141
s = new Socket();
142
synchronized(this) {
143
while(!wake && keepGoing) {
144
try {
145
wait();
146
} catch (InterruptedException ex) { }
147
}
148
wake = false;
149
}
150
s.connect(new InetSocketAddress(host, sensorPort));
151
try {
152
Thread.sleep(10);
153
} catch (InterruptedException ex) { }
154
} catch (IOException ex) {
155
System.err.println("Exception on sensor client " + ex.getMessage());
156
} finally {
157
if(s != null) {
158
try {
159
s.close();
160
} catch(IOException ex) { ex.printStackTrace();}
161
}
162
}
163
}
164
}
165
166
public SensorClient() {
167
theClient = this;
168
}
169
170
public static void wakeMe() {
171
synchronized(theClient) {
172
wake = true;
173
theClient.notify();
174
}
175
}
176
}
177
178
static class TargetClient extends Thread {
179
volatile boolean ready = false;
180
public void run() {
181
while(keepGoing) {
182
try {
183
final SocketChannel s = SocketChannel.open(
184
new InetSocketAddress(host, targetPort));
185
s.finishConnect();
186
s.socket().setSoLinger(false, 0);
187
ready = false;
188
Thread t = new Thread() {
189
public void run() {
190
ByteBuffer b = ByteBuffer.allocate(1);
191
try {
192
for(;;) {
193
b.clear();
194
b.put((byte) 'A');
195
b.flip();
196
s.write(b);
197
ready = true;
198
}
199
} catch (IOException ex) {
200
if(!(ex instanceof ClosedChannelException))
201
System.err.println("Exception in target client child "
202
+ ex.toString());
203
}
204
}
205
};
206
t.start();
207
while(!ready && keepGoing) {
208
try {
209
Thread.sleep(10);
210
} catch (InterruptedException ex) {}
211
}
212
s.close();
213
SensorClient.wakeMe();
214
t.join();
215
} catch (IOException ex) {
216
System.err.println("Exception in target client parent "
217
+ ex.getMessage());
218
} catch (InterruptedException ex) {}
219
}
220
}
221
}
222
223
static abstract class ServerThread extends Thread {
224
ServerSocket server;
225
public ServerThread() {
226
super();
227
try {
228
server = new ServerSocket(0);
229
} catch (IOException ex) {
230
ex.printStackTrace();
231
}
232
}
233
234
public void interrupt() {
235
super.interrupt();
236
if (server != null) {
237
try {
238
server.close();
239
} catch (IOException ex) {
240
ex.printStackTrace();
241
}
242
}
243
}
244
public void run() {
245
try {
246
runEx();
247
} catch (Exception ex) {
248
ex.printStackTrace();
249
}
250
}
251
252
abstract void runEx() throws Exception;
253
}
254
255
public static void closeIt(Socket s) {
256
try {
257
if(s != null)
258
s.close();
259
} catch (IOException ex) { }
260
}
261
}
262
263