57226
|
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 |
* @run testng/othervm -Djdk.net.usePlainSocketImpl AsyncShutdown
|
|
29 |
* @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.net.ServerSocket;
|
|
34 |
import java.net.Socket;
|
|
35 |
import java.net.SocketTimeoutException;
|
|
36 |
import java.util.concurrent.Executors;
|
|
37 |
import java.util.concurrent.ScheduledExecutorService;
|
|
38 |
import java.util.concurrent.TimeUnit;
|
|
39 |
|
|
40 |
import org.testng.annotations.Test;
|
|
41 |
import static org.testng.Assert.*;
|
|
42 |
|
|
43 |
@Test
|
|
44 |
public class AsyncShutdown {
|
|
45 |
|
|
46 |
public void testShutdownInput1() throws IOException {
|
|
47 |
withConnection((s1, s2) -> {
|
|
48 |
scheduleShutdownInput(s1, 2000);
|
|
49 |
int n = s1.getInputStream().read();
|
|
50 |
assertTrue(n == -1);
|
|
51 |
});
|
|
52 |
}
|
|
53 |
|
|
54 |
public void testShutdownInput2() throws IOException {
|
|
55 |
withConnection((s1, s2) -> {
|
|
56 |
scheduleShutdownInput(s1, 2000);
|
|
57 |
s1.setSoTimeout(30*1000);
|
|
58 |
int n = s1.getInputStream().read();
|
|
59 |
assertTrue(n == -1);
|
|
60 |
});
|
|
61 |
}
|
|
62 |
|
|
63 |
public void testShutdownOutput1() throws IOException {
|
|
64 |
withConnection((s1, s2) -> {
|
|
65 |
scheduleShutdownOutput(s1, 2000);
|
|
66 |
byte[] data = new byte[128*1024];
|
|
67 |
try {
|
|
68 |
while (true) {
|
|
69 |
s1.getOutputStream().write(data);
|
|
70 |
}
|
|
71 |
} catch (IOException expected) { }
|
|
72 |
});
|
|
73 |
}
|
|
74 |
|
|
75 |
public void testShutdownOutput2() throws IOException {
|
|
76 |
withConnection((s1, s2) -> {
|
|
77 |
s1.setSoTimeout(100);
|
|
78 |
try {
|
|
79 |
s1.getInputStream().read();
|
|
80 |
assertTrue(false);
|
|
81 |
} catch (SocketTimeoutException e) { }
|
|
82 |
|
|
83 |
scheduleShutdownOutput(s1, 2000);
|
|
84 |
byte[] data = new byte[128*1024];
|
|
85 |
try {
|
|
86 |
while (true) {
|
|
87 |
s1.getOutputStream().write(data);
|
|
88 |
}
|
|
89 |
} catch (IOException expected) { }
|
|
90 |
});
|
|
91 |
}
|
|
92 |
|
|
93 |
static void scheduleShutdownInput(Socket s, long delay) {
|
|
94 |
schedule(() -> {
|
|
95 |
try {
|
|
96 |
s.shutdownInput();
|
|
97 |
} catch (IOException ioe) { }
|
|
98 |
}, delay);
|
|
99 |
}
|
|
100 |
|
|
101 |
static void scheduleShutdownOutput(Socket s, long delay) {
|
|
102 |
schedule(() -> {
|
|
103 |
try {
|
|
104 |
s.shutdownOutput();
|
|
105 |
} catch (IOException ioe) { }
|
|
106 |
}, delay);
|
|
107 |
}
|
|
108 |
|
|
109 |
static void schedule(Runnable task, long delay) {
|
|
110 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
111 |
try {
|
|
112 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
113 |
} finally {
|
|
114 |
executor.shutdown();
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
interface ThrowingBiConsumer<T, U> {
|
|
119 |
void accept(T t, U u) throws IOException;
|
|
120 |
}
|
|
121 |
|
|
122 |
static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
|
|
123 |
throws IOException
|
|
124 |
{
|
|
125 |
Socket s1 = null;
|
|
126 |
Socket s2 = null;
|
|
127 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
128 |
s1 = new Socket();
|
|
129 |
s1.connect(ss.getLocalSocketAddress());
|
|
130 |
s2 = ss.accept();
|
|
131 |
consumer.accept(s1, s2);
|
|
132 |
} finally {
|
|
133 |
if (s1 != null) s1.close();
|
|
134 |
if (s2 != null) s2.close();
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
}
|