1 /*
   2  * Copyright (c) 2001, 2002, 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 4513011
  26  * @summary Registering and cancelling same fd many times
  27  * @library ..
  28  */
  29 
  30 import java.io.*;
  31 import java.net.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.util.*;
  35 import java.nio.channels.spi.SelectorProvider;
  36 
  37 public class Alias {
  38 
  39     static int success = 0;
  40     static int LIMIT = 20; // Hangs after just 1 if problem is present
  41 
  42     public static void main(String[] args) throws Exception {
  43         test1();
  44     }
  45 
  46     public static void test1() throws Exception {
  47         Selector selector = SelectorProvider.provider().openSelector();
  48         InetAddress myAddress=InetAddress.getByName(TestUtil.HOST);
  49         InetSocketAddress isa = new InetSocketAddress(myAddress,13);
  50 
  51         for (int j=0; j<LIMIT; j++) {
  52             SocketChannel sc = SocketChannel.open();
  53             sc.configureBlocking(false);
  54             boolean result = sc.connect(isa);
  55             if (!result) {
  56                 SelectionKey key = sc.register(selector,
  57                                                SelectionKey.OP_CONNECT);
  58                 while (!result) {
  59                     int keysAdded = selector.select(100);
  60                     if (keysAdded > 0) {
  61                         Set readyKeys = selector.selectedKeys();
  62                         Iterator i = readyKeys.iterator();
  63                         while (i.hasNext()) {
  64                             SelectionKey sk = (SelectionKey)i.next();
  65                             SocketChannel nextReady =
  66                                 (SocketChannel)sk.channel();
  67                             result = nextReady.finishConnect();
  68                         }
  69                     }
  70                 }
  71                 key.cancel();
  72             }
  73             read(sc);
  74         }
  75         selector.close();
  76     }
  77 
  78     static void read(SocketChannel sc) throws Exception {
  79         ByteBuffer bb = ByteBuffer.allocateDirect(100);
  80         int n = 0;
  81         while (n == 0) // Note this is not a rigorous check for done reading
  82             n = sc.read(bb);
  83         //bb.position(bb.position() - 2);
  84         //bb.flip();
  85         //CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
  86         //System.out.println("Received: \"" + cb + "\"");
  87         sc.close();
  88         success++;
  89     }
  90 }