J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
Huaming Li | 9fdfd0f | 2016-01-17 08:43:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* @test |
Huaming Li | 9fdfd0f | 2016-01-17 08:43:49 +0000 | [diff] [blame] | 25 | * @bug 4286936 8146213 |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 26 | * @summary Unit test for server-socket-channel adaptors |
| 27 | */ |
| 28 | |
| 29 | import java.io.*; |
| 30 | import java.net.*; |
| 31 | import java.nio.*; |
| 32 | import java.nio.channels.*; |
| 33 | import java.nio.charset.*; |
| 34 | |
| 35 | |
| 36 | public class AdaptServerSocket { |
| 37 | |
| 38 | static java.io.PrintStream out = System.out; |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 39 | static volatile boolean clientStarted = false; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 40 | static volatile Exception clientException = null; |
| 41 | static volatile Thread client = null; |
| 42 | |
| 43 | static void startClient(final int port, final int dally) |
| 44 | throws Exception |
| 45 | { |
| 46 | Thread t = new Thread() { |
| 47 | public void run() { |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 48 | try (Socket so = new Socket()) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 49 | out.println("client: " + so); |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 50 | clientStarted = true; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 51 | if (dally > 0) |
| 52 | Thread.sleep(dally); |
| 53 | so.connect(new InetSocketAddress(port)); |
| 54 | if (Thread.interrupted()) { |
| 55 | out.println("client interrupted"); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | out.println("client: " + so); |
| 59 | int a = so.getInputStream().read(); |
| 60 | out.println("client: read " + a); |
| 61 | a += 1; |
| 62 | so.getOutputStream().write(a); |
| 63 | out.println("client: wrote " + a); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 64 | } catch (Exception x) { |
| 65 | if (x instanceof InterruptedException) |
| 66 | return; |
| 67 | clientException = x; |
| 68 | x.printStackTrace(); |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | t.setDaemon(true); |
| 73 | t.start(); |
| 74 | client = t; |
| 75 | } |
| 76 | |
| 77 | static void test(int clientDally, int timeout, boolean shouldTimeout) |
| 78 | throws Exception |
| 79 | { |
Huaming Li | 9fdfd0f | 2016-01-17 08:43:49 +0000 | [diff] [blame] | 80 | boolean needClient = !shouldTimeout; |
| 81 | client = null; |
| 82 | clientException = null; |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 83 | clientStarted = false; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 84 | out.println(); |
| 85 | |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 86 | try (ServerSocketChannel ssc = ServerSocketChannel.open(); |
| 87 | ServerSocket sso = ssc.socket()) { |
| 88 | out.println("created: " + ssc); |
| 89 | out.println(" " + sso); |
| 90 | if (timeout != 0) |
| 91 | sso.setSoTimeout(timeout); |
| 92 | out.println("timeout: " + sso.getSoTimeout()); |
| 93 | sso.bind(null); |
| 94 | out.println("bound: " + ssc); |
| 95 | out.println(" " + sso); |
Huaming Li | 9fdfd0f | 2016-01-17 08:43:49 +0000 | [diff] [blame] | 96 | if (needClient) { |
| 97 | startClient(sso.getLocalPort(), clientDally); |
| 98 | while (!clientStarted) { |
| 99 | Thread.sleep(20); |
| 100 | } |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 101 | } |
| 102 | Socket so = null; |
| 103 | try { |
| 104 | so = sso.accept(); |
| 105 | } catch (SocketTimeoutException x) { |
| 106 | if (shouldTimeout) |
| 107 | out.println("Accept timed out, as expected"); |
| 108 | else |
| 109 | throw x; |
| 110 | } |
| 111 | if (shouldTimeout && (so != null)) |
| 112 | throw new Exception("Accept did not time out"); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 113 | |
Yiming Wang | ab37de5 | 2013-08-15 13:42:45 +0100 | [diff] [blame] | 114 | if (so != null) { |
| 115 | int a = 42; |
| 116 | so.getOutputStream().write(a); |
| 117 | int b = so.getInputStream().read(); |
| 118 | if (b != a + 1) |
| 119 | throw new Exception("Read incorrect data"); |
| 120 | out.println("server: read " + b); |
| 121 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 122 | } |
Huaming Li | 9fdfd0f | 2016-01-17 08:43:49 +0000 | [diff] [blame] | 123 | if (needClient) { |
| 124 | client.interrupt(); |
| 125 | client.join(); |
| 126 | if (clientException != null) |
| 127 | throw clientException; |
| 128 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | public static void main(String[] args) throws Exception { |
| 132 | test(0, 0, false); |
Tristan Yan | 7f9414b | 2015-07-21 14:15:59 -0400 | [diff] [blame] | 133 | test(50, 5000, false); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 134 | test(500, 50, true); |
| 135 | } |
| 136 | |
| 137 | } |