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 Unit test for server-socket-channel adaptors 27 * @requires (os.family == "linux") 28 * @library .. /test/lib 29 * @build RsocketTest 30 * @run main/othervm -Djava.net.preferIPv4Stack=true AdaptServerSocket 31 */ 32 33 import java.io.*; 34 import java.net.*; 35 import java.nio.*; 36 import java.nio.channels.*; 37 import java.nio.charset.*; 38 import jdk.net.Sockets; 39 40 public class AdaptServerSocket { 41 42 static java.io.PrintStream out = System.out; 43 static volatile boolean clientStarted = false; 44 static volatile Exception clientException = null; 45 static volatile Thread client = null; 46 47 static void startClient(final int port, final int dally) 48 throws Exception 49 { 50 Thread t = new Thread() { 51 public void run() { 52 try { 53 Socket so = Sockets.openRdmaSocket(); 54 out.println("client: " + so); 55 clientStarted = true; 56 if (dally > 0) 57 Thread.sleep(dally); 58 so.connect(new InetSocketAddress(port)); 59 if (Thread.interrupted()) { 60 out.println("client interrupted"); 61 return; 62 } 63 out.println("client: " + so); 64 int a = so.getInputStream().read(); 65 out.println("client: read " + a); 66 a += 1; 67 so.getOutputStream().write(a); 68 out.println("client: wrote " + a); 69 } catch (Exception x) { 70 if (x instanceof InterruptedException) 71 return; 72 clientException = x; 73 x.printStackTrace(); 74 } 75 } 76 }; 77 t.setDaemon(true); 78 t.start(); 79 client = t; 80 } 81 82 static void test(int clientDally, int timeout, boolean shouldTimeout) 83 throws Exception 84 { 85 boolean needClient = !shouldTimeout; 86 client = null; 87 clientException = null; 88 clientStarted = false; 89 out.println(); 90 91 try { 92 ServerSocketChannel ssc = Sockets.openRdmaServerSocketChannel(); 93 ServerSocket sso = ssc.socket(); 94 out.println("created: " + ssc); 95 out.println(" " + sso); 96 if (timeout != 0) 97 sso.setSoTimeout(timeout); 98 out.println("timeout: " + sso.getSoTimeout()); 99 sso.bind(null); 100 out.println("bound: " + ssc); 101 out.println(" " + sso); 102 if (needClient) { 103 startClient(sso.getLocalPort(), clientDally); 104 while (!clientStarted) { 105 Thread.sleep(20); 106 } 107 } 108 Socket so = null; 109 try { 110 so = sso.accept(); 111 } catch (SocketTimeoutException x) { 112 if (shouldTimeout) 113 out.println("Accept timed out, as expected"); 114 else 115 throw x; 116 } 117 if (shouldTimeout && (so != null)) 118 throw new Exception("Accept did not time out"); 119 120 if (so != null) { 121 int a = 42; 122 so.getOutputStream().write(a); 123 int b = so.getInputStream().read(); 124 if (b != a + 1) 125 throw new Exception("Read incorrect data"); 126 out.println("server: read " + b); 127 } 128 } catch (Exception e) { 129 e.printStackTrace(); 130 throw new Exception("Test Failed!"); 131 } 132 133 if (needClient) { 134 client.interrupt(); 135 client.join(); 136 if (clientException != null) 137 throw clientException; 138 } 139 } 140 141 public static void main(String[] args) throws Exception { 142 if (!RsocketTest.isRsocketAvailable()) 143 return; 144 145 test(0, 0, false); 146 test(50, 5000, false); 147 test(500, 50, true); 148 } 149 }