blob: ee1e045042fa64aec05b5bc48d86b8aaeb0871de [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohairf5857212010-12-28 15:53:50 -08002 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
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 {
chegardf4deb42010-07-21 13:29:26 +010036 ServerSocket ss = new ServerSocket(0);
37 Socket clientSocket = new Socket();
duke6e45e102007-12-01 00:00:00 +000038
chegardf4deb42010-07-21 13:29:26 +010039 try {
40 // Start the server thread
41 Thread s1 = new Thread(new ServerThread(ss));
42 s1.start();
duke6e45e102007-12-01 00:00:00 +000043
chegardf4deb42010-07-21 13:29:26 +010044 // Start the client thread
45 ClientThread ct = new ClientThread(clientSocket, ss.getLocalPort());
46 Thread c1 = new Thread(ct);
47 c1.start();
duke6e45e102007-12-01 00:00:00 +000048
chegardf4deb42010-07-21 13:29:26 +010049 // Wait for the client thread to finish
50 c1.join(20000);
duke6e45e102007-12-01 00:00:00 +000051
chegardf4deb42010-07-21 13:29:26 +010052 // 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 throw new Exception("Takes too long. Dead lock");
58 }
59 } finally {
60 ss.close();
61 clientSocket.close();
duke6e45e102007-12-01 00:00:00 +000062 }
63 }
64}
65
66class 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
chegardf4deb42010-07-21 13:29:26 +010077 public ServerThread(ServerSocket serverSocket) throws Exception {
78 this.server = serverSocket;
duke6e45e102007-12-01 00:00:00 +000079 }
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) {
duke6e45e102007-12-01 00:00:00 +000091 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) {
chegardf4deb42010-07-21 13:29:26 +0100112 System.out.println(e);
duke6e45e102007-12-01 00:00:00 +0000113 // 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
140class ClientThread implements Runnable {
141
142 ObjectInputStream in;
143 ObjectOutputStream out;
144
145 Socket sock;
146
chegardf4deb42010-07-21 13:29:26 +0100147 public ClientThread(Socket sock, int serverPort) throws Exception {
duke6e45e102007-12-01 00:00:00 +0000148 try {
chegardf4deb42010-07-21 13:29:26 +0100149 System.out.println("About to connect the client socket");
150 this.sock = sock;
151 this.sock.connect(new InetSocketAddress("localhost", serverPort));
duke6e45e102007-12-01 00:00:00 +0000152 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
duke6e45e102007-12-01 00:00:00 +0000163 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
chegardf4deb42010-07-21 13:29:26 +0100216 System.out.println(ioe);
duke6e45e102007-12-01 00:00:00 +0000217 }
218 }
219}
220
221class 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}