jdk/test/java/nio/channels/SocketChannel/Hangup.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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 2002 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 4617165
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Ensure that socket hangups are handled correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build TestUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main Hangup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Hangup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static class Failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        extends RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Failure(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void doSelect(Selector sel, SelectionKey sk, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int n = sel.select();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (n != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new Failure("Select returned zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Set sks = sel.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (sks.size() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new Failure("Wrong size for selected-key set: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                              + sks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!sks.remove(sk))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new Failure("Key not in selected-key set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        log.println("S: Socket selected #" + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void dally() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void test(boolean writeFromClient, boolean readAfterClose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ServerSocketChannel ssc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        SocketChannel cl = null;        // client end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        SocketChannel sv = null;        // server end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        log.println("Test: writeFromClient = " + writeFromClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    + ", readAfterClose = " + readAfterClose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            int ns = 0;                 // Number of selection operations done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // Set up server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            SocketAddress sa = TestUtil.bindToRandomPort(ssc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            log.println("S: Listening on port "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        + ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // Connect client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            cl = SocketChannel.open(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            log.println("C: Connected via port "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        + cl.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // Accept client connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            sv = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            log.println("S: Client connection accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // Create selector and register server side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            sel = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            sv.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            SelectionKey sk = sv.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ByteBuffer stuff = ByteBuffer.allocate(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (writeFromClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                // Write from client, read from server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                stuff.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (cl.write(stuff) != stuff.capacity())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    throw new Failure("Incorrect number of bytes written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                log.println("C: Wrote stuff");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                dally();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                doSelect(sel, sk, ++ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                stuff.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (sv.read(stuff) != stuff.capacity())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    throw new Failure("Wrong number of bytes read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                log.println("S: Read stuff");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // Close client side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            cl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            log.println("C: Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            dally();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // Select again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            doSelect(sel, sk, ++ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (readAfterClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // Read from client after client has disconnected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                stuff.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (sv.read(stuff) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    throw new Failure("Wrong number of bytes read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                log.println("S: Read EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // Select a couple more times just to make sure we're doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // the right thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            doSelect(sel, sk, ++ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            doSelect(sel, sk, ++ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (ssc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (cl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                cl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (sv != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                sv.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                sel.close();
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 static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (boolean writeFromClient = false;; writeFromClient = true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (boolean readAfterClose = false;; readAfterClose = true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    test(writeFromClient, readAfterClose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch (Failure x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    x.printStackTrace(log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (readAfterClose)
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 (writeFromClient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new RuntimeException("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
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
}