Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/ServerSocketChannel/AdaptServerSocket.java
38828 views
1
/*
2
* Copyright (c) 2001, 2013, 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
* @summary Unit test for server-socket-channel adaptors
26
*/
27
28
import java.io.*;
29
import java.net.*;
30
import java.nio.*;
31
import java.nio.channels.*;
32
import java.nio.charset.*;
33
34
35
public class AdaptServerSocket {
36
37
static java.io.PrintStream out = System.out;
38
static volatile boolean clientStarted = false;
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 (Socket so = new Socket()) {
48
out.println("client: " + so);
49
clientStarted = true;
50
if (dally > 0)
51
Thread.sleep(dally);
52
so.connect(new InetSocketAddress(port));
53
if (Thread.interrupted()) {
54
out.println("client interrupted");
55
return;
56
}
57
out.println("client: " + so);
58
int a = so.getInputStream().read();
59
out.println("client: read " + a);
60
a += 1;
61
so.getOutputStream().write(a);
62
out.println("client: wrote " + a);
63
} catch (Exception x) {
64
if (x instanceof InterruptedException)
65
return;
66
clientException = x;
67
x.printStackTrace();
68
}
69
}
70
};
71
t.setDaemon(true);
72
t.start();
73
client = t;
74
}
75
76
static void test(int clientDally, int timeout, boolean shouldTimeout)
77
throws Exception
78
{
79
clientStarted = false;
80
out.println();
81
82
try (ServerSocketChannel ssc = ServerSocketChannel.open();
83
ServerSocket sso = ssc.socket()) {
84
out.println("created: " + ssc);
85
out.println(" " + sso);
86
if (timeout != 0)
87
sso.setSoTimeout(timeout);
88
out.println("timeout: " + sso.getSoTimeout());
89
sso.bind(null);
90
out.println("bound: " + ssc);
91
out.println(" " + sso);
92
startClient(sso.getLocalPort(), clientDally);
93
while (!clientStarted) {
94
Thread.sleep(20);
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
}
116
}
117
client.interrupt();
118
client.join();
119
if (clientException != null)
120
throw clientException;
121
}
122
123
public static void main(String[] args) throws Exception {
124
test(0, 0, false);
125
test(50, 5000, false);
126
test(500, 50, true);
127
}
128
129
}
130
131