blob: 9b1c7013900a5087b380bf379d2a406babf09c78 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @summary Unit test for server-socket-channel adaptors
26 */
27
28import java.io.*;
29import java.net.*;
30import java.nio.*;
31import java.nio.channels.*;
32import java.nio.charset.*;
33
34
35public class AdaptServerSocket {
36
37 static java.io.PrintStream out = System.out;
38
39 static volatile Exception clientException = null;
40 static volatile Thread client = null;
41
42 static void startClient(final int port, final int dally)
43 throws Exception
44 {
45 Thread t = new Thread() {
46 public void run() {
47 try {
48 Socket so = new Socket();
49 out.println("client: " + so);
50 if (dally > 0)
51 Thread.sleep(dally);
52 so.connect(new InetSocketAddress(port));
53 if (Thread.interrupted()) {
54 out.println("client interrupted");
55 so.close();
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);
64 so.close();
65 } catch (Exception x) {
66 if (x instanceof InterruptedException)
67 return;
68 clientException = x;
69 x.printStackTrace();
70 }
71 }
72 };
73 t.setDaemon(true);
74 t.start();
75 client = t;
76 }
77
78 static void test(int clientDally, int timeout, boolean shouldTimeout)
79 throws Exception
80 {
81 out.println();
82
83 ServerSocketChannel ssc = ServerSocketChannel.open();
84 ServerSocket sso = ssc.socket();
85 out.println("created: " + ssc);
86 out.println(" " + sso);
87 if (timeout != 0)
88 sso.setSoTimeout(timeout);
89 out.println("timeout: " + sso.getSoTimeout());
90 sso.bind(null);
91 out.println("bound: " + ssc);
92 out.println(" " + sso);
93 startClient(sso.getLocalPort(), clientDally);
94 Thread.sleep(10);
95
96 Socket so = null;
97 try {
98 so = sso.accept();
99 } catch (SocketTimeoutException x) {
100 if (shouldTimeout)
101 out.println("Accept timed out, as expected");
102 else
103 throw x;
104 }
105 if (shouldTimeout && (so != null))
106 throw new Exception("Accept did not time out");
107
108 if (so != null) {
109 int a = 42;
110 so.getOutputStream().write(a);
111 int b = so.getInputStream().read();
112 if (b != a + 1)
113 throw new Exception("Read incorrect data");
114 out.println("server: read " + b);
115 sso.close();
116 }
117
118 client.interrupt();
119 client.join();
120 if (clientException != null)
121 throw clientException;
122 }
123
124 public static void main(String[] args) throws Exception {
125 test(0, 0, false);
126 test(50, 500, false);
127 test(500, 50, true);
128 }
129
130}