jdk/test/java/nio/channels/SocketChannel/AsyncCloseChannel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 702 3bd5e70b2fe9
permissions -rw-r--r--
Initial load
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 2006 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6285901
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Check no data is written to wrong socket channel during async closing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Xueming Shen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class AsyncCloseChannel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static boolean keepGoing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static String host = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static int sensorPort = 3010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static int targetPort = 3020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static int maxAcceptCount = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int acceptCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (System.getProperty("os.name").startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.err.println("WARNING: Still does not work on Windows!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Thread ss = new SensorServer(); ss.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Thread ts = new TargetServer(); ts.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Thread sc = new SensorClient(); sc.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Thread tc = new TargetClient(); tc.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        while(acceptCount < maxAcceptCount && !failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        keepGoing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ss.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ts.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            sc.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            tc.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new RuntimeException("AsyncCloseChannel2 failed after <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                       + acceptCount + "> times of accept!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static class SensorServer extends ThreadEx {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public void runEx() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            server = new ServerSocket(sensorPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            while(keepGoing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    final Socket s = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                int c = s.getInputStream().read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                if(c != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                    // No data is ever written to the peer's socket!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                    System.out.println("Oops: read a character: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                                       + (char) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                    failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                closeIt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    //ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static class TargetServer extends ThreadEx {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public void runEx() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            server = new ServerSocket(targetPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            while (keepGoing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    final Socket s = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    acceptCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            boolean empty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                    int c = s.getInputStream().read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                    if(c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                        if(!empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                    empty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                closeIt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    //ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
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
    static class SensorClient extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        private static boolean wake;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        private static SensorClient theClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            while (keepGoing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                Socket s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        while(!wake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            } catch (InterruptedException ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    wake = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    s.connect(new InetSocketAddress(host, sensorPort));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    } catch (InterruptedException ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    System.out.println("Exception on sensor client " + ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if(s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        } catch(IOException ex) { ex.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public SensorClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            theClient = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public static void wakeMe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            synchronized(theClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                wake = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                theClient.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static class TargetClient extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        volatile boolean ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            while(keepGoing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    final SocketChannel s = SocketChannel.open(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        new InetSocketAddress(host, targetPort));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    s.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    s.socket().setSoLinger(false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            ByteBuffer b = ByteBuffer.allocate(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                    b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                    b.put((byte) 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                    b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                    s.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                    ready = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                if(!(ex instanceof ClosedChannelException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                    System.out.println("Exception in target client child "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                       + ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    while(!ready)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    SensorClient.wakeMe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    t.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                     System.out.println("Exception in target client parent "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                        + ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                } catch (InterruptedException ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    static abstract class ThreadEx extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                runEx();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        abstract void runEx() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static void closeIt(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if(s != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (IOException ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}