Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/DeadlockTest.java
38812 views
/*1* Copyright (c) 1999, 2010, 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* @summary Make sure a deadlock situation27* would not occur28*/2930import java.net.*;31import java.io.*;3233public class DeadlockTest {34public static void main(String [] argv) throws Exception {35ServerSocket ss = new ServerSocket(0);36Socket clientSocket = new Socket();3738try {39// Start the server thread40Thread s1 = new Thread(new ServerThread(ss));41s1.start();4243// Start the client thread44ClientThread ct = new ClientThread(clientSocket, ss.getLocalPort());45Thread c1 = new Thread(ct);46c1.start();4748// Wait for the client thread to finish49c1.join(20000);5051// If timeout, we assume there is a deadlock52if (c1.isAlive() == true) {53// Close the socket to force the server thread54// terminate too55s1.stop();56throw new Exception("Takes too long. Dead lock");57}58} finally {59ss.close();60clientSocket.close();61}62}63}6465class ServerThread implements Runnable {6667private static boolean dbg = false;6869ObjectInputStream in;70ObjectOutputStream out;7172ServerSocket server;7374Socket sock;7576public ServerThread(ServerSocket serverSocket) throws Exception {77this.server = serverSocket;78}7980public void ping(int cnt) {81Message.write(out, new PingMessage(cnt));82}8384private int cnt = 1;8586public void run() {8788try {89if (Thread.currentThread().getName().startsWith("child") == false) {90sock = server.accept();9192new Thread(this, "child").start();9394out = new ObjectOutputStream(sock.getOutputStream());95out.flush();9697if (dbg) System.out.println("*** ping0 ***");98ping(0);99if (dbg) System.out.println("*** ping1 ***");100ping(1);101if (dbg) System.out.println("*** ping2 ***");102ping(2);103if (dbg) System.out.println("*** ping3 ***");104ping(3);105if (dbg) System.out.println("*** ping4 ***");106ping(4);107if (dbg) System.out.println("*** end ***");108}109110} catch (Throwable e) {111System.out.println(e);112// If anything goes wrong, just quit.113}114115if (Thread.currentThread().getName().startsWith("child")) {116try {117118in = new ObjectInputStream(sock.getInputStream());119120while (true) {121if (dbg) System.out.println("read " + cnt);122Message msg = (Message) in.readObject();123if (dbg) System.out.println("read done " + cnt++);124switch (msg.code) {125case Message.PING: {126if (true) System.out.println("ping recv'ed");127} break;128}129130}131132} catch (Throwable e) {133// If anything goes wrong, just quit. }134}135}136}137}138139class ClientThread implements Runnable {140141ObjectInputStream in;142ObjectOutputStream out;143144Socket sock;145146public ClientThread(Socket sock, int serverPort) throws Exception {147try {148System.out.println("About to connect the client socket");149this.sock = sock;150this.sock.connect(new InetSocketAddress("localhost", serverPort));151System.out.println("connected");152153out = new ObjectOutputStream(sock.getOutputStream());154out.flush();155} catch (Throwable e) {156System.out.println("client failed with: " + e);157e.printStackTrace();158throw new Exception("Unexpected exception");159}160}161162private int cnt = 1;163164public void run() {165try {166in = new ObjectInputStream(sock.getInputStream());167168int count = 0;169170while (true) {171System.out.println("read " + cnt);172Message msg = (Message) in.readObject();173System.out.println("read done " + cnt++);174switch (msg.code) {175case Message.PING: {176System.out.println("ping recv'ed");177count++;178} break;179}180if (count == 5) {181sock.close();182break;183}184}185} catch (IOException ioe) {186} catch (Throwable e) {187// If anything went wrong, just quit188}189}190191}192193class Message implements java.io.Serializable {194195static final int UNKNOWN = 0;196static final int PING = 1;197198protected int code;199200public Message() { this.code = UNKNOWN; }201202public Message(int code) { this.code = code; }203204private static int cnt = 1;205206public static void write(ObjectOutput out, Message msg) {207try {208System.out.println("write message " + cnt);209out.writeObject(msg);210System.out.println("flush message");211out.flush();212System.out.println("write message done " + cnt++);213} catch (IOException ioe) {214// Ignore the exception215System.out.println(ioe);216}217}218}219220class PingMessage extends Message implements java.io.Serializable {221222public PingMessage() {223code = Message.PING;224}225226public PingMessage(int cnt)227{228code = Message.PING;229this.cnt = cnt;230231data = new int[50000];232}233234int cnt;235int[] data;236}237238239