test/java/nio/channels/SocketChannel/IsConnectable.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -26,28 +26,23 @@
  * @summary Test if isConnectable returns true after connected
  * @library ..
  */
 
 import java.net.*;
-import java.io.*;
-import java.nio.*;
 import java.nio.channels.*;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.*;
 
 public class IsConnectable {
 
-    static final int DAYTIME_PORT = 13;
-    static final String DAYTIME_HOST = TestUtil.HOST;
-
-    static void test() throws Exception {
+    static void test(TestClass.DayTimeServer daytimeServer) throws Exception {
         InetSocketAddress isa
-            = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
-                                    DAYTIME_PORT);
+            = new InetSocketAddress(daytimeServer.getAddress(),
+                                    daytimeServer.getPort());
         SocketChannel sc = SocketChannel.open();
         sc.configureBlocking(false);
-        sc.connect(isa);
+        final boolean immediatelyConnected = sc.connect(isa);
 
         Selector selector = SelectorProvider.provider().openSelector();
         try {
             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
             int keysAdded = selector.select();

@@ -65,18 +60,26 @@
                     // 4737146: isConnectable should be false while connected
                     if (sk.isConnectable())
                         throw new Exception("Test failed: 4737146 detected");
                 }
             } else {
+                if (!immediatelyConnected) {
                 throw new Exception("Select failed");
+                } else {
+                    System.out.println("IsConnectable couldn't be fully tested for "
+                            + System.getProperty("os.name"));
+                }
             }
         } finally {
             sc.close();
             selector.close();
         }
     }
 
     public static void main(String[] args) throws Exception {
-        test();
+        try (TestClass.DayTimeServer daytimeServer
+                = TestClass.DayTimeServer.startNewServer(100)) {
+            test(daytimeServer);
+        }
     }
 
 }