Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/DeadlockTest.java
41149 views
1
/*
2
* Copyright (c) 1999, 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
/*
25
* @test
26
* @bug 4176738
27
* @library /test/lib
28
* @summary Make sure a deadlock situation
29
* would not occur
30
* @run main DeadlockTest
31
* @run main/othervm -Djava.net.preferIPv4Stack=true DeadlockTest
32
*/
33
34
import java.net.*;
35
import java.io.*;
36
import jdk.test.lib.net.IPSupport;
37
38
public class DeadlockTest {
39
public static void main(String [] argv) throws Exception {
40
IPSupport.throwSkippedExceptionIfNonOperational();
41
42
ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
43
Socket clientSocket = new Socket();
44
45
try {
46
// Start the server thread
47
Thread s1 = new Thread(new ServerThread(ss));
48
s1.start();
49
50
// Start the client thread
51
ClientThread ct = new ClientThread(clientSocket, ss.getLocalPort());
52
Thread c1 = new Thread(ct);
53
c1.start();
54
55
// Wait for the client thread to finish
56
c1.join(20000);
57
58
// If timeout, we assume there is a deadlock
59
if (c1.isAlive() == true) {
60
// Close the socket to force the server thread
61
// terminate too
62
s1.stop();
63
throw new Exception("Takes too long. Dead lock");
64
}
65
} finally {
66
ss.close();
67
clientSocket.close();
68
}
69
}
70
}
71
72
class ServerThread implements Runnable {
73
74
private static boolean dbg = false;
75
76
ObjectInputStream in;
77
ObjectOutputStream out;
78
79
ServerSocket server;
80
81
Socket sock;
82
83
public ServerThread(ServerSocket serverSocket) throws Exception {
84
this.server = serverSocket;
85
}
86
87
public void ping(int cnt) {
88
Message.write(out, new PingMessage(cnt));
89
}
90
91
private int cnt = 1;
92
93
public void run() {
94
95
try {
96
if (Thread.currentThread().getName().startsWith("child") == false) {
97
sock = server.accept();
98
99
new Thread(this, "child").start();
100
101
out = new ObjectOutputStream(sock.getOutputStream());
102
out.flush();
103
104
if (dbg) System.out.println("*** ping0 ***");
105
ping(0);
106
if (dbg) System.out.println("*** ping1 ***");
107
ping(1);
108
if (dbg) System.out.println("*** ping2 ***");
109
ping(2);
110
if (dbg) System.out.println("*** ping3 ***");
111
ping(3);
112
if (dbg) System.out.println("*** ping4 ***");
113
ping(4);
114
if (dbg) System.out.println("*** end ***");
115
}
116
117
} catch (Throwable e) {
118
System.out.println(e);
119
// If anything goes wrong, just quit.
120
}
121
122
if (Thread.currentThread().getName().startsWith("child")) {
123
try {
124
125
in = new ObjectInputStream(sock.getInputStream());
126
127
while (true) {
128
if (dbg) System.out.println("read " + cnt);
129
Message msg = (Message) in.readObject();
130
if (dbg) System.out.println("read done " + cnt++);
131
switch (msg.code) {
132
case Message.PING: {
133
if (true) System.out.println("ping recv'ed");
134
} break;
135
}
136
137
}
138
139
} catch (Throwable e) {
140
// If anything goes wrong, just quit. }
141
}
142
}
143
}
144
}
145
146
class ClientThread implements Runnable {
147
148
ObjectInputStream in;
149
ObjectOutputStream out;
150
151
Socket sock;
152
153
public ClientThread(Socket sock, int serverPort) throws Exception {
154
try {
155
System.out.println("About to connect the client socket");
156
this.sock = sock;
157
this.sock.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort));
158
System.out.println("connected");
159
160
out = new ObjectOutputStream(sock.getOutputStream());
161
out.flush();
162
} catch (Throwable e) {
163
System.out.println("client failed with: " + e);
164
e.printStackTrace();
165
throw new Exception("Unexpected exception");
166
}
167
}
168
169
private int cnt = 1;
170
171
public void run() {
172
try {
173
in = new ObjectInputStream(sock.getInputStream());
174
175
int count = 0;
176
177
while (true) {
178
System.out.println("read " + cnt);
179
Message msg = (Message) in.readObject();
180
System.out.println("read done " + cnt++);
181
switch (msg.code) {
182
case Message.PING: {
183
System.out.println("ping recv'ed");
184
count++;
185
} break;
186
}
187
if (count == 5) {
188
sock.close();
189
break;
190
}
191
}
192
} catch (IOException ioe) {
193
} catch (Throwable e) {
194
// If anything went wrong, just quit
195
}
196
}
197
198
}
199
200
class Message implements java.io.Serializable {
201
202
static final int UNKNOWN = 0;
203
static final int PING = 1;
204
205
protected int code;
206
207
public Message() { this.code = UNKNOWN; }
208
209
public Message(int code) { this.code = code; }
210
211
private static int cnt = 1;
212
213
public static void write(ObjectOutput out, Message msg) {
214
try {
215
System.out.println("write message " + cnt);
216
out.writeObject(msg);
217
System.out.println("flush message");
218
out.flush();
219
System.out.println("write message done " + cnt++);
220
} catch (IOException ioe) {
221
// Ignore the exception
222
System.out.println(ioe);
223
}
224
}
225
}
226
227
class PingMessage extends Message implements java.io.Serializable {
228
229
public PingMessage() {
230
code = Message.PING;
231
}
232
233
public PingMessage(int cnt)
234
{
235
code = Message.PING;
236
this.cnt = cnt;
237
238
data = new int[50000];
239
}
240
241
int cnt;
242
int[] data;
243
}
244
245