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