jdk/test/java/nio/channels/SocketChannel/IsConnectable.java
changeset 14415 7a31b0e0cfaf
parent 7668 d4a77089c587
equal deleted inserted replaced
14414:f338be3ef659 14415:7a31b0e0cfaf
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2012, Oracle and/or its affiliates. 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.
    26  * @summary Test if isConnectable returns true after connected
    26  * @summary Test if isConnectable returns true after connected
    27  * @library ..
    27  * @library ..
    28  */
    28  */
    29 
    29 
    30 import java.net.*;
    30 import java.net.*;
    31 import java.io.*;
       
    32 import java.nio.*;
       
    33 import java.nio.channels.*;
    31 import java.nio.channels.*;
    34 import java.nio.channels.spi.SelectorProvider;
    32 import java.nio.channels.spi.SelectorProvider;
    35 import java.util.*;
    33 import java.util.*;
    36 
    34 
    37 public class IsConnectable {
    35 public class IsConnectable {
    38 
    36 
    39     static final int DAYTIME_PORT = 13;
    37     static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
    40     static final String DAYTIME_HOST = TestUtil.HOST;
       
    41 
       
    42     static void test() throws Exception {
       
    43         InetSocketAddress isa
    38         InetSocketAddress isa
    44             = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
    39             = new InetSocketAddress(daytimeServer.getAddress(),
    45                                     DAYTIME_PORT);
    40                                     daytimeServer.getPort());
    46         SocketChannel sc = SocketChannel.open();
    41         SocketChannel sc = SocketChannel.open();
    47         sc.configureBlocking(false);
    42         sc.configureBlocking(false);
    48         sc.connect(isa);
    43         final boolean immediatelyConnected = sc.connect(isa);
    49 
    44 
    50         Selector selector = SelectorProvider.provider().openSelector();
    45         Selector selector = SelectorProvider.provider().openSelector();
    51         try {
    46         try {
    52             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
    47             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
    53             int keysAdded = selector.select();
    48             int keysAdded = selector.select();
    65                     // 4737146: isConnectable should be false while connected
    60                     // 4737146: isConnectable should be false while connected
    66                     if (sk.isConnectable())
    61                     if (sk.isConnectable())
    67                         throw new Exception("Test failed: 4737146 detected");
    62                         throw new Exception("Test failed: 4737146 detected");
    68                 }
    63                 }
    69             } else {
    64             } else {
    70                 throw new Exception("Select failed");
    65                 if (!immediatelyConnected) {
       
    66                     throw new Exception("Select failed");
       
    67                 } else {
       
    68                     System.out.println("IsConnectable couldn't be fully tested for "
       
    69                             + System.getProperty("os.name"));
       
    70                 }
    71             }
    71             }
    72         } finally {
    72         } finally {
    73             sc.close();
    73             sc.close();
    74             selector.close();
    74             selector.close();
    75         }
    75         }
    76     }
    76     }
    77 
    77 
    78     public static void main(String[] args) throws Exception {
    78     public static void main(String[] args) throws Exception {
    79         test();
    79         try (TestServers.DayTimeServer daytimeServer
       
    80                 = TestServers.DayTimeServer.startNewServer(100)) {
       
    81             test(daytimeServer);
       
    82         }
    80     }
    83     }
    81 
    84 
    82 }
    85 }