57207
|
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 |
* @library /test/lib
|
|
27 |
* @build jdk.test.lib.Utils
|
|
28 |
* @run testng Timeouts
|
|
29 |
* @run testng/othervm -Djdk.net.usePlainSocketImpl Timeouts
|
|
30 |
* @summary Test Socket timeouts
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.Closeable;
|
|
34 |
import java.io.IOException;
|
57210
|
35 |
import java.io.InputStream;
|
|
36 |
import java.io.OutputStream;
|
57207
|
37 |
import java.net.ConnectException;
|
|
38 |
import java.net.ServerSocket;
|
|
39 |
import java.net.Socket;
|
|
40 |
import java.net.SocketAddress;
|
|
41 |
import java.net.SocketException;
|
|
42 |
import java.net.SocketTimeoutException;
|
|
43 |
import java.util.concurrent.Executors;
|
|
44 |
import java.util.concurrent.ScheduledExecutorService;
|
|
45 |
import java.util.concurrent.TimeUnit;
|
|
46 |
|
|
47 |
import org.testng.annotations.Test;
|
|
48 |
import static org.testng.Assert.*;
|
|
49 |
import jdk.test.lib.Utils;
|
|
50 |
|
|
51 |
@Test
|
|
52 |
public class Timeouts {
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Test timed connect where connection is established
|
|
56 |
*/
|
|
57 |
public void testTimedConnect1() throws IOException {
|
|
58 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
59 |
try (Socket s = new Socket()) {
|
57210
|
60 |
s.connect(ss.getLocalSocketAddress(), 2000);
|
57207
|
61 |
}
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Test timed connect where connection is refused
|
|
67 |
*/
|
|
68 |
public void testTimedConnect2() throws IOException {
|
|
69 |
try (Socket s = new Socket()) {
|
|
70 |
SocketAddress remote = Utils.refusingEndpoint();
|
|
71 |
try {
|
|
72 |
s.connect(remote, 2000);
|
|
73 |
} catch (ConnectException expected) { }
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
57210
|
78 |
* Test timed read where the read succeeds immediately
|
57207
|
79 |
*/
|
|
80 |
public void testTimedRead1() throws IOException {
|
|
81 |
withConnection((s1, s2) -> {
|
|
82 |
s1.getOutputStream().write(99);
|
57210
|
83 |
s2.setSoTimeout(30*1000);
|
|
84 |
int b = s2.getInputStream().read();
|
|
85 |
assertTrue(b == 99);
|
|
86 |
});
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Test timed read where the read succeeds after a delay
|
|
91 |
*/
|
|
92 |
public void testTimedRead2() throws IOException {
|
|
93 |
withConnection((s1, s2) -> {
|
|
94 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
95 |
s2.setSoTimeout(30*1000);
|
57207
|
96 |
int b = s2.getInputStream().read();
|
|
97 |
assertTrue(b == 99);
|
|
98 |
});
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Test timed read where the read times out
|
|
103 |
*/
|
57210
|
104 |
public void testTimedRead3() throws IOException {
|
57207
|
105 |
withConnection((s1, s2) -> {
|
57210
|
106 |
s2.setSoTimeout(2000);
|
57207
|
107 |
try {
|
|
108 |
s2.getInputStream().read();
|
|
109 |
assertTrue(false);
|
|
110 |
} catch (SocketTimeoutException expected) { }
|
|
111 |
});
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Test timed read that succeeds after a previous read has timed out
|
|
116 |
*/
|
57210
|
117 |
public void testTimedRead4() throws IOException {
|
57207
|
118 |
withConnection((s1, s2) -> {
|
57210
|
119 |
s2.setSoTimeout(2000);
|
57207
|
120 |
try {
|
|
121 |
s2.getInputStream().read();
|
|
122 |
assertTrue(false);
|
|
123 |
} catch (SocketTimeoutException e) { }
|
|
124 |
s1.getOutputStream().write(99);
|
|
125 |
int b = s2.getInputStream().read();
|
|
126 |
assertTrue(b == 99);
|
|
127 |
});
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
57210
|
131 |
* Test timed read that succeeds after a previous read has timed out and
|
|
132 |
* after a short delay
|
57207
|
133 |
*/
|
57210
|
134 |
public void testTimedRead5() throws IOException {
|
57207
|
135 |
withConnection((s1, s2) -> {
|
57210
|
136 |
s2.setSoTimeout(2000);
|
|
137 |
try {
|
|
138 |
s2.getInputStream().read();
|
|
139 |
assertTrue(false);
|
|
140 |
} catch (SocketTimeoutException e) { }
|
|
141 |
s2.setSoTimeout(30*3000);
|
|
142 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
143 |
int b = s2.getInputStream().read();
|
|
144 |
assertTrue(b == 99);
|
|
145 |
});
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Test untimed read that succeeds after a previous read has timed out
|
|
150 |
*/
|
|
151 |
public void testTimedRead6() throws IOException {
|
|
152 |
withConnection((s1, s2) -> {
|
|
153 |
s2.setSoTimeout(2000);
|
57207
|
154 |
try {
|
|
155 |
s2.getInputStream().read();
|
|
156 |
assertTrue(false);
|
|
157 |
} catch (SocketTimeoutException e) { }
|
|
158 |
s1.getOutputStream().write(99);
|
|
159 |
s2.setSoTimeout(0);
|
|
160 |
int b = s2.getInputStream().read();
|
|
161 |
assertTrue(b == 99);
|
|
162 |
});
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
57210
|
166 |
* Test untimed read that succeeds after a previous read has timed out and
|
|
167 |
* after a short delay
|
|
168 |
*/
|
|
169 |
public void testTimedRead7() throws IOException {
|
|
170 |
withConnection((s1, s2) -> {
|
|
171 |
s2.setSoTimeout(2000);
|
|
172 |
try {
|
|
173 |
s2.getInputStream().read();
|
|
174 |
assertTrue(false);
|
|
175 |
} catch (SocketTimeoutException e) { }
|
|
176 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
177 |
s2.setSoTimeout(0);
|
|
178 |
int b = s2.getInputStream().read();
|
|
179 |
assertTrue(b == 99);
|
|
180 |
});
|
|
181 |
}
|
|
182 |
|
|
183 |
/**
|
57207
|
184 |
* Test async close of timed read
|
|
185 |
*/
|
57210
|
186 |
public void testTimedRead8() throws IOException {
|
57207
|
187 |
withConnection((s1, s2) -> {
|
57210
|
188 |
s2.setSoTimeout(30*1000);
|
57207
|
189 |
scheduleClose(s2, 2000);
|
|
190 |
try {
|
|
191 |
s2.getInputStream().read();
|
|
192 |
assertTrue(false);
|
|
193 |
} catch (SocketException expected) { }
|
|
194 |
});
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Test writing after a timed read. The timed read changes the underlying
|
|
199 |
* socket to non-blocking.
|
|
200 |
*/
|
|
201 |
public void testTimedWrite1() throws IOException {
|
|
202 |
withConnection((s1, s2) -> {
|
|
203 |
s1.getOutputStream().write(99);
|
|
204 |
s2.setSoTimeout(3000);
|
|
205 |
int b = s2.getInputStream().read();
|
|
206 |
assertTrue(b == 99);
|
|
207 |
|
|
208 |
// schedule thread to read s1 to EOF
|
57210
|
209 |
scheduleReadToEOF(s1.getInputStream(), 3000);
|
57207
|
210 |
|
|
211 |
// write a lot so that write blocks
|
|
212 |
byte[] data = new byte[128*1024];
|
|
213 |
for (int i = 0; i < 100; i++) {
|
|
214 |
s2.getOutputStream().write(data);
|
|
215 |
}
|
|
216 |
});
|
|
217 |
}
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Test async close of writer (after a timed read). The timed read changes
|
|
221 |
* the underlying socket to non-blocking.
|
|
222 |
*/
|
|
223 |
public void testTimedWrite2() throws IOException {
|
|
224 |
withConnection((s1, s2) -> {
|
|
225 |
s1.getOutputStream().write(99);
|
|
226 |
s2.setSoTimeout(3000);
|
|
227 |
int b = s2.getInputStream().read();
|
|
228 |
assertTrue(b == 99);
|
|
229 |
|
|
230 |
// schedule s2 to be be closed
|
|
231 |
scheduleClose(s2, 3000);
|
|
232 |
|
|
233 |
// write a lot so that write blocks
|
|
234 |
byte[] data = new byte[128*1024];
|
|
235 |
try {
|
|
236 |
while (true) {
|
|
237 |
s2.getOutputStream().write(data);
|
|
238 |
}
|
|
239 |
} catch (SocketException expected) { }
|
|
240 |
});
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
57210
|
244 |
* Test timed accept where a connection is established immediately
|
57207
|
245 |
*/
|
|
246 |
public void testTimedAccept1() throws IOException {
|
|
247 |
Socket s1 = null;
|
|
248 |
Socket s2 = null;
|
|
249 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
250 |
s1 = new Socket();
|
|
251 |
s1.connect(ss.getLocalSocketAddress());
|
57210
|
252 |
ss.setSoTimeout(30*1000);
|
57207
|
253 |
s2 = ss.accept();
|
|
254 |
} finally {
|
|
255 |
if (s1 != null) s1.close();
|
|
256 |
if (s2 != null) s2.close();
|
|
257 |
}
|
|
258 |
}
|
|
259 |
|
|
260 |
/**
|
57210
|
261 |
* Test timed accept where a connection is established after a short delay
|
57207
|
262 |
*/
|
|
263 |
public void testTimedAccept2() throws IOException {
|
|
264 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
265 |
ss.setSoTimeout(30*1000);
|
|
266 |
scheduleConnect(ss.getLocalSocketAddress(), 2000);
|
|
267 |
Socket s = ss.accept();
|
|
268 |
s.close();
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Test timed accept where the accept times out
|
|
274 |
*/
|
|
275 |
public void testTimedAccept3() throws IOException {
|
|
276 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57207
|
277 |
ss.setSoTimeout(2000);
|
|
278 |
try {
|
57210
|
279 |
Socket s = ss.accept();
|
|
280 |
s.close();
|
57207
|
281 |
assertTrue(false);
|
|
282 |
} catch (SocketTimeoutException expected) { }
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
57210
|
287 |
* Test timed accept where a connection is established immediately after a
|
|
288 |
* previous accept timed out.
|
57207
|
289 |
*/
|
57210
|
290 |
public void testTimedAccept4() throws IOException {
|
57207
|
291 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
292 |
ss.setSoTimeout(2000);
|
57207
|
293 |
try {
|
57210
|
294 |
Socket s = ss.accept();
|
|
295 |
s.close();
|
57207
|
296 |
assertTrue(false);
|
|
297 |
} catch (SocketTimeoutException expected) { }
|
|
298 |
try (Socket s1 = new Socket()) {
|
|
299 |
s1.connect(ss.getLocalSocketAddress());
|
|
300 |
Socket s2 = ss.accept();
|
|
301 |
s2.close();
|
|
302 |
}
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
57210
|
307 |
* Test untimed accept where a connection is established after a previous
|
|
308 |
* accept timed out
|
|
309 |
*/
|
|
310 |
public void testTimedAccept5() throws IOException {
|
|
311 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
312 |
ss.setSoTimeout(2000);
|
|
313 |
try {
|
|
314 |
Socket s = ss.accept();
|
|
315 |
s.close();
|
|
316 |
assertTrue(false);
|
|
317 |
} catch (SocketTimeoutException expected) { }
|
|
318 |
ss.setSoTimeout(0);
|
|
319 |
try (Socket s1 = new Socket()) {
|
|
320 |
s1.connect(ss.getLocalSocketAddress());
|
|
321 |
Socket s2 = ss.accept();
|
|
322 |
s2.close();
|
|
323 |
}
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
/**
|
|
328 |
* Test untimed accept where a connection is established after a previous
|
|
329 |
* accept timed out and after a short delay
|
|
330 |
*/
|
|
331 |
public void testTimedAccept6() throws IOException {
|
|
332 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
333 |
ss.setSoTimeout(2000);
|
|
334 |
try {
|
|
335 |
Socket s = ss.accept();
|
|
336 |
s.close();
|
|
337 |
assertTrue(false);
|
|
338 |
} catch (SocketTimeoutException expected) { }
|
|
339 |
ss.setSoTimeout(0);
|
|
340 |
scheduleConnect(ss.getLocalSocketAddress(), 2000);
|
|
341 |
Socket s = ss.accept();
|
|
342 |
s.close();
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
/**
|
57207
|
347 |
* Test async close of a timed accept
|
|
348 |
*/
|
57210
|
349 |
public void testTimedAccept7() throws IOException {
|
57207
|
350 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
351 |
ss.setSoTimeout(30*1000);
|
57207
|
352 |
scheduleClose(ss, 2000);
|
|
353 |
try {
|
|
354 |
ss.accept().close();
|
|
355 |
assertTrue(false);
|
|
356 |
} catch (SocketException expected) { }
|
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
@Test(expectedExceptions = { IllegalArgumentException.class })
|
|
361 |
public void testBadTimeout1() throws IOException {
|
|
362 |
try (Socket s = new Socket()) {
|
|
363 |
s.setSoTimeout(-1);
|
|
364 |
}
|
|
365 |
}
|
|
366 |
|
|
367 |
@Test(expectedExceptions = { IllegalArgumentException.class })
|
|
368 |
public void testBadTimeout2() throws IOException {
|
|
369 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
370 |
try (Socket s = new Socket()) {
|
|
371 |
s.connect(ss.getLocalSocketAddress(), -1);
|
|
372 |
}
|
|
373 |
}
|
|
374 |
}
|
|
375 |
|
|
376 |
@Test(expectedExceptions = { IllegalArgumentException.class })
|
|
377 |
public void testBadTimeout3() throws IOException {
|
|
378 |
try (ServerSocket ss = new ServerSocket()) {
|
|
379 |
ss.setSoTimeout(-1);
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
interface ThrowingBiConsumer<T, U> {
|
|
384 |
void accept(T t, U u) throws IOException;
|
|
385 |
}
|
|
386 |
|
|
387 |
/**
|
|
388 |
* Invokes the consumer with a connected pair of sockets
|
|
389 |
*/
|
|
390 |
static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
|
|
391 |
throws IOException
|
|
392 |
{
|
|
393 |
Socket s1 = null;
|
|
394 |
Socket s2 = null;
|
|
395 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
396 |
s1 = new Socket();
|
|
397 |
s1.connect(ss.getLocalSocketAddress());
|
|
398 |
s2 = ss.accept();
|
|
399 |
consumer.accept(s1, s2);
|
|
400 |
} finally {
|
|
401 |
if (s1 != null) s1.close();
|
|
402 |
if (s2 != null) s2.close();
|
|
403 |
}
|
|
404 |
}
|
|
405 |
|
|
406 |
/**
|
57210
|
407 |
* Schedule c to be closed after a delay
|
57207
|
408 |
*/
|
|
409 |
static void scheduleClose(Closeable c, long delay) {
|
|
410 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
411 |
try {
|
|
412 |
Runnable task = () -> { try { c.close(); } catch (IOException ioe) { } };
|
|
413 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
414 |
} finally {
|
|
415 |
executor.shutdown();
|
|
416 |
}
|
|
417 |
}
|
|
418 |
|
|
419 |
/**
|
57210
|
420 |
* Schedule a thread to connect to the given end point after a delay
|
57207
|
421 |
*/
|
57210
|
422 |
static void scheduleConnect(SocketAddress remote, long delay) {
|
57207
|
423 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
424 |
try {
|
|
425 |
Runnable task = () -> {
|
57210
|
426 |
try (Socket s = new Socket()) {
|
|
427 |
s.connect(remote);
|
57207
|
428 |
} catch (IOException ioe) { }
|
|
429 |
};
|
|
430 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
431 |
} finally {
|
|
432 |
executor.shutdown();
|
|
433 |
}
|
|
434 |
}
|
57210
|
435 |
|
|
436 |
/**
|
|
437 |
* Schedule a thread to read to EOF after a delay
|
|
438 |
*/
|
|
439 |
static void scheduleReadToEOF(InputStream in, long delay) {
|
|
440 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
441 |
try {
|
|
442 |
Runnable task = () -> {
|
|
443 |
byte[] bytes = new byte[8192];
|
|
444 |
try {
|
|
445 |
while (in.read(bytes) != -1) { }
|
|
446 |
} catch (IOException ioe) { }
|
|
447 |
};
|
|
448 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
449 |
} finally {
|
|
450 |
executor.shutdown();
|
|
451 |
}
|
|
452 |
}
|
|
453 |
|
|
454 |
/**
|
|
455 |
* Schedule a thread to write after a delay
|
|
456 |
*/
|
|
457 |
static void scheduleWrite(OutputStream out, byte[] data, long delay) {
|
|
458 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
459 |
try {
|
|
460 |
Runnable task = () -> {
|
|
461 |
try {
|
|
462 |
out.write(data);
|
|
463 |
} catch (IOException ioe) { }
|
|
464 |
};
|
|
465 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
466 |
} finally {
|
|
467 |
executor.shutdown();
|
|
468 |
}
|
|
469 |
}
|
|
470 |
static void scheduleWrite(OutputStream out, int b, long delay) {
|
|
471 |
scheduleWrite(out, new byte[] { (byte)b }, delay);
|
|
472 |
}
|
57207
|
473 |
}
|