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

Print this page


   1 /*
   2  * Copyright (c) 2002, 2010, 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 ..
  28  */
  29 
  30 import java.net.*;
  31 import java.io.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.nio.channels.spi.SelectorProvider;
  35 import java.util.*;
  36 
  37 public class IsConnectable {
  38 
  39     static final int DAYTIME_PORT = 13;
  40     static final String DAYTIME_HOST = TestUtil.HOST;
  41 
  42     static void test() throws Exception {
  43         InetSocketAddress isa
  44             = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
  45                                     DAYTIME_PORT);
  46         SocketChannel sc = SocketChannel.open();
  47         sc.configureBlocking(false);
  48         sc.connect(isa);
  49 
  50         Selector selector = SelectorProvider.provider().openSelector();
  51         try {
  52             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
  53             int keysAdded = selector.select();
  54             if (keysAdded > 0) {
  55                 boolean result = sc.finishConnect();
  56                 if (result) {
  57                     keysAdded = selector.select(5000);
  58                     // 4750573: keysAdded should not be incremented when op is dropped
  59                     // from a key already in the selected key set
  60                     if (keysAdded > 0)
  61                         throw new Exception("Test failed: 4750573 detected");
  62                     Set<SelectionKey> sel = selector.selectedKeys();
  63                     Iterator<SelectionKey> i = sel.iterator();
  64                     SelectionKey sk = i.next();
  65                     // 4737146: isConnectable should be false while connected
  66                     if (sk.isConnectable())
  67                         throw new Exception("Test failed: 4737146 detected");
  68                 }
  69             } else {

  70                 throw new Exception("Select failed");




  71             }
  72         } finally {
  73             sc.close();
  74             selector.close();
  75         }
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         test();



  80     }
  81 
  82 }
   1 /*
   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.
   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 ..
  28  */
  29 
  30 import java.net.*;


  31 import java.nio.channels.*;
  32 import java.nio.channels.spi.SelectorProvider;
  33 import java.util.*;
  34 
  35 public class IsConnectable {
  36 
  37     static void test(TestUtil.DayTimeServer daytimeServer) throws Exception {
  38         InetSocketAddress isa = new InetSocketAddress(daytimeServer.getAddress(),
  39                 daytimeServer.getPort());




  40         SocketChannel sc = SocketChannel.open();
  41         sc.configureBlocking(false);
  42         final boolean immediatelyConnected = sc.connect(isa);
  43 
  44         Selector selector = SelectorProvider.provider().openSelector();
  45         try {
  46             SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
  47             int keysAdded = selector.select();
  48             if (keysAdded > 0) {
  49                 boolean result = sc.finishConnect();
  50                 if (result) {
  51                     keysAdded = selector.select(5000);
  52                     // 4750573: keysAdded should not be incremented when op is dropped
  53                     // from a key already in the selected key set
  54                     if (keysAdded > 0)
  55                         throw new Exception("Test failed: 4750573 detected");
  56                     Set<SelectionKey> sel = selector.selectedKeys();
  57                     Iterator<SelectionKey> i = sel.iterator();
  58                     SelectionKey sk = i.next();
  59                     // 4737146: isConnectable should be false while connected
  60                     if (sk.isConnectable())
  61                         throw new Exception("Test failed: 4737146 detected");
  62                 }
  63             } else {
  64                 if (!immediatelyConnected) {
  65                     throw new Exception("Select failed");
  66                 } else {
  67                     System.err.println("IsConnectable couldn't be tested for "
  68                             + System.getProperty("os.name"));
  69                 }
  70             }
  71         } finally {
  72             sc.close();
  73             selector.close();
  74         }
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         try (TestUtil.DayTimeServer daytimeServer =
  79                         TestUtil.DayTimeServer.startNewServer(100)) {
  80             test(daytimeServer);
  81         }
  82     }
  83 
  84 }