jdk/test/java/net/Socket/DeadlockTest.java
author chegar
Thu, 11 Mar 2010 17:50:30 +0000
changeset 5147 96642e83ad41
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy Reviewed-by: michaelm, chegar Contributed-by: damjan.jov@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4176738
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Make sure a deadlock situation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *     would not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class DeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String [] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        // Start the server thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        Thread s1 = new Thread(new ServerThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        s1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        // Sleep to make sure s1 has created a server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // Start the client thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        ClientThread ct = new ClientThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Thread c1 = new Thread(ct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        c1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // Wait for the client thread to finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        c1.join(40000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // If timeout, we assume there is a deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (c1.isAlive() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            // Close the socket to force the server thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // terminate too
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            s1.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            ct.getSock().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception("Takes too long. Dead lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
class ServerThread implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static boolean dbg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    ObjectInputStream  in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    ObjectOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Socket sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ServerThread() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void ping(int cnt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       Message.write(out, new PingMessage(cnt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private int cnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (Thread.currentThread().getName().startsWith("child") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                server = new ServerSocket(4711);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                sock  = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                new Thread(this, "child").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                out = new ObjectOutputStream(sock.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (dbg) System.out.println("*** ping0 ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                ping(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (dbg) System.out.println("*** ping1 ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                ping(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (dbg) System.out.println("*** ping2 ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                ping(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (dbg) System.out.println("*** ping3 ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                ping(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if (dbg) System.out.println("*** ping4 ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ping(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (dbg) System.out.println("*** end ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // If anything goes wrong, just quit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (Thread.currentThread().getName().startsWith("child")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                in  = new ObjectInputStream(sock.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    if (dbg) System.out.println("read " + cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    Message msg = (Message) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if (dbg) System.out.println("read done " + cnt++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    switch (msg.code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    case Message.PING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        if (true) System.out.println("ping recv'ed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    } break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // If anything goes wrong, just quit.       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
class ClientThread implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    ObjectInputStream  in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    ObjectOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    Socket sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public ClientThread() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("About to create a socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            sock = new Socket(InetAddress.getLocalHost().getHostName(), 4711);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.out.println("connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out = new ObjectOutputStream(sock.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
          System.out.println("client failed with: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
          e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
          throw new Exception("Unexpected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public Socket getSock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private int cnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
          in  = new ObjectInputStream(sock.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
          int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
          while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
              System.out.println("read " + cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
              Message msg = (Message) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
              System.out.println("read done " + cnt++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
              switch (msg.code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
              case Message.PING: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                  System.out.println("ping recv'ed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                  count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
              } break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
              if (count == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                  sock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }  catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // If anything went wrong, just quit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
class Message implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static final int UNKNOWN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static final int PING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected int code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public Message() { this.code = UNKNOWN; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public Message(int code) { this.code = code; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static int cnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static void write(ObjectOutput out, Message msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            System.out.println("write message " + cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            out.writeObject(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println("flush message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            System.out.println("write message done " + cnt++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // Ignore the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
class PingMessage extends Message implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
      public PingMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
          code = Message.PING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      public PingMessage(int cnt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
          code = Message.PING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
          this.cnt = cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
          data = new int[50000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      int cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
      int[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}