Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/AsyncShutdown.java
41149 views
1
/*
2
* Copyright (c) 2019, 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
/*
25
* @test
26
* @requires (os.family == "linux" | os.family == "mac")
27
* @run testng AsyncShutdown
28
* @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
29
*/
30
31
import java.io.IOException;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.ServerSocket;
35
import java.net.Socket;
36
import java.net.SocketTimeoutException;
37
import java.util.concurrent.Executors;
38
import java.util.concurrent.ScheduledExecutorService;
39
import java.util.concurrent.TimeUnit;
40
41
import org.testng.annotations.Test;
42
import static org.testng.Assert.*;
43
44
@Test
45
public class AsyncShutdown {
46
47
public void testShutdownInput1() throws IOException {
48
withConnection((s1, s2) -> {
49
scheduleShutdownInput(s1, 2000);
50
int n = s1.getInputStream().read();
51
assertTrue(n == -1);
52
});
53
}
54
55
public void testShutdownInput2() throws IOException {
56
withConnection((s1, s2) -> {
57
scheduleShutdownInput(s1, 2000);
58
s1.setSoTimeout(30*1000);
59
int n = s1.getInputStream().read();
60
assertTrue(n == -1);
61
});
62
}
63
64
public void testShutdownOutput1() throws IOException {
65
withConnection((s1, s2) -> {
66
scheduleShutdownOutput(s1, 2000);
67
byte[] data = new byte[128*1024];
68
try {
69
while (true) {
70
s1.getOutputStream().write(data);
71
}
72
} catch (IOException expected) { }
73
});
74
}
75
76
public void testShutdownOutput2() throws IOException {
77
withConnection((s1, s2) -> {
78
s1.setSoTimeout(100);
79
try {
80
s1.getInputStream().read();
81
assertTrue(false);
82
} catch (SocketTimeoutException e) { }
83
84
scheduleShutdownOutput(s1, 2000);
85
byte[] data = new byte[128*1024];
86
try {
87
while (true) {
88
s1.getOutputStream().write(data);
89
}
90
} catch (IOException expected) { }
91
});
92
}
93
94
static void scheduleShutdownInput(Socket s, long delay) {
95
schedule(() -> {
96
try {
97
s.shutdownInput();
98
} catch (IOException ioe) { }
99
}, delay);
100
}
101
102
static void scheduleShutdownOutput(Socket s, long delay) {
103
schedule(() -> {
104
try {
105
s.shutdownOutput();
106
} catch (IOException ioe) { }
107
}, delay);
108
}
109
110
static void schedule(Runnable task, long delay) {
111
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
112
try {
113
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
114
} finally {
115
executor.shutdown();
116
}
117
}
118
119
interface ThrowingBiConsumer<T, U> {
120
void accept(T t, U u) throws IOException;
121
}
122
123
static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
124
throws IOException
125
{
126
Socket s1 = null;
127
Socket s2 = null;
128
try (ServerSocket ss = createBoundServer()) {
129
s1 = new Socket();
130
s1.connect(ss.getLocalSocketAddress());
131
s2 = ss.accept();
132
consumer.accept(s1, s2);
133
} finally {
134
if (s1 != null) s1.close();
135
if (s2 != null) s2.close();
136
}
137
}
138
139
static ServerSocket createBoundServer() throws IOException {
140
ServerSocket ss = new ServerSocket();
141
InetAddress loopback = InetAddress.getLoopbackAddress();
142
InetSocketAddress address = new InetSocketAddress(loopback, 0);
143
ss.bind(address);
144
return ss;
145
}
146
147
}
148
149