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