jdk/test/java/nio/channels/SocketChannel/Shutdown.java
changeset 3486 0e14ae32369e
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
3485:bc05765c365a 3486:0e14ae32369e
     1 /*
     1 /*
     2  * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2002-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    21  * have any questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4618960
    25  * @bug 4618960 4516760
    26  * @summary Test isInputShutdown
    26  * @summary Test shutdownXXX and isInputShutdown
    27  * @library ..
       
    28  */
    27  */
    29 
    28 
       
    29 import java.io.IOException;
    30 import java.net.*;
    30 import java.net.*;
    31 import java.nio.*;
    31 import java.nio.ByteBuffer;
    32 import java.nio.channels.*;
    32 import java.nio.channels.*;
    33 
    33 
    34 public class Shutdown {
    34 public class Shutdown {
    35 
    35 
    36     public static void main(String args[]) throws Exception {
    36     /**
    37         InetSocketAddress sa = new InetSocketAddress(
    37      * Accept a connection, and close it immediately causing a hard reset.
    38                                 InetAddress.getByName(TestUtil.HOST), 23);
    38      */
    39         SocketChannel sc = SocketChannel.open(sa);
    39     static void acceptAndReset(ServerSocketChannel ssc) throws IOException {
    40         boolean before = sc.socket().isInputShutdown();
    40         SocketChannel peer = ssc.accept();
    41         sc.socket().shutdownInput();
    41         try {
    42         boolean after = sc.socket().isInputShutdown();
    42             peer.setOption(StandardSocketOption.SO_LINGER, 0);
    43         sc.close();
    43             peer.configureBlocking(false);
    44         if (before || !after)
    44             peer.write(ByteBuffer.wrap(new byte[128*1024]));
    45             throw new Exception("Test failed");
    45         } finally {
       
    46             peer.close();
       
    47         }
       
    48     }
       
    49 
       
    50     public static void main(String[] args) throws Exception {
       
    51         ServerSocketChannel ssc = ServerSocketChannel.open()
       
    52             .bind(new InetSocketAddress(0));
       
    53         try {
       
    54             InetAddress lh = InetAddress.getLocalHost();
       
    55             int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
       
    56             SocketAddress remote = new InetSocketAddress(lh, port);
       
    57 
       
    58             // Test SocketChannel shutdownXXX
       
    59             SocketChannel sc;
       
    60             sc = SocketChannel.open(remote);
       
    61             try {
       
    62                 acceptAndReset(ssc);
       
    63                 sc.shutdownInput();
       
    64                 sc.shutdownOutput();
       
    65             } finally {
       
    66                 sc.close();
       
    67             }
       
    68 
       
    69             // Test Socket adapter shutdownXXX and isShutdownInput
       
    70             sc = SocketChannel.open(remote);
       
    71             try {
       
    72                 acceptAndReset(ssc);
       
    73                 boolean before = sc.socket().isInputShutdown();
       
    74                 sc.socket().shutdownInput();
       
    75                 boolean after = sc.socket().isInputShutdown();
       
    76                 if (before || !after)
       
    77                     throw new RuntimeException("Before and after test failed");
       
    78                 sc.socket().shutdownOutput();
       
    79             } finally {
       
    80                 sc.close();
       
    81             }
       
    82         } finally {
       
    83             ssc.close();
       
    84         }
    46     }
    85     }
    47 }
    86 }