blob: 373c681215d50d0f62bd1fecdb57e3ad361b7252 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4176738
27 * @summary Make sure a deadlock situation
28 * would not occur
29 */
30
31import java.net.*;
32import java.io.*;
33
34public class DeadlockTest {
35 public static void main(String [] argv) throws Exception {
36
37 // Start the server thread
38 Thread s1 = new Thread(new ServerThread());
39 s1.start();
40
41 // Sleep to make sure s1 has created a server socket
42 Thread.sleep(1000);
43
44 // Start the client thread
45 ClientThread ct = new ClientThread();
46 Thread c1 = new Thread(ct);
47 c1.start();
48
49 // Wait for the client thread to finish
50 c1.join(40000);
51
52 // If timeout, we assume there is a deadlock
53 if (c1.isAlive() == true) {
54 // Close the socket to force the server thread
55 // terminate too
56 s1.stop();
57 ct.getSock().close();
58 throw new Exception("Takes too long. Dead lock");
59 }
60 }
61}
62
63class ServerThread implements Runnable {
64
65 private static boolean dbg = false;
66
67 ObjectInputStream in;
68 ObjectOutputStream out;
69
70 ServerSocket server;
71
72 Socket sock;
73
74 public ServerThread() throws Exception {
75
76 }
77
78 public void ping(int cnt) {
79 Message.write(out, new PingMessage(cnt));
80 }
81
82 private int cnt = 1;
83
84 public void run() {
85
86 try {
87 if (Thread.currentThread().getName().startsWith("child") == false) {
88 server = new ServerSocket(4711);
89 sock = server.accept();
90
91 new Thread(this, "child").start();
92
93 out = new ObjectOutputStream(sock.getOutputStream());
94 out.flush();
95
96 if (dbg) System.out.println("*** ping0 ***");
97 ping(0);
98 if (dbg) System.out.println("*** ping1 ***");
99 ping(1);
100 if (dbg) System.out.println("*** ping2 ***");
101 ping(2);
102 if (dbg) System.out.println("*** ping3 ***");
103 ping(3);
104 if (dbg) System.out.println("*** ping4 ***");
105 ping(4);
106 if (dbg) System.out.println("*** end ***");
107 }
108
109 } catch (Throwable e) {
110 // If anything goes wrong, just quit.
111 }
112
113 if (Thread.currentThread().getName().startsWith("child")) {
114 try {
115
116 in = new ObjectInputStream(sock.getInputStream());
117
118 while (true) {
119 if (dbg) System.out.println("read " + cnt);
120 Message msg = (Message) in.readObject();
121 if (dbg) System.out.println("read done " + cnt++);
122 switch (msg.code) {
123 case Message.PING: {
124 if (true) System.out.println("ping recv'ed");
125 } break;
126 }
127
128 }
129
130 } catch (Throwable e) {
131 // If anything goes wrong, just quit. }
132 }
133 }
134 }
135}
136
137class ClientThread implements Runnable {
138
139 ObjectInputStream in;
140 ObjectOutputStream out;
141
142 Socket sock;
143
144 public ClientThread() throws Exception {
145 try {
146 System.out.println("About to create a socket");
147 sock = new Socket(InetAddress.getLocalHost().getHostName(), 4711);
148 System.out.println("connected");
149
150 out = new ObjectOutputStream(sock.getOutputStream());
151 out.flush();
152 } catch (Throwable e) {
153 System.out.println("client failed with: " + e);
154 e.printStackTrace();
155 throw new Exception("Unexpected exception");
156 }
157 }
158
159 public Socket getSock() {
160 return sock;
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
194class 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
216 }
217 }
218}
219
220class PingMessage extends Message implements java.io.Serializable {
221
222 public PingMessage() {
223 code = Message.PING;
224 }
225
226 public PingMessage(int cnt)
227 {
228 code = Message.PING;
229 this.cnt = cnt;
230
231 data = new int[50000];
232 }
233
234 int cnt;
235 int[] data;
236}