1 /*
   2  * Copyright (c) 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 8195160
  26  * @summary Test making lots of Selectors
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build jdk.test.lib.Utils TestServers
  30  * @build RsocketTest
  31  * @run main/othervm Connect
  32  */
  33 
  34 import java.net.InetAddress;
  35 import java.net.InetSocketAddress;
  36 import java.nio.ByteBuffer;
  37 import java.nio.channels.SelectionKey;
  38 import java.nio.channels.Selector;
  39 import java.nio.channels.SocketChannel;
  40 import java.util.Iterator;
  41 import java.util.Set;
  42 import jdk.net.RdmaSockets;
  43 import jtreg.SkippedException;
  44 import static java.net.StandardProtocolFamily.INET;
  45 
  46 public class Connect {
  47 
  48     static int success = 0;
  49     static final int LIMIT = 30;
  50 
  51     public static void main(String[] args) throws Exception {
  52         if (!RsocketTest.isRsocketAvailable())
  53             throw new SkippedException("rsocket is not available");
  54 
  55         try (TestServers.DayTimeServer daytimeServer
  56                 = TestServers.DayTimeServer.startNewServer(50)) {
  57             scaleTest(daytimeServer);
  58         }
  59     }
  60 
  61     static void scaleTest(TestServers.DayTimeServer daytimeServer)
  62         throws Exception
  63     {
  64         InetAddress myAddress = daytimeServer.getAddress();
  65         InetSocketAddress isa = new InetSocketAddress(myAddress, daytimeServer.getPort());
  66 
  67         for (int j=0; j<LIMIT; j++) {
  68             SocketChannel sc = RdmaSockets.openSocketChannel(INET);
  69             sc.configureBlocking(false);
  70             boolean connected = sc.connect(isa);
  71             System.out.println("connected = " + connected);
  72 
  73             if (!connected) {
  74                 Selector selector = RdmaSockets.openSelector();
  75                 sc.register(selector, SelectionKey.OP_CONNECT);
  76                 while (!connected) {
  77                     int keysAdded = selector.select(100);
  78                     if (keysAdded > 0) {
  79                         Set<SelectionKey> readyKeys = selector.selectedKeys();
  80                         Iterator<SelectionKey> i = readyKeys.iterator();
  81                         while (i.hasNext()) {
  82                             SelectionKey sk = i.next();
  83                             SocketChannel nextReady = (SocketChannel)sk.channel();
  84                             connected = nextReady.finishConnect();
  85                         }
  86                         readyKeys.clear();
  87                     }
  88                 }
  89                 selector.close();
  90             }
  91             readAndClose(sc);
  92         }
  93     }
  94 
  95     static void readAndClose(SocketChannel sc) throws Exception {
  96         ByteBuffer bb = ByteBuffer.allocateDirect(100);
  97         int n = 0;
  98         while (n == 0) // Note this is not a rigorous check for done reading
  99             n = sc.read(bb);
 100         sc.close();
 101         success++;
 102         System.out.println("success count = " + success);
 103     }
 104 }