test/jdk/java/nio/channels/SocketChannel/IsConnectable.java
branchJDK-8188051-branch
changeset 56398 9d3b0eb749a9
parent 56397 729f80d0cf31
parent 49544 d958597c7908
child 56413 f33dd5d6b480
equal deleted inserted replaced
56397:729f80d0cf31 56398:9d3b0eb749a9
     1 /*
       
     2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4737146 4750573
       
    26  * @summary Test if isConnectable returns true after connected
       
    27  * @library .. /test/lib
       
    28  * @build jdk.test.lib.Utils TestServers
       
    29  * @run main IsConnectable
       
    30  */
       
    31 
       
    32 import java.net.*;
       
    33 import java.nio.channels.*;
       
    34 import java.nio.channels.spi.SelectorProvider;
       
    35 import java.util.*;
       
    36 
       
    37 public class IsConnectable {
       
    38 
       
    39     static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
       
    40         InetSocketAddress isa
       
    41             = new InetSocketAddress(daytimeServer.getAddress(),
       
    42                                     daytimeServer.getPort());
       
    43         SocketChannel sc = SocketChannel.open();
       
    44         sc.configureBlocking(false);
       
    45         final boolean immediatelyConnected = sc.connect(isa);
       
    46 
       
    47         Selector selector = SelectorProvider.provider().openSelector();
       
    48         try {
       
    49             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
       
    50             int keysAdded = selector.select();
       
    51             if (keysAdded > 0) {
       
    52                 boolean result = sc.finishConnect();
       
    53                 if (result) {
       
    54                     keysAdded = selector.select(5000);
       
    55                     // 4750573: keysAdded should not be incremented when op is dropped
       
    56                     // from a key already in the selected key set
       
    57                     if (keysAdded > 0)
       
    58                         throw new Exception("Test failed: 4750573 detected");
       
    59                     Set<SelectionKey> sel = selector.selectedKeys();
       
    60                     Iterator<SelectionKey> i = sel.iterator();
       
    61                     SelectionKey sk = i.next();
       
    62                     // 4737146: isConnectable should be false while connected
       
    63                     if (sk.isConnectable())
       
    64                         throw new Exception("Test failed: 4737146 detected");
       
    65                 }
       
    66             } else {
       
    67                 if (!immediatelyConnected) {
       
    68                     throw new Exception("Select failed");
       
    69                 } else {
       
    70                     System.out.println("IsConnectable couldn't be fully tested for "
       
    71                             + System.getProperty("os.name"));
       
    72                 }
       
    73             }
       
    74         } finally {
       
    75             sc.close();
       
    76             selector.close();
       
    77         }
       
    78     }
       
    79 
       
    80     public static void main(String[] args) throws Exception {
       
    81         try (TestServers.DayTimeServer daytimeServer
       
    82                 = TestServers.DayTimeServer.startNewServer(100)) {
       
    83             test(daytimeServer);
       
    84         }
       
    85     }
       
    86 
       
    87 }