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