Path: blob/master/test/jdk/java/net/Socket/DeadlockTest.java
41149 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 417673826* @library /test/lib27* @summary Make sure a deadlock situation28* would not occur29* @run main DeadlockTest30* @run main/othervm -Djava.net.preferIPv4Stack=true DeadlockTest31*/3233import java.net.*;34import java.io.*;35import jdk.test.lib.net.IPSupport;3637public class DeadlockTest {38public static void main(String [] argv) throws Exception {39IPSupport.throwSkippedExceptionIfNonOperational();4041ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());42Socket clientSocket = new Socket();4344try {45// Start the server thread46Thread s1 = new Thread(new ServerThread(ss));47s1.start();4849// Start the client thread50ClientThread ct = new ClientThread(clientSocket, ss.getLocalPort());51Thread c1 = new Thread(ct);52c1.start();5354// Wait for the client thread to finish55c1.join(20000);5657// If timeout, we assume there is a deadlock58if (c1.isAlive() == true) {59// Close the socket to force the server thread60// terminate too61s1.stop();62throw new Exception("Takes too long. Dead lock");63}64} finally {65ss.close();66clientSocket.close();67}68}69}7071class ServerThread implements Runnable {7273private static boolean dbg = false;7475ObjectInputStream in;76ObjectOutputStream out;7778ServerSocket server;7980Socket sock;8182public ServerThread(ServerSocket serverSocket) throws Exception {83this.server = serverSocket;84}8586public void ping(int cnt) {87Message.write(out, new PingMessage(cnt));88}8990private int cnt = 1;9192public void run() {9394try {95if (Thread.currentThread().getName().startsWith("child") == false) {96sock = server.accept();9798new Thread(this, "child").start();99100out = new ObjectOutputStream(sock.getOutputStream());101out.flush();102103if (dbg) System.out.println("*** ping0 ***");104ping(0);105if (dbg) System.out.println("*** ping1 ***");106ping(1);107if (dbg) System.out.println("*** ping2 ***");108ping(2);109if (dbg) System.out.println("*** ping3 ***");110ping(3);111if (dbg) System.out.println("*** ping4 ***");112ping(4);113if (dbg) System.out.println("*** end ***");114}115116} catch (Throwable e) {117System.out.println(e);118// If anything goes wrong, just quit.119}120121if (Thread.currentThread().getName().startsWith("child")) {122try {123124in = new ObjectInputStream(sock.getInputStream());125126while (true) {127if (dbg) System.out.println("read " + cnt);128Message msg = (Message) in.readObject();129if (dbg) System.out.println("read done " + cnt++);130switch (msg.code) {131case Message.PING: {132if (true) System.out.println("ping recv'ed");133} break;134}135136}137138} catch (Throwable e) {139// If anything goes wrong, just quit. }140}141}142}143}144145class ClientThread implements Runnable {146147ObjectInputStream in;148ObjectOutputStream out;149150Socket sock;151152public ClientThread(Socket sock, int serverPort) throws Exception {153try {154System.out.println("About to connect the client socket");155this.sock = sock;156this.sock.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort));157System.out.println("connected");158159out = new ObjectOutputStream(sock.getOutputStream());160out.flush();161} catch (Throwable e) {162System.out.println("client failed with: " + e);163e.printStackTrace();164throw new Exception("Unexpected exception");165}166}167168private int cnt = 1;169170public void run() {171try {172in = new ObjectInputStream(sock.getInputStream());173174int count = 0;175176while (true) {177System.out.println("read " + cnt);178Message msg = (Message) in.readObject();179System.out.println("read done " + cnt++);180switch (msg.code) {181case Message.PING: {182System.out.println("ping recv'ed");183count++;184} break;185}186if (count == 5) {187sock.close();188break;189}190}191} catch (IOException ioe) {192} catch (Throwable e) {193// If anything went wrong, just quit194}195}196197}198199class Message implements java.io.Serializable {200201static final int UNKNOWN = 0;202static final int PING = 1;203204protected int code;205206public Message() { this.code = UNKNOWN; }207208public Message(int code) { this.code = code; }209210private static int cnt = 1;211212public static void write(ObjectOutput out, Message msg) {213try {214System.out.println("write message " + cnt);215out.writeObject(msg);216System.out.println("flush message");217out.flush();218System.out.println("write message done " + cnt++);219} catch (IOException ioe) {220// Ignore the exception221System.out.println(ioe);222}223}224}225226class PingMessage extends Message implements java.io.Serializable {227228public PingMessage() {229code = Message.PING;230}231232public PingMessage(int cnt)233{234code = Message.PING;235this.cnt = cnt;236237data = new int[50000];238}239240int cnt;241int[] data;242}243244245