1 /*
   2  * Copyright (c) 2001, 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 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 -Djava.net.preferIPv4Stack=true Connect
  32  */
  33 
  34 import java.net.*;
  35 import java.nio.*;
  36 import java.nio.channels.*;
  37 import java.nio.channels.spi.SelectorProvider;
  38 import java.util.*;
  39 import jdk.net.Sockets;
  40 
  41 public class Connect implements Runnable {
  42 
  43     static int success = 0;
  44     static int LIMIT = 30;
  45     static SocketChannel sc;
  46     static InetSocketAddress isa;
  47     static boolean connected;
  48     
  49     public static void main(String[] args) throws Exception {
  50         if (!RsocketTest.isRsocketAvailable())
  51             return;
  52 
  53         try (TestServers.DayTimeServer daytimeServer
  54                 = TestServers.DayTimeServer.startNewServer(50)) {
  55             scaleTest(daytimeServer);
  56         }
  57     }
  58 
  59     static void scaleTest(TestServers.DayTimeServer daytimeServer)
  60         throws Exception
  61     {
  62         InetAddress myAddress = daytimeServer.getAddress();
  63         isa = new InetSocketAddress(myAddress, daytimeServer.getPort());
  64 
  65         for (int j=0; j<LIMIT; j++) {
  66             sc = Sockets.openRdmaSocketChannel();
  67             sc.configureBlocking(false);
  68 
  69             Thread t = new Thread (new Connect());
  70             t.start();
  71             t.join();
  72             if (!connected) {
  73                 Selector RSelector = Sockets.openRdmaSelector();
  74                 SelectionKey RKey = sc.register (RSelector, SelectionKey.OP_CONNECT);
  75                 while (!connected) {
  76                     int keysAdded = RSelector.select(100);
  77                     if (keysAdded > 0) {
  78                         Set<SelectionKey> readyKeys = RSelector.selectedKeys();
  79                         Iterator<SelectionKey> i = readyKeys.iterator();
  80                         while (i.hasNext()) {
  81                             SelectionKey sk = i.next();
  82                             SocketChannel nextReady = (SocketChannel)sk.channel();
  83                             connected = nextReady.finishConnect();
  84                         }
  85                         readyKeys.clear();
  86                     }
  87                 }
  88                 RSelector.close();
  89             }
  90             readAndClose(sc);
  91         }
  92     }
  93 
  94     static void readAndClose(SocketChannel sc) throws Exception {
  95         ByteBuffer bb = ByteBuffer.allocateDirect(100);
  96         int n = 0;
  97         while (n == 0) // Note this is not a rigorous check for done reading
  98             n = sc.read(bb);
  99         sc.close();
 100         success++;
 101         System.out.println("success count = " + success);
 102     }
 103 
 104     public void run() {
 105         try {
 106             connected = sc.connect(isa);
 107             System.out.println("connected = " + connected);
 108         } catch (Exception e) {
 109             e.printStackTrace();
 110             throw new RuntimeException("Test Failed!");
 111         }
 112     }
 113 }