1 /* 2 * Copyright (c) 2002, 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 Unit test for socket channels 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.util.*; 38 import jdk.net.Sockets; 39 40 public class Connect { 41 42 private static final long INCREMENTAL_DELAY = 30L * 1000L; 43 44 public static void main(String args[]) throws Exception { 45 if (!RsocketTest.isRsocketAvailable()) 46 return; 47 48 try (TestServers.EchoServer echoServer 49 = TestServers.EchoServer.startNewServer(1000)) { 50 test1(echoServer); 51 } 52 try { 53 test1(TestServers.RefusingServer.newRefusingServer()); 54 throw new Exception("Refused connection throws no exception"); 55 } catch (ConnectException ce) { 56 // Correct result 57 } 58 } 59 60 static void test1(TestServers.AbstractServer server) throws Exception { 61 Selector selector; 62 SocketChannel sc; 63 SelectionKey sk; 64 InetSocketAddress isa = new InetSocketAddress( 65 server.getAddress(), server.getPort()); 66 sc = Sockets.openRdmaSocketChannel(); 67 sc.configureBlocking(false); 68 69 selector = Sockets.openRdmaSelector(); 70 sk = sc.register(selector, SelectionKey.OP_CONNECT); 71 if (sc.connect(isa)) { 72 System.err.println("Connected immediately!"); 73 sc.close(); 74 selector.close(); 75 return; 76 } else { 77 ByteBuffer buf = ByteBuffer.allocateDirect(100); 78 buf.asCharBuffer().put(new String( 79 "The quick brown fox jumped over the lazy dog." 80 ).toCharArray()); 81 buf.flip(); 82 long startTime = System.currentTimeMillis(); 83 while(true) { 84 selector.select(INCREMENTAL_DELAY); 85 Set selectedKeys = selector.selectedKeys(); 86 87 if(selectedKeys.isEmpty()) { 88 System.err.println("Elapsed time without response: " + 89 (System.currentTimeMillis() - 90 startTime) / 1000L + " seconds."); 91 } 92 else if(!selectedKeys.contains(sk)) 93 { 94 System.err.println("Got wrong event about selection key."); 95 } else { 96 System.err.println("Got event for our selection key."); 97 if(sk.isConnectable()) { 98 if(sc.finishConnect()) { 99 if(sc.isConnected()) { 100 System.err.println("Successful connect."); 101 sk.interestOps(SelectionKey.OP_WRITE); 102 sc.write(buf); 103 } else { 104 System.err.println( 105 "Finish connect completed incorrectly."); 106 } 107 } else { 108 System.err.println( 109 "key incorrectly indicated socket channel connectable."); 110 } 111 } 112 if(sk.isWritable() && (buf.remaining() > 0)) { 113 sc.write(buf); 114 } 115 if(buf.remaining() == 0) { 116 System.err.println( 117 "SUCCESS! buffer contents were sent."); 118 sc.close(); 119 selector.close(); 120 return; 121 } 122 } 123 selectedKeys.clear(); 124 } 125 } 126 } 127 }