blob: 79391ecca997d7867bf8ee401cc3a457384cc403 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Huaming Li9fdfd0f2016-01-17 08:43:49 +00002 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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'Hairfe008ae2010-05-25 15:58:33 -070019 * 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. Duke319a3b92007-12-01 00:00:00 +000022 */
23
24/* @test
Huaming Li9fdfd0f2016-01-17 08:43:49 +000025 * @bug 4286936 8146213
J. Duke319a3b92007-12-01 00:00:00 +000026 * @summary Unit test for server-socket-channel adaptors
27 */
28
29import java.io.*;
30import java.net.*;
31import java.nio.*;
32import java.nio.channels.*;
33import java.nio.charset.*;
34
35
36public class AdaptServerSocket {
37
38 static java.io.PrintStream out = System.out;
Yiming Wangab37de52013-08-15 13:42:45 +010039 static volatile boolean clientStarted = false;
J. Duke319a3b92007-12-01 00:00:00 +000040 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 Wangab37de52013-08-15 13:42:45 +010048 try (Socket so = new Socket()) {
J. Duke319a3b92007-12-01 00:00:00 +000049 out.println("client: " + so);
Yiming Wangab37de52013-08-15 13:42:45 +010050 clientStarted = true;
J. Duke319a3b92007-12-01 00:00:00 +000051 if (dally > 0)
52 Thread.sleep(dally);
53 so.connect(new InetSocketAddress(port));
54 if (Thread.interrupted()) {
55 out.println("client interrupted");
J. Duke319a3b92007-12-01 00:00:00 +000056 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. Duke319a3b92007-12-01 00:00:00 +000064 } 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 Li9fdfd0f2016-01-17 08:43:49 +000080 boolean needClient = !shouldTimeout;
81 client = null;
82 clientException = null;
Yiming Wangab37de52013-08-15 13:42:45 +010083 clientStarted = false;
J. Duke319a3b92007-12-01 00:00:00 +000084 out.println();
85
Yiming Wangab37de52013-08-15 13:42:45 +010086 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 Li9fdfd0f2016-01-17 08:43:49 +000096 if (needClient) {
97 startClient(sso.getLocalPort(), clientDally);
98 while (!clientStarted) {
99 Thread.sleep(20);
100 }
Yiming Wangab37de52013-08-15 13:42:45 +0100101 }
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. Duke319a3b92007-12-01 00:00:00 +0000113
Yiming Wangab37de52013-08-15 13:42:45 +0100114 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. Duke319a3b92007-12-01 00:00:00 +0000122 }
Huaming Li9fdfd0f2016-01-17 08:43:49 +0000123 if (needClient) {
124 client.interrupt();
125 client.join();
126 if (clientException != null)
127 throw clientException;
128 }
J. Duke319a3b92007-12-01 00:00:00 +0000129 }
130
131 public static void main(String[] args) throws Exception {
132 test(0, 0, false);
Tristan Yan7f9414b2015-07-21 14:15:59 -0400133 test(50, 5000, false);
J. Duke319a3b92007-12-01 00:00:00 +0000134 test(500, 50, true);
135 }
136
137}